如何使用 Google Sheet 脚本计算包含特定文本的单元格数量?
目前,我正在使用以下脚本来计算包含特定文本的单元格数量,例如包含“完整”或“不完整”的单元格。
function COUNT_TEXT_ACROSS_SHEETS(sheetNames, range, text) {
sheetNames = sheetNames.split(',');
var count = 0;
sheetNames.forEach(function(sheetName) {
var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
var values = sheet.getRange(range).getValues();
values.forEach(function(row) {
row.forEach(function(cell) {
if (cell.indexOf(text) !== -1 ) count++;
});
});
});
return count;
};
目前,该脚本对包含文本“完整”和“不完整”的两个单元格进行计数有没有办法修改脚本以仅计算包含完整单词“完整”的单元格,而不是也计算包含“不完整”的单元格,因为单元格还包含关键字:“完整”?
当前脚本的用法示例:
=COUNT_TEXT_ACROSS_SHEETS("Sheet1", "C1:C1000", "NOT COMPLETE")
Use 文本查找器 https://developers.google.com/apps-script/reference/spreadsheet/text-finder.html#findAll() class.
示例脚本:
/**
* @customfunction
* @return count of given text in spreadsheet
* @param {string} text text to search
* @param {boolean} mec whether to match entire cell
* @param {boolean} mc whether to match case
*/
function COUNT_SS(text = 'NOT COMPLETE', mec = true, mc = true) {
return SpreadsheetApp.getActive()
.createTextFinder(text)
.matchEntireCell(mec)
.matchCase(mc)
.findAll()
.length;
}
Usage:
=COUNT_SS()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)