简体   繁体   中英

Wildcard expression when searching for sheet name in google sheets. Google App Script

Having an issue attempting to search for certain google sheets in a folder that are imported. Each google sheet that is imported, has a regular name then a unique ID. EX: Manual Export(1024508324)

I created a script that takes all the google sheets inside a folder to be imported into one master google sheet. Below: '''

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);         
  };
};

'''

The area where im having trouble is

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

I want it to find any document with the word "Manual" inside the name to be used in SrcSheetName. What is the best way to do this wildcard? I tried using ./Manual./ but that did not work.

Let me know, Garrett Kidd

Get all sheets and use String#includes or String#startsWith and Array#find to get the correct sheet:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM