Iterate through folders and sub-folders/files and

2019-05-10 01:57发布

问题:

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

回答1:

Here is some code that I hope works for you. I didn't test it, so let me know if there are any problems.

function runDataReport () {
  var allFoldersInThisDrive = DriveApp.getFolders();
  var foldersToExclude = ["folder1","folder2","folder3"];

  while (allFoldersInThisDrive.hasNext()) {
    var folder = allFoldersInThisDrive.next();
    //Logger.log(allFoldersInThisDrive.getName());

    var thisFolderName = folder.getName();

    //If index is -1, then thisFolderName was not found in the folders to exclude
    if (foldersToExclude.indexOf(thisFolderName)===-1) {
      //branch to another function in order to interate through all the files in this folder
      processAllFiles(folder)
    };
  };
}

function processAllFiles(argFolder) {

  var sheetFiles = argFolder.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('The ID here'), //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);
      }
    }      
  }
};