Conditional formatting if a cell matches one from a list of a different sheet using Apps Script(如果单元格与使用Apps脚本的不同工作表列表中的单元格匹配,则设置条件格式)
问题描述
我在一个选项卡上有一个不断变化的数字列表,如果该单元格出现在另一个数字列表中、不同的工作表中,我希望对其应用条件格式。
Potential Cities/Zip-Codes
List of Blocked Zip Codes
我希望主要";潜在城市";工作表上的邮政编码在";阻止的邮政编码";工作表上列出时格式化。
其目的是创建格式更改,如果用户试图输入的邮政编码被阻止(或在列表中),该更改将非常清楚地向用户显示。常规条件格式不起作用,因为复制/粘贴将覆盖CF规则。我还需要能够将解决方案应用于多个不同的工作表,这些工作表都在根据被阻止的单元格列表检查其单元格。
推荐答案
您可以为您的潜在城市电子表格创建installable onEdit()
trigger,该电子表格检查阻止的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 = "your-spreadsheet-id"
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")
}
}
注意事项:
- 您需要使用
e.range.getValue()
而不是e.value
,以便可以读取复制/粘贴的值 - 您需要将此脚本添加到潜在城市工作表中,并将其授权为可安装触发器
这篇关于如果单元格与使用Apps脚本的不同工作表列表中的单元格匹配,则设置条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果单元格与使用Apps脚本的不同工作表列表中的


基础教程推荐
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01