2017-01-06 11 views
-1

Мне нужно немного помочь написать функцию скрипта google. У меня есть лист Google, в котором каждая отдельная строка может иметь другой цвет, назначенный ему. Мне нужен скрипт, который может отметить код цвета html, найденный в первой ячейке каждой строки в этой ячейке.google script note row colorGoogle Script color color detector

Так, например, скажем, строка 1 - это зеленая строка, а строка 2 - синяя строка, ячейка A1 должна сказать # 00ff00, а A2 должен сказать # 0000ff после запуска скрипта. Ниже я до сих пор.

function colorDetector() { 
    var startRow = 1; // First row of data to process 
    var numRows = 3; // Number of rows to process 
    var currentsheet = 'Production' // What sheet you would like to process (must be within ' ') 
    //This section prepares the document to be read 
    var ss = SpreadsheetApp.getActive(); 
    var sheet = ss.getSheetByName(currentsheet); 
    var dataRange = sheet.getRange(startRow, 1, numRows, 15) // Fetch values for each row in the Range (row, column, numRows, numColumns) 
    var data = dataRange.getValues(); 
    for (var i = 0; i < data.length; ++i) { 
    var row = data[i]; 

    //this is the section i cant seem to get working correctly 
    var color = row[0].getbackground(); //should set the variable "color" to the html color found in the first cell of the current row. 

    sheet.getRange(startRow + i, 1).setValue(color);  // notes the variable found 
    SpreadsheetApp.flush(); 

    } 
} 
+0

Считаете ли вы использование getbackgrounds (множественное число) и setValues? Таким образом, вам не нужен цикл «для». – utphx

ответ

0

Вот что я в конечном итоге выяснил самостоятельно. Он работает отлично.

function colorDetector2() { 
    var startRow = 1; // First row of data to process 
    var currentsheet = 'sheet 4' // What sheet you would like to process (must be within ' ') 
    var ss = SpreadsheetApp.getActive(); 
    var sheet = ss.getSheetByName(currentsheet); 
    var numRows = 10; // Number of rows to process 
    var dataRange = sheet.getRange(startRow, 1, numRows, 20) // Fetch values for each row in the Range (row, column, numRows, numColumns) 
    var colordata = dataRange.getBackgrounds(); 
    var data = dataRange.getValues(); 
    for (var i = 0; i < data.length; ++i) { 
    var color = colordata[i] 
    var colorconv = String(color[1]); 
    if (colorconv == "#00ff00") { 
     //do something here; 
    } else { 
     //do something else here; 
    } 
    } 
}