[英]Conditional formatting if a cell matches one from a list of a different sheet using Apps Script
I have a changing list of numbers on a tab that I would like to apply conditional formatting to, if the cell is present in another list of numbers, on a different sheet.如果单元格存在于另一个数字列表中,我想在不同的工作表上应用条件格式,我在选项卡上有一个不断变化的数字列表。
Potential Cities/Zip-Codes 潜在城市/邮政编码
List of Blocked Zip Codes 已阻止的 Zip 代码列表
I would like the zip codes on the main "Potential Cities" sheet, to be formatted when they are listed on the "Blocked Zip Codes" sheet.我希望主“潜在城市”表上的 zip 代码在“阻止的 Zip 代码”表上列出时进行格式化。
The purpose is to create a formatting change that will very plainly show the user if a zip code they are trying to enter is blocked (or on the list).目的是创建一个格式更改,如果他们尝试输入的 zip 代码被阻止(或在列表中),它将非常清楚地向用户显示。 Normal conditional formatting does not work because copy/paste will overwrite CF rules.
正常的条件格式不起作用,因为复制/粘贴会覆盖 CF 规则。 I also need to be able to apply the solution to multiple different sheets, that are all checking their cells against the list of blocked cells.
我还需要能够将解决方案应用于多个不同的工作表,这些工作表都根据被阻止的单元格列表检查它们的单元格。
You can create an installable onEdit()
trigger for your Potential Cities Spreadsheet which checks the Blocked Zips sheet for a match and applies some kind of formatting accordingly.您可以为您的潜在城市电子表格创建一个可安装的
onEdit()
触发器,该触发器会检查 Blocked Zips 表是否匹配并相应地应用某种格式。
function checkForBlockedZips(e) {
// do nothing if not column D
if (e.range.getColumn() !== 4) return
// get list of zips from blocked zips sheet
const blockedZipsSsId = "1T9BbifVJjVMqH5iaOvxZjgmZ24ByKsft9nfUGhbDQls"
const blockedZipsSs = SpreadsheetApp.openById(blockedZipsSsId)
const blockedZipsSheet = blockedZipsSs.getSheetByName("Sheet1")
const zipCodes = blockedZipsSheet.getRange("A2:A").getValues()
.flat(2)
.filter(x => x)
// check if the entered value is in the list of blocked zips
if (~zipCodes.indexOf(e.range.getValue())) {
// create cell style
const strikethrough = SpreadsheetApp.newTextStyle()
.setStrikethrough(true)
.build()
const richText = SpreadsheetApp.newRichTextValue()
.setText(e.range.getValue())
.setTextStyle(strikethrough)
.build()
// set the cell to have the desired rich text style
e.range.setRichTextValue(richText).setBackground("yellow")
}
else {
// if the value is not a blocked zip then reset the cell style
const nostrikethrough = SpreadsheetApp.newTextStyle()
.setStrikethrough(false)
.build()
const richText = SpreadsheetApp.newRichTextValue()
.setText(e.range.getValue())
.setTextStyle(nostrikethrough)
.build()
e.range.setRichTextValue(richText).setBackground("white")
}
}
Things to note:注意事项:
e.range.getValue()
instead of e.value
so that copy/pasted values can be reade.range.getValue()
而不是e.value
以便可以读取复制/粘贴的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.