如何从脚本中排除某些工作表?

时间:2019-07-18 15:44:34

标签: google-apps-script google-sheets

function sheetnames() {
  var out = new Array()
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var exclude = ["Contractors", "All Group Bids", "About Me","Bid Summary"] // these are the sheet names I do not want 
  var groupsheets = (sheets != exclude); // I'm not sure if this is formatted properly 


  for (var i=0 ; i<groupsheets.length ; i++) 

  {out.push( [ groupsheets[i].getName() ] )}

  return out 
}

获取所有工作表的名称,但某些名称除外。 other code running

2 个答案:

答案 0 :(得分:0)

目标:

在排除数组中找不到工作表名称的返回数组。


修改后的代码:

function sheetNames() {
  var out = [];
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var exclude = ["Contractors", "All Group Bids", "About Me", "Bid Summary"];
  for (i = 0; i < sheets.length; i++) {
    if (exclude.indexOf(sheets[i].getName()) === -1) {
      out.push(sheets[i].getName());
    }
  }
  return out;
}

说明:

我已经使用array.indexOf()遍历了工作表数组以比较两者。返回-1(无匹配项)的任何内容都将添加到数组out中。


参考:

答案 1 :(得分:0)

这里是使用es6 +功能的更简洁版本。这不一定比https://stackoverflow.com/a/57100899/181344更好,但是我总是发现使用命令式循环时更容易出错:

function sheetNames() {
  let excluded = ["Contractors", "All Group Bids", "About Me", "Bid Summary"];
  return SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheets()
    .filter(s => !excluded.includes(s));
}