如果單元格存在于另一個數字串列中,我想在不同的作業表上應用條件格式,我在選項卡上有一個不斷變化的數字串列。
潛在城市/郵政編碼
被封鎖的郵政編碼串列
我希望主“潛在城市”表上的郵政編碼在“封鎖的郵政編碼”表上列出時進行格式化。
目的是創建一個格式更改,如果他們嘗試輸入的郵政編碼被阻止(或在串列中),它將非常清楚地向用戶顯示。正常的條件格式不起作用,因為復制/粘貼會覆寫 CF 規則。我還需要能夠將解決方案應用于多個不同的作業表,這些作業表都根據被阻止的單元格串列檢查它們的單元格。
uj5u.com熱心網友回復:
您可以為您的潛在城市電子表格創建一個可安裝的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 = "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這樣才能讀取復制/粘貼的值 - 您需要將此腳本添加到潛在城市表并將其授權為可安裝觸發器
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/410902.html
標籤:
