[英]Google Sheets script: how do I run a script only on the sheets whose names contain particular text?
(On Google Sheets) I want to create a script that runs only on certain sheets in a spreadsheet. (在 Google 表格上)我想创建一个仅在电子表格中的某些表格上运行的脚本。 To do this I have created a script that creates an array of sheet names whose names (defined as a variable curSheetName) fit the following condition:为此,我创建了一个脚本,该脚本创建一个工作表名称数组,其名称(定义为变量 curSheetName)符合以下条件:
if(curSheetName == "CM:") if(curSheetName == "CM:")
so currently the sheet name must equal "CM:", however I want it to be so that the script runs on all sheets that CONTAIN the text "CM:" in their title, but I can't find how to write that syntactically.所以目前工作表名称必须等于“CM:”,但是我希望它是这样,以便脚本在标题中包含文本“CM:”的所有工作表上运行,但我找不到如何在语法上编写它。 In MySQL it is '%CM:%', in GSheet formulas you just use asterisks..在 MySQL 中,它是 '%CM:%',在 GSheet 公式中,您只需使用星号..
Here is my code:这是我的代码:
// Get list of sheet names in an array format
function sheetnames() {
// Define array
var out = new Array()
// Get sheets
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// Work through each sheet one at a time
for (var i=0 ; i<sheets.length ; i++) {
// define the current sheet
var curSheetName = sheets[i].getName();
// determine if matches criteria
if(curSheetName == "CM:") { //***************** NEED HELP HERE
// put into array if matches criteria
out.push( [ sheets[i].getName() ] );
// RUN FUNCTION HERE
}
}
}
Any help appreciated, Cheers, Mike任何帮助表示赞赏,干杯,迈克
You can use the JavaScript Array.indexOf()
method to achieve this.您可以使用 JavaScript Array.indexOf()
方法来实现此目的。
if (curSheetName.indexOf('CM:') !== -1) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.