Conditional Formatting if Cells in Column Contain Exact Match, Partial or None of Values in Cell

huangapple go评论69阅读模式
英文:

Conditional Formatting if Cells in Column Contain Exact Match, Partial or None of Values in Cell

问题

我对尝试基于列顶部的单元格应用条件格式化而感到困惑,当高亮取决于精确匹配或匹配的反义,部分匹配或无匹配时。请参见此处的示例Google表格。

以下是标准...

  1. 精确匹配可以是精确匹配或A1中的内容的反义。对于精确匹配,与A1匹配的列中的单元格将被着色为绿色。但是,精确匹配可以是X,Y,也可以是反义Y,X。关键是精确匹配必须具有这两个值,顺序不重要。

  2. 对于部分匹配,它们需要被着色为橙色。部分匹配应该在单元格中具有A1中的一个值。例如,如果A1有B,D,则任何值可以在匹配的一个值之前或之后。 (B,) 或 (,B) 或 (D,) 或 (,D) 或仅B或仅D。

  3. 任何不包含A1中任何值的单元格都会被突出显示为红色。
    因此,不包含A1中任何值的任何单元格都应为红色。

有关更多解释和示例,请查看上面的电子表格链接。

英文:

I'm stumped on trying to apply conditional formatting to a column based on a cell at the top of the column when the highlighting is dependent on exact match or inverse of match, partial match, or no match. See example Google Sheet here.

Here's the criteria...

1. Exact matches can be exact or also the inverse of what's in A1.
For an exact match, the cells in the column that match A1 would be colored green. But, an exact match could be X,Y, or the inverse Y,X. The point is that an exact match has to have those 2 values, the order doesn't matter.

2. For partial matches, they need to be colored orange. A partial match should have one of the values from A1 in the cell. For example, if A1 has B,D, then any value can precede or follow a match of one of the values. (B,) or (,B) or (D,) or (,D) or just B by itself or just D by itself.

3. Any cells with none of the values from A1 in them are highlighted red.
So, any cell that does not contain any of the values from A1 should be red.

See spreadsheet link above for further explanation and examples.

答案1

得分: 1

这里有一个有条件格式设置的选项。我不确定我是否完全理解了你关于橙色和红色的要求,但这里有一个你可能可以适应的选项。

如果所有选项都匹配,它将返回绿色。这是通过将每个单元格与A1中的值拆分并比较完成的。通过使用REGEXMATCH计数过滤后的数据,并将其与两个单元格中的项目数量进行比较,您将能够知道每个选项是否都匹配。

要获得橙色,您将有类似的操作,但不需要最后一步。

至于红色,您可以使用NOT与REGEXMATCH结合使用。

绿色: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(A3,",",1,1),(COUNTA(IFNA(FILTER(a,REGEXMATCH(a,JOIN("|",v))))))=COUNTA(v))*(COUNTA(v)=COUNTA(a))

橙色: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(A3,",",1,1),COUNTA(IFNA(FILTER(a,REGEXMATCH(a,JOIN("|",v)))))

红色: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(TO_TEXT(A3),",",1,1),COUNTA(IFNA(FILTER(a,NOT(REGEXMATCH(a,JOIN("|",v))))))=COUNTA(a)

英文:

Here you have an option with conditional formatting. I don't know if I fully understood your requirements about orange and red, but here you have an option you may be able to adapt

If all the options are a match, it will return green. It's done by splitting and comparing each cell with the value in A1. By counting the Filtered data with REGEXMATCH, and comparing to the amount of items in both cells you'll be able to know if every option is a match

To orange you'll have something similar but without the last step

And for red you use NOT in combination with REGEXMATCH

Green: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(A3,",",1,1),(COUNTA(IFNA(FILTER(a,REGEXMATCH(a,JOIN("|",v)))))=COUNTA(v))*(COUNTA(v)=COUNTA(a)))

Orange: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(A3,",",1,1),COUNTA(IFNA(FILTER(a,REGEXMATCH(a,JOIN("|",v))))))

Red: =LET(v,SPLIT($A$1,",",1,1),a,SPLIT(TO_TEXT(A3),",",1,1),COUNTA(IFNA(FILTER(a,NOT(REGEXMATCH(a,JOIN("|",v))))))=COUNTA(a))

