I have a script that iterates through a folder, opens all spreadsheets, and copies a certain array/range of these sheets to an archive sheet. My script functions just fine [on Folders containing files], but I can't figure out how to get my script to iterate through sub-folders as well.
I know that I'm going to have time-out issues eventually. But I'm going to look at setting up a token to have it continue where it left off once I can get it to iterate through the whole folder structure. (So not a problem for now.)
An example of my folder structure is as follows:
- Master Folder
- ---Teacher Name (Folder)
- -----Reading Assessment Data (Folder)
- -------Spreadsheet
- -------Spreadsheet
- -------Spreadsheet
- -----Math Assessment Data (Folder)
- -------Spreadsheet
- -------Spreadsheet
- -------Spreadsheet
- ---Teacher Name (Folder)
- -----This Continues...
Any help you guys can give me would be greatly appreciated! Thanks, Brandon
function runDataReport () {
var folder = DriveApp.getFolderById("ID");
Logger.log('folder: ' + folder);
var sheetFiles = folder.getFilesByType("application/vnd.google-apps.spreadsheet");
Logger.log("sheetFiles.hasNext(): " + sheetFiles.hasNext());
var data;
while (sheetFiles.hasNext()) {
var file = sheetFiles.next();
var theFileType = file.getMimeType();
Logger.log("theFileType: " + theFileType);
var ssID = file.getId();
Logger.log('ssID: ' + ssID);
var thisSS = SpreadsheetApp.openById(ssID),
ssName = thisSS.getName(),
ssUrl = thisSS.getUrl(),
classData = thisSS.getSheets()[0],
dataLastRow = classData.getLastRow(),
Avals = classData.getRange("A1:A").getValues(),
Alast = Avals.filter(String).length,
Jvals = classData.getRange("J2:J").getValues(),
Jlast = Jvals.filter(String).length,
classDataRange = classData.getRange(3, 1, Alast-2, 50),
dataArray = classDataRange.getValues();
var dataReportSS = SpreadsheetApp.openById('ID'), //Data Report Sheet ID
dataReportSheet = dataReportSS.getSheets()[0],
dataReportNewRow = dataReportSheet.getLastRow() + 1,
newDataRange = dataReportSheet.getRange(dataReportNewRow, 3, Alast-2, 50);
if (Jlast > 1){
newDataRange.setValues(dataArray);
for (var i=0, x = dataArray.length; i<x; i++){
dataReportSheet.getRange(dataReportNewRow + i, 1).setValue(ssName);
}
for (var i=0, x = dataArray.length; i<x; i++){
dataReportSheet.getRange(dataReportNewRow + i, 2).setValue(ssUrl);
}
}
}
}