简体   繁体   中英

Google Apps Script to Iterate through a list of Spreadsheet ID's and copy range to a master file

I have a folder in my drive where I create a folder (based on a template structure) for each new client. I need to extract information from one file in particular from each of their folders. I have the Spreadsheet IDs in a list, and I know the sheet name, I am just not sure how to create a loop to go through each file and append it to a master file.

I found a script recently here which would do the trick, but it assumes that all the spreadsheets are in one folder:

function getDataToMaster2() {
    var comp = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("System_Control").getRange("d7").getValue();
  var folder = DriveApp.getFolderById(comp); //Define id of folder
  var contents = folder.getFiles();
  var file; 
  var data;
  var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form_Responses"); //first sheet of the file, change by getSheetByName("NAME") if you want
  while(contents.hasNext()){  
    file = contents.next();
    if (file.getMimeType() == "application/vnd.google-apps.spreadsheet") {
      var sheet = SpreadsheetApp.openById(file.getId()).getSheetByName("Form_Responses");//first sheet of the file, change by getSheetByName("NAME") if you want
      var startRow = 2; 
      var data = sheet.getDataRange().getValues();
      var colToCheck = 14;
      for(var j = 1; j < data.length;j++){
        if(data[j][colToCheck-1] != "copied"){
          sheetMaster.appendRow(data[j]);
          sheet.getRange((j+1), colToCheck).setValue("copied");
          SpreadsheetApp.flush();
        }
      }
    } 
  }
}

Here is a function that loops over idList, an array holding the Id of each spreadsheet to be copied. It grabs all data from "Sheet1" in each of those, and copies to sheet "Master" in the active spreadsheet.

The data copying happens at once, without looping over rows as in your example; it is more efficient to move larger chunks at once.

function getDataToMaster2() {
  var idList = ['id1', 'id2', 'id3'];
  var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master");
  for (var i = 0; i < idList.length; i++) {  
    var sheet = SpreadsheetApp.openById(idList[i]).getSheetByName("Sheet1"); 
    var data = sheet.getDataRange().getValues();
    var lastRow = sheetMaster.getLastRow();
    sheetMaster.insertRowsAfter(lastRow, data.length);
    sheetMaster.getRange(lastRow + 1, 1, data.length, data[0].length).setValues(data);
  }
}

If the ids are stored, say, in column B of sheet "Ids", then the beginning of function would be different:

  var sheetIds = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Ids");
  var values = sheetIds.getRange('B:B').getValues();
  var idList = values.map(function (row) {
    return row[0];
  }).filter(function (id) {
    return id;
  });

and then proceed for the for loop as above.

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-2024 STACKOOM.COM