Conditional Formatting if Cells in Column Contain Exact Match, Partial or None of Values in Cell

答案2

得分: 1

以下是已翻译的代码部分:

替代解决方案:

您可以尝试使用Apps Script根据参考单元格格式化列。请参考下面的脚本,根据您提供的表格进行调整。

  1. 单元格参考位于单元格A1上。
  2. 脚本应用于B2:B列中的值。

脚本:

您可以将此示例脚本用作解决问题的基础。

function applyConditionalFormatting() {
  
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); //获取活动电子表格
  var lastRow = sheet.getLastRow();
  var range = sheet.getRange("B2:B" + lastRow); //声明将应用脚本的列以及带有值的最后一行
  var numRows = range.getNumRows();

  var orangeBackground = '#FFA500';
  var greenBackground = '#00FF00';
  var redBackground = '#ff0000';


  var criteria = sheet.getRange("A1").getValue().toString().split(",");   //获取参考单元格的值

  for (var row = 1; row <= numRows; row++) {
    var cell = range.getCell(row, 1);
    var value = cell.getValue().toString(); //迭代提供的范围上的每个值

    var hasCriteria = [];
    for (var i = 0; i < criteria.length; i++) {
      var currentCriteria = criteria[i].trim();
      hasCriteria[i] = value.indexOf(currentCriteria) >= 0; //检查每个单元格值是否具有“B”或“D”
    }
    var allCriteriaMatched = hasCriteria.every(function (c) {
      return c; //检查每个单元格值是否具有“B,D”或“D,B”
    });

    if (allCriteriaMatched) {
      cell.setBackground(greenBackground); //完全匹配时设置颜色为绿色
    } else if (hasCriteria.some(function (c) {
      return c;
    })) {
      cell.setBackground(orangeBackground); //部分匹配时设置颜色为橙色
    } else {
      cell.setBackground(redBackground); //如果都不匹配,则设置颜色为红色
    }
  }
}

输出:

Conditional Formatting if Cells in Column Contain Exact Match, Partial or None of Values in Cell

注意:我只是在表格中添加了一个按钮,并附加了上面的脚本,以进行演示目的

参考:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

英文:

Alternate Solution:

You may try using Apps Script to format a column based on a reference cell. Please see the script below based from your sheets provided.

  1. Cell reference is on cell A1
  2. The script is applied on the values in Column B2:B

Script:

You can use this sample script as a baseline to your problem.

function applyConditionalFormatting() {
var sheet = SpreadsheetApp.getActive().getActiveSheet(); //get the active spreadsheeet
var lastRow = sheet.getLastRow();
var range = sheet.getRange(&quot;B2:B&quot; + lastRow); //declaring the column in which the script will be applied to along with last row with values
var numRows = range.getNumRows();
var orangeBackground = &#39;#FFA500&#39;;
var greenBackground = &#39;#00FF00&#39;;
var redBackground = &#39;#ff0000&#39;;
var criteria = sheet.getRange(&quot;A1&quot;).getValue().toString().split(&quot;,&quot;);   //getting the value of reference cell
for (var row = 1; row &lt;= numRows; row++) {
var cell = range.getCell(row, 1);
var value = cell.getValue().toString(); // iterating on every value on the range provided
var hasCriteria = [];
for (var i = 0; i &lt; criteria.length; i++) {
var currentCriteria = criteria[i].trim();
hasCriteria[i] = value.indexOf(currentCriteria) &gt;= 0; //checking if each of the cell value has either &quot;B&quot; or &quot;D&quot;
}
var allCriteriaMatched = hasCriteria.every(function (c) {
return c; //checking if each of the cell value has either &quot;B,D&quot; or &quot;D,B&quot;
});
if (allCriteriaMatched) {
cell.setBackground(greenBackground); //setting the color to green for full match
} else if (hasCriteria.some(function (c) {
return c;
})) {
cell.setBackground(orangeBackground); // setting the color to orange forp partial match
} else {
cell.setBackground(redBackground); //setting the color to red if neither
}
}
}

Output:

Conditional Formatting if Cells in Column Contain Exact Match, Partial or None of Values in Cell

Note: I just added a button in the sheet and attached the script above for demo purposes

References:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

huangapple
  • 本文由 发表于 2023年4月4日 08:01:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定