javascript - 有没有办法检查单元格中的单词?
问题描述
我正在寻找一种方法来检查单元格是否包含 Google Apps 脚本和 Google 表格中的文本。
var cell = "Joe, Bob, Tim, John"
//I would want this to execute
if(cell contains "Tim") {
//code
}
解决方案
最近,服务中TextFinder
添加了一个类SpreadsheetApp
,允许您在现场Range
(Sheet
甚至是Spreadsheet
自己)上执行此操作。有关更多详细信息,请参阅文档。只需将查找器设置在上下文中createTextFinder()
,并将您要查找的文本作为参数。findNext()
在结果TextFinder
实例上调用方法将返回Range
它被调用的方法,或者null
如果没有找到。例如:
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);
}
}
推荐阅读
- google-chrome - 在使用 chrome.debugger api 时,它会自动关闭并说目标已关闭,但选项卡仍处于打开状态,有人可以帮忙吗?
- typescript - 如何使用 v3 sdk 开玩笑地模拟 AWS S3 GetObjectCommand?
- github - 如何删除我的 github 页面的旧证书?
- sql - 当它与时间之间的时间不同时选择值
- python - 在 python 计数函数中为给定数字的倍数添加时间延迟
- node.js - 我可以使用 dotenv 加载包含 bash 环境变量导出的 .env 文件吗
- css - 反应原生纸 -
错误还是我愚蠢? - javascript - 在头部加载RequireJS
- c++ - 常量指针与指针:C++
- javascript - Chrome 扩展 - 通过后端快速服务器进行 oauth 身份验证