脚本执行期间活动行中的第一个单元格

时间:2014-11-10 19:38:52

标签: google-apps-script

我正在使用下面的代码查看工作表,如果任何其他单元格为红色,则将第一个单元格背景更改为红色。我想要的是,将每行中的第一个单元格更改为红色,仅当该行包含红色单元格时。此代码通过电子表格工作,但只使单元格A1变为红色。如何让它独立影响每一行的第一个单元?

    function MakeRed(){
  var book = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = book.getActiveSheet();
  var first_column = "B";
  var first_row = 1;
  var last_row = sheet.getLastRow();
  var last_column = sheet.getLastColumn();
  var active_row = 1;
  var range_input = sheet.getRange(1,1,last_row,last_column);
  var range_output = sheet.getRange("A1");
  var cell_colors = range_input.getBackgroundColors();
  var color = "#ff0000";
  var count = 0;

  for(var r = 0; r < cell_colors.length; r++) {
    for(var c = 0; c < cell_colors[0].length; c++) {
      if(cell_colors[r][c] == color) {
        count++;
        range_output.setBackground("#ff0000");
      } else {
        count--;
        if (count == 0) {
          range_output.setBackground("#ffffff");
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:1)

我建议你在已经拥有的背景颜色数组中完成所有内容,而不是每次条件为真时调用电子表格服务。

代码变得更简单,因为我在条件

中使用了indexOf数组方法
function MakeRed(){
  var book = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = book.getActiveSheet();
  var first_column = "B";
  var first_row = 1;
  var last_row = sheet.getLastRow();
  var last_column = sheet.getLastColumn();
  var active_row = 1;
  var range_input = sheet.getRange(1,1,last_row,last_column);
  var range_output = sheet.getRange("A1");
  var cell_colors = range_input.getBackgroundColors();
  var color = "#ff0000";
  var count = 0;

  for(var r = 0; r < cell_colors.length; r++) {
    Logger.log(cell_colors[r].indexOf(color));// just to check, you can remove this of course ...
    if(cell_colors[r].indexOf(color)>-1) {
      count++;
      cell_colors[r][0]=color;
    } else {
      count--;
      if (count == 0) {
        cell_colors[r][0]="#ffffff";
      }
    }
  }
  range_input.setBackgroundColors(cell_colors);// update sheet colors
}

编辑:我真的不明白“计数”背后的想法......如果没有它,它的工作原理相同(下面的代码):

function MakeRed(){
  var book = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = book.getActiveSheet();
  var first_column = "B";
  var first_row = 1;
  var last_row = sheet.getLastRow();
  var last_column = sheet.getLastColumn();
  var active_row = 1;
  var range_input = sheet.getRange(1,1,last_row,last_column);
  var range_output = sheet.getRange("A1");
  var cell_colors = range_input.getBackgroundColors();
  var color = "#ff0000";

  for(var r = 0; r < cell_colors.length; r++) {
    Logger.log(cell_colors[r].indexOf(color))
    if(cell_colors[r].indexOf(color)>-1) {
      cell_colors[r][0]=color;
    } else {
      cell_colors[r][0]="#ffffff";
    }
  }
  range_input.setBackgroundColors(cell_colors);// update sheet colors
}

编辑2 :如果行中没有其他红色单元格,此版本会将col A重置为白色

function MakeRed(){
  var book = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = book.getActiveSheet();
  var first_column = "B";
  var first_row = 1;
  var last_row = sheet.getLastRow();
  var last_column = sheet.getLastColumn();
  var active_row = 1;
  var range_input = sheet.getRange(1,1,last_row,last_column);
  var range_output = sheet.getRange("A1");
  var cell_colors = range_input.getBackgroundColors();
  var color = "#ff0000";

  for(var r = 0; r < cell_colors.length; r++) {
    var rowWoColA = cell_colors[r].slice(1);
    if(rowWoColA.indexOf(color)>-1) {
      cell_colors[r][0]=color;
    } else {
      cell_colors[r][0]="#ffffff";
    }
  }
  range_input.setBackgroundColors(cell_colors);// update sheet colors
}

答案 1 :(得分:0)

var range_output = sheet.getRange("A1");更改为您要编辑的范围。

您可以在循环中执行此操作:

for(var r = 0; r < cell_colors.length; r++) {
    for(var c = 0; c < cell_colors[0].length; c++) {
      if(cell_colors[r][c] == color) {
        count++;
        // Assuming you only want to change bg color of the first column on each row
        sheet.getRange(r,1).setBackground("#ff0000");
      } else {
        count--;
        if (count == 0) {
          sheet.getRange(r,1).setBackground("#ffffff");
        }
      }
    }
  }