有没有一种方法可以检查单元格中的单词? [英] Is there a way to check for a word within cell?

查看:95
本文介绍了有没有一种方法可以检查单元格中的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for a way to check if a cell contains text in Google Apps Script and Google Sheets.

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

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

推荐答案

最近,在SpreadsheetApp服务中添加了TextFinder类,允许您在实时Range(或或什至Spreadsheet本身).有关更多详细信息,请参见文档.只需在带有createTextFinder()的上下文中将查找器设置为您要查找的文本作为参数. 在生成的TextFinder实例上调用findNext()方法将返回被调用的Range,如果未找到则返回null.例如:

Recently, a TextFinder class was has been added to the SpreadsheetApp service that allows you to do this on a live Range (or Sheet or even a Spreadsheet itself). See documentation for more details. Simply set the finder on a context with a createTextFinder() with the text you want to find as an argument. Calling a findNext() method on the resulting TextFinder instance will either return the Range it was called on or null if nothing was found. For example:

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);
  }

}

这篇关于有没有一种方法可以检查单元格中的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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