I have 100 Google Sheet files in one folder of Google Drive. Each Google sheet file has 10 sheets (A,B,C,D,E,F,G,H,I,J). I wanted to append all 100 Google Sheet files into one Google Sheet appending data from "B" sheets of all the 100 files. But the sheets with name "B" will not be in 2nd place always. All the sheets has same columns. I have the below code which appends the sheets from multiple files only if those sheets are in 2nd place in the order in each sheet. 1) It has to pick the sheet name instead of the 2nd sheet 2) This code is including even the blank rows if any in the sheets. it has to skip the blank rows from the sheets.
function myFunction() {
var myFolder = DriveApp.getFolderById("1ZtxfMNDn3uFhCcdcp5unfmTwUIJ_3fZK");
var spreadSheets = myFolder.getFilesByType("application/vnd.google-apps.spreadsheet");
var master_files = DriveApp.getFilesByName("MergedNew")
if (master_files.hasNext()){
var master_file=master_files.next();
var newSpreadSheet = SpreadsheetApp.openById(master_file.getId());
}
else {
var newSpreadSheet = SpreadsheetApp.create("MergedNew");
newSpreadSheet.getSheets()[0].setName('B data');
}
var bSheet = newSpreadSheet.getSheetByName('B data');
while(spreadSheets.hasNext())
{
var sheet = spreadSheets.next();
var spreadSheet = SpreadsheetApp.openById(sheet.getId());
var sh = spreadSheet.getSheets()[1];
var data = sh.getRange(2,1,sh.getMaxRows(),sh.getMaxColumns()).getValues();
if (bSheet.getLastRow() == 0){
var headers = sh.getRange(1,1,1,sh.getMaxColumns()).getValues();
bSheet.getRange(1,1,1,sh.getMaxColumns()).setValues(headers);
}
bSheet.getRange(bSheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
}