如果单元格与使用Apps脚本的不同工作表列表中的单元格匹配,则设置条件格式 [英] Conditional formatting if a cell matches one from a list of a different sheet using Apps Script

查看:0
本文介绍了如果单元格与使用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脚本的不同工作表列表中的单元格匹配,则设置条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