google 脚本注释行颜色Google 脚本行颜色检测器

google script note row colorGoogle Script row color detector

我在编写 google 脚本函数时需要一些帮助。 我有一个 Google Sheet ,其中每一行可能分配有不同的颜色。 我需要一个脚本,可以记录在该单元格中每一行的第一个单元格中找到的 html 颜色代码。

例如,假设第 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();

  }
}

以下是我自己得出的结论。它完美运行。

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;
    }
  }
}