【问题标题】:Google Sheets - Highlight cell if value exists in other sheetGoogle表格 - 如果其他表格中存在值,则突出显示单元格
【发布时间】:2020-02-17 10:27:03
【问题描述】:

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

建议的解决方案

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

【问题讨论】:

  • 您好,您的问题解决了吗?

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


【解决方案1】:

您可以使用MATCH函数搜索其他工作表。

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

【讨论】:

    【解决方案2】:

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

    • 使用getRangegetValuessheet1(C、D 列)中检索值。对于每一行,执行以下操作:
    • 检查 C 列中的复选框是否已选中。
    • 从 D 列检索具有逗号分隔值的数组并过滤它们,以便只保留包含 TF 的数组。 String.prototype.split()Array.prototype.filter() 用于此目的。
    • 使用getRangegetValues 检索具有来自sheet2(E 列)值的数组。
    • 检查第一个数组中的任何值(D 列中的逗号分隔值)是否与第二个数组中的任何值(sheet2 中的值)与Array.prototype.some() 匹配。
    • 如果找到匹配项,请使用 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) {

    参考:

    【讨论】:

    • 嗨 iamblichus,这适用于一个细胞,但不适用于所有细胞。我想为工作表 1 中的每一行逐行检查。然后在工作表 2 中,我想检查整个工作表单元格而不是特定单元格。
    • @TristanC 正如它所写的,它适用于所有单元格。但是只有一个单元格满足条件(D9),所以它是唯一一个改变背景颜色的单元格。为什么你认为它只适用于一个细胞?
    猜你喜欢
    • 2016-07-03
    • 1970-01-01
    • 2018-03-30
    • 2016-08-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多