Google表格-如果其他表格中存在值,则突出显示单元格

时间:2020-02-17 10:27:03

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

如果值与另一个工作表中的单元格匹配,我想突出显示一个单元格。

建议的解决方案

FOR EACH row in 'sheet1'

IF column C == TRUE 
&& column D (value split by comma, only search values with 'tf' in) does not match any cell in 'sheet2'

Highlight the cell Red

示例值

手机D9

“平台,TF平台,TF用户”

  1. 请勿在sheet2中搜索“平台”
  2. 在sheet2中搜索“ TF平台”
  3. 在sheet2中搜索“ TF用户”

示例表

https://docs.google.com/spreadsheets/d/1eI9FTVNuyMEzQTJnHzufz_gElE53AMmk-mxVEWKPdRk/edit?usp=sharing

2 个答案:

答案 0 :(得分:0)

您可以使用MATCH功能搜索其他工作表。

例如:=NOT(ISERROR(MATCH(D1,'SheetName'!C:C,0)))D1[SheetName]中搜索C单元格值

答案 1 :(得分:0)

您可以在Apps脚本中执行以下操作:

  • 使用getRangegetValuessheet1(C,D列)中检索值。对于每一行,请执行以下操作:
  • 检查是否已选中C列中的复选框。
  • 从D列中检索带有逗号分隔值的数组,并对其进行过滤,以便仅保留包含TF的数组。 String.prototype.split()Array.prototype.filter()用于此目的。
  • 使用getRangegetValuessheet2(E列)中的值检索数组。
  • 检查第一个数组中的任何值(列D中的逗号分隔值)是否与Array.prototype.some()中第二个数组中的值(sheet2中的值)相匹配。
  • 如果找到匹配项,请使用setBackground(color)更改单元格的背景颜色。

可能与以下几行有关:

function highlightCells() {
  var ss = SpreadsheetApp.getActive();
  var sheet1 = ss.getSheetByName("sheet1");
  var sheet2 = ss.getSheetByName("sheet2");
  // Get the values from sheet1 (column C, D):
  var firstRow = 1;
  var firstCol = 3;
  var numRows = sheet1.getLastRow() - firstRow + 1;
  var numCols = 2;
  var originValues = sheet1.getRange(firstRow, firstCol, numRows, numCols).getValues();
  // Iterate through each row in sheet1:
  originValues.forEach(function(row, i) {
    var checkbox = row[0];
    if (checkbox === true) { // Check that checkbox in column C is checked
      var cellValue = row[1];
      // Get the comma-separated values in column D (only if the contain `TF`, capitalized):
      var values = cellValue.split(",").filter(function(value) { 
        return value.indexOf("TF") !== -1;
      });
      // Get the values from sheet2:
      var firstRow2 = 1;
      var column = 5;
      var numRows2 = sheet2.getLastRow() - firstRow2 + 1;
      var valuesSheet2 = sheet2.getRange(firstRow2, column, numRows2).getValues().map(function(value) { 
        return value[0];
      });
      // Look for any comma-separated value in column D (sheet1) that matches a value in column E (sheet2):
      var found = values.some(function(value) { 
        return valuesSheet2.indexOf(value) > -1;
      });
      // If any value matches, change background color in to red:
      if (found) {
        sheet1.getRange(i + firstRow, 4).setBackground("red");
      }
    }
  });
}

我不确定如果值匹配或是否不匹配(您的问题不清楚)是否要设置背景色。如果值匹配,则会更改背景颜色。如果您希望它不匹配时进行更改,则应改为if (!found) {

参考:

相关问题