简体   繁体   English

Google 表格脚本:如何仅在名称包含特定文本的表格上运行脚本?

[英]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()方法来实现此目的。


Example:例子:

if (curSheetName.indexOf('CM:') !== -1) {
  ...
}

Documentation:文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在多张工作表上运行 Google 脚本? - How do I run Google script on multiple sheets? 如何在多个工作表上运行脚本,Google Sheets - How to run a script on multiple sheets, Google Sheets 如何使用脚本编辑器替换谷歌表格中的文本 - how do I replace text in google sheets using script editor 如何在 Google 表格中的所有表格(选项卡)上运行 Google 表格应用脚本? - How do I run Google Sheets App Script on all sheets (tabs) in a Google Sheet? Google 表格在所有表格中运行脚本 - Google Sheets run the script in all sheets 如何使用 Google Apps 脚本将 Google 表格单元格中的文本字符串解析为 CSV 文件? - How do I parse text strings in Google Sheets cells to a CSV file with Google Apps Script? 如何在所有工作表上运行脚本,谷歌表格 - How to run script on all worksheets, Google Sheets 如何让我的脚本在 Google 表格的多个表格上运行? 我正在插入带有数据的行并需要它在多张纸上运行 - How can I make my script run on multiple sheets on Google Sheets ? I'm inserting rows with data and need it to run on multiple sheets 关闭时运行 Google Sheets 脚本 - Run Google Sheets script on closing 使用Google表格,我如何每小时运行一个脚本并填充列中的每个单元格? - Using Google Sheets, how do I run a script every hour and populate each cell in a column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM