title: How to loop data and print each iteration to PDF in a defined Google Drive folder
body: <p>I'm a novice at GAS, so please bear with me.</p>
  
  <p>I'd like to create a script for a Google Sheet that will loop through a series of values, pause at each value, and print a specific tab from the sheet to a defined location in Google Drive (creating a new folder with a date within the parent folder). Below is what I have so far, which achieves looping and printing, but I can't figure out how to get it to save the PDF files to a specific folder. Grateful for any help! Thank you.</p>
  
  <p>Link to dummy spreadsheet with script <a href="https://docs.google.com/spreadsheets/d/1F6fgrQpZH0BzwWSbcEpzFLThKbgifzZGYIUHTEYJ4ho/edit#gid=1207870189" rel="nofollow noreferrer">here</a>, and target Google Drive folder <a href="https://drive.google.com/drive/u/1/folders/1QmjXLyssyCGU16ApLQ4Anj_kir0uuQS8" rel="nofollow noreferrer">here</a>.</p>
  
  <pre><code>function loop() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var validation_sheet = ss.getSheetByName('Validation');
    var lastRow = validation_sheet.getLastRow();
    var inputs = ss.getSheetByName('Validation').getRange('A2:A'+lastRow).getValues();
    var sheet2 = ss.getSheetByName('Loadout');
    var tab = ss.getSheetByName('Loadout');
    var formattedDate = Utilities.formatDate(new Date(), "GMT+5", "yyyy-MM-dd hh:mm");
  
    //Create Folder for PDFs
    var fld = DriveApp.createFolder(formattedDate);
    fld.addFile(DriveApp.getFileById(ss.getId()));
  
    for (var i = 0; i &lt; inputs.length; i++) {
      sheet2.getRange('A1').setValue(inputs[i][0]);
      SpreadsheetApp.flush();
      Utilities.sleep(5000);
  
    //Print PDFs
    var name = tab.getRange(1, 1).getValue();
    fld.createFile(ss.getAs('application/pdf')).setName(name);
    }
    DriveApp.getRootFolder().removeFile(DriveApp.getFileById(ss.getId()));
  }
  </code></pre>