如何转义查询函数中的[]

问题描述 投票:0回答:2

我正在尝试将所有工作表中的数据合并到一张工作表中。如何转义查询函数中的 []?

function listAllProducts() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = spreadsheet.getSheets();

  // Start from index 1 to skip the first sheet
  for (var i = sheets.length - 1; i > 1; i--) {
    QUERY({sheets[i]!A2:Z},"select * where Col1 is not null");
  }   
}

Google 表格 - Google 应用程序脚本

我希望每个工作表的名称都会自动填充到查询中,这样我就不必手动键入每个查询。每张纸的长度和尺寸各不相同,并且有很多张。也许有更好的方法来做到这一点?

javascript google-sheets google-apps-script
2个回答
1
投票

迭代从索引 1 开始的工作表,并从活动工作表中获取数据范围。

function listAllProducts() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = spreadsheet.getSheets();

    // Start from index 1 to skip the first sheet
    for (var sheetIdx = 1; sheetIdx < sheets.length; sheetIdx++) {
      //Retrieve the active sheet
      var sheet = sheets[sheetIdx];
      //Retrieve the dataRange of interest from the sheet
      var dataRange = sheet.getRange("A2:Z");
      //The above will get around you [ problem; however it
      //appears you can not call the QUERY sheet function from 
      //App Script.  You can not call sheet functions from App 
      //Script.  You could set this as a formula within a cell, 
      //but not sure exactly what you are looking to do??  
      //QUERY(dataRange,"select * where Col1 is not null");
    }   
}

注意:您无法从 Google App Script 调用 Google Sheet 函数 QUERY。如果您提供更多背景信息,也许我们可以为最后一步提供解决方案。


0
投票

列出二维数组数组中的所有工作表内容,并在每个工作表数据数组的顶行上添加标签

function listAllProducts() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const incl = ["Sheet0","Sheet1","Sheet2"];//sheets to include
  const mshn = "Sheet4";//master sheet
  const msh = ss.getSheetByName(mshn);
  msh.clearContents();
  const shts = ss.getSheets().filter(sh => ~incl.indexOf(sh.getName()));
  const dA = [];
  shts.forEach(sh => {
    let vs = sh.getDataRange().getValues().filter(r => r[0] != "").filter(e => e);
    let nA = [...new Array(vs[0].length)];
    nA[0] = sh.getName();
    vs.unshift(nA);
    dA.push(vs);//put sheet name in list
  });
  //Logger.log(JSON.stringify(dA));
  dA.forEach((arr => {
    msh.getRange(msh.getLastRow() + 1,1,arr.length,arr[0].length).setValues(arr);
  })); 
}
© www.soinside.com 2019 - 2024. All rights reserved.