简体   繁体   English

如果单元格与使用 Apps 脚本的不同工作表列表中的单元格匹配,则条件格式

[英]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 表是否匹配并相应地应用某种格式。

For example:例如:

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:注意事项:

  • You need to use e.range.getValue() instead of e.value so that copy/pasted values can be read您需要使用e.range.getValue()而不是e.value以便可以读取复制/粘贴的值
  • You need to add this script to the Potential cities sheet and authorise it as an installable trigger您需要将此脚本添加到潜在城市表并将其授权为可安装触发器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序脚本条件格式的值不在数组中 - Apps script conditional formatting for value not in array Google Apps脚本,如果语句匹配单元格值 - Google Apps Script If Statement Matches Cell Value Google Script,如何使用正则表达式将一行从 Sheet1 复制到 Sheet2 而不是一个单元格? - Google Script, how can I copy a row from Sheet1 to Sheet2 instead of just one cell by using regex? Google Sheet 单元格显示的数字与应用脚本添加的实际内容不同 - Google Sheet cell displaying number is different than actual content in it added by apps script 寻找不同阵列中的匹配项(Google Apps脚本) - Looking for matches in different arrays (Google Apps Script) 应用程序脚本在对该选项卡进行任何编辑时更新谷歌表格中每个选项卡上的一个单元格中的时间戳 - Apps script to update timestamp in one cell on each tab in a google sheet upon any edit of that tab Google Apps脚本每行随机抽取一个单元格 - Google Apps Script Taking one random cell from each row 从各种 Google 表格单元格中提取不同的链接,使用 Google Apps 脚本将它们作为活动 URL/超链接粘贴到 Google 文档模板中 - Extract different links from various Google Sheet cells, paste them in a Google Doc Template as Active URLs /Hyperlinked using Google Apps Script 将谷歌工作表中的单元格数据放入 html 输入框。 谷歌应用程序脚本 - put cell data from google sheet into html input box. google apps script 使用脚本应用从Google表格中的单元格提取日期 - Pulling Date From Cell in Google Sheet Using Script App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM