2017-01-06 132 views
-1

我需要一点点的帮助写一个脚本谷歌的功能。 我有一个谷歌表,每个单排可以分配有不同的颜色。 我需要一个脚本,可以注意到,在每一行的第一个单元格中发现的HTML颜色代码,该单元格。谷歌脚本注排colorGoogle脚本一行色标检测器

因此,例如,让我们说第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?这样你就不需要'for'循环。 – 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; 
    } 
    } 
}