Combine Multiple Tabs From Multiple Sheets

239 views
Skip to first unread message

ZWaring

unread,
Sep 15, 2022, 10:16:27 AM9/15/22
to Google Apps Script Community
Hi all,

I was hoping someone could help me tweak this script please that I've found from others.
The aim is to combine data from multiple tabs in multiple workbooks into one new workbook in Google Sheets.

This currently works but only takes the first tab from each workbook, I would like to take all tabs.

function combineRevitSchedules() {

 var folder = DriveApp.getFolderById("changedforsecurity");

var filesIterator = folder.getFiles();

var file;
var fileType;
var ssID;
var combinedData = [];
var data;

while(filesIterator.hasNext()){
  file = filesIterator.next();
  fileType = file.getMimeType();
  if(fileType === "application/vnd.google-apps.spreadsheet"){
    ssID = file.getId();
    data = getDataFromSpreadsheets(ssID);
    data = data.map(function(r) { return r.concat([file.getName()]);  });
    combinedData = combinedData.concat(data);
  } //if ends here

} //while ends here

var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Combined");
ws.getRange("A2:G").clearContent();
ws.getRange(2,1, combinedData.length, combinedData[0].length).setValues(combinedData);
}

function getDataFromSpreadsheets(ssID){

var ss = SpreadsheetApp.openById(ssID);
 var ws =  ss.getSheets()[0]; //this line currently returns only one tab
 var data = ws.getRange("B3:E" + ws.getLastRow()).getValues();
 return(data);

}

I can't quite work out how to change the getSheets to return more than one sheet without an error.
Any help would be appreciated, I'm still learning my way around.
Thank you!

Andrew Roberts

unread,
Sep 16, 2022, 5:03:03 AM9/16/22
to google-apps-sc...@googlegroups.com
ss.getSheets() returns an array, use ss.getSheets().forEach(sheet => { // get data for that tab })

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/fc77adf6-7614-410a-9dd3-3352c10054bdn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages