如果行上的单元格B-J为空,则突出显示colA中的单元格

时间:2013-05-29 02:19:37

标签: google-apps-script google-sheets gs-conditional-formatting

我在Google云端硬盘电子表格中有一个简单的电子表格,其中包含colA中的名称列表。通过ColJ的ColB填写空白单元格。

当ColB通过ColJ没有输入数据时,我想更改ColA中单元格的背景颜色,但是我没有办法做到这一点。

2 个答案:

答案 0 :(得分:1)

您认为条件格式化会提供此功能,但它不允许您引用其他单元格。 (与Excel不同。)

这是一个可以完成这项工作的脚本。 (如果您是Google Apps脚本新手,请阅读this introduction。)

/**
 * The onEdit() function, when defined, is automatically invoked whenever the
 * spreadsheet is edited. (Not every change is an "edit".)
 *
 * If all of the "fill in the blanks" columns are empty, set the
 * background color of the name column in that row.
 */
function onEdit(event) {
  var row = event.range.getRow();
  var sheet = event.range.getSheet();
  var cellA = sheet.getRange(row,1);
  // Get the range of cells B-J that make up the "fill in the blanks" range...
  var fillInTheBlanks = sheet.getRange(row, 2, 1, 9)
                             .getValues()[0]  // ... get values in 0-th row
                             .join('');       // ... and join into one string

  // Check that string - if it is empty, the range was blank,
  // and we should set the background color to, say, yellow.
  if (fillInTheBlanks.length == 0) {
    cellA.setBackground("yellow");
  }
  // On the other hand, if the string was NOT blank, there is something
  // in the "fill in the blanks" range, so we will clear the background.
  else {
    cellA.setBackground("white");
  }
}

/**
 * The onOpen() function, when defined, is automatically invoked whenever the
 * spreadsheet is opened.
 *
 * Iterates through all rows in spreadsheet, faking onEdit triggers, as if
 * each name has been edited.
 */
function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var lastRow = sheet.getLastRow();
  for (var row=1; row <= lastRow; row++) {
    onEdit({range:sheet.getRange('A'+row)});
  }
};

答案 1 :(得分:0)

从ColumnA清除格式,选择ColumnA和格式,条件格式...,格式化单元格如果... 自定义公式并且:

=and(A1<>"",counta(B1:J1)=0)

然后选择填充选项和完成