【问题标题】:Is there a way to check for a word within cell?有没有办法检查单元格中的单词?
【发布时间】:2019-05-26 17:05:45
【问题描述】:

我正在寻找一种方法来检查单元格是否包含 Google Apps 脚本和 Google 表格中的文本。

var cell = "Joe, Bob, Tim, John"

//I would want this to execute
if(cell contains "Tim") {
//code
}

【问题讨论】:

  • 您可以使用split() 使其成为一个数组,然后使用includes() 方法。
  • @Xp.L Google Apps 脚本不包含对includes() 的内置支持,因此需要使用 polyfill。

标签: javascript if-statement google-apps-script google-sheets


【解决方案1】:

最近,TextFinder 类已添加到SpreadsheetApp 服务中,允许您在实时Range(或Sheet 甚至Spreadsheet 本身)上执行此操作。有关详细信息,请参阅documentation。只需将查找器设置在带有createTextFinder() 的上下文中,并将您要查找的文本作为参数。 在生成的TextFinder 实例上调用findNext() 方法将返回调用它的Rangenull,如果没有找到。例如:

function findText() {

  var sh = getSheet(); //custom function that returns target Sheet;
  var rng = sh.getRange(1,1); //change to desired Range boundaries;

  //create TextFinder and configure;
  var tf = rng.createTextFinder('textToFind');
      tf.matchCase(false); //{Boolean} -> match target text's case or not;
      tf.matchEntireCell(false); //{Boolean} -> check the whole Range or within;
      tf.ignoreDiacritics(true); //{Boolean} -> ignore diacretic signs during match;
      tf.matchFormulaText(false); //{Boolean} -> search in formulas (if any) or values;

  //invoke search;
  var res = tf.findNext();

  //do something with result;
  if(res!==null) {
    var vals = res.getValues();
    Logger.log(vals);
  }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    相关资源
    最近更新 更多