【问题标题】:Wildcard expression when searching for sheet name in google sheets. Google App Script在谷歌工作表中搜索工作表名称时的通配符表达式。谷歌应用脚​​本
【发布时间】:2020-02-26 19:21:16
【问题描述】:

尝试在导入的文件夹中搜索某些 google 表格时遇到问题。 导入的每个 google 工作表都有一个常规名称,然后是一个唯一 ID。例如:手动导出(1024508324)

我创建了一个脚本,它将文件夹中的所有 google 表格导入到一个主 google 表格中。以下: '''

function getdata() {
  //declare multiple variables in one statement - Initial value will
  //be undefined
  var destinationSpreadsheet,destinationSS_ID,destsheet,destrange,file,files,
      sourcerange,sourceSS,sourcesheet,srcSheetName,sourcevalues;
  var pattern = /.Manual./;
  srcSheetName = (pattern)   
  destinationSS_ID = "1DmG5DuWmBqaImUuVvXrhH6mRRozsv7ksjYoFLbEhVGY";
  files = DriveApp.getFolderById("13Z0RSeWnzW9mbm1wnTOEQrkzxUFCdCZF").getFiles();
  destinationSpreadsheet = SpreadsheetApp.openById(destinationSS_ID);
  destsheet = destinationSpreadsheet.getSheetByName('Master');  

  while (files.hasNext()) {
    file = files.next();
    if (file.getMimeType() !== "application/vnd.google-apps.spreadsheet") {
      continue;
    };
    sourceSS = SpreadsheetApp.openById(file.getId());
    sourcesheet = sourceSS.getSheetByName(srcSheetName);
    //sourcesheet.getRange(start row, start column, numRows, number of Columns)
    sourcerange = sourcesheet.getRange(2,1,sourcesheet.getLastRow()-1,13);
    sourcevalues = sourcerange.getValues();

    //Write all the new data to the end of this data
    destrange = destinationSpreadsheet.getSheetByName("Master")
        .getRange(destsheet.getLastRow()+1,1,sourcevalues.length,sourcevalues[0].length);

    destrange.setValues(sourcevalues);         
  };
};

'''

我遇到问题的地方是

 var pattern = /.Manual./;
      srcSheetName = (pattern) 

我希望它找到名称中包含“Manual”一词的任何文档,以便在 SrcSheetName 中使用。做这个通配符的最佳方法是什么?我尝试使用 ./Manual./ 但这不起作用。

告诉我, 加勒特·基德

【问题讨论】:

    标签: javascript google-apps-script wildcard


    【解决方案1】:

    Get all sheets 并使用 String#includesString#startsWithArray#find 获取正确的工作表:

    sourceSS = SpreadsheetApp.openById(file.getId());
    sourcesheet = sourceSS.getSheets().find(sheet => 
      sheet.getName().includes("Manual"))
    

    【讨论】:

    • 主要/重要的部分是您必须使用spreadsheet.getSheets() 遍历所有工作表,然后使用.getName() 检查它们的名称以找到您想要的。
    猜你喜欢
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多