2

Ok, I'm 99% there thanks to a lot of other posts about how to do this.

What am I doing? I have a google form. When that form is completed and the answers are in the respective google sheet I have another tab that has a template (with a bunch of formulas) that auto-populates pay rates for people. The first formula is the name and it is drive off of the Form Responses tab. The following script hides all other sheets, saves the one tab I want to a PDF, in the folder that I want (then unhides the other sheets).

The problem When I run this as a test it works perfectly. The PDF that drops into the folder is named with the output from the cell E5 (which is a formula) pulling the person's name. However when I set the trigger to when a form answer comes in then everything is right (all the data on the sheet is right) just the name of the file is showing as "Yes". I'm so confused as to why it is "Yes". If I run the script right afterwards it is the name of the person from the cell E5..... Here is my code.

Is the trigger to fast to pull the response? I don't think it can be as the output on the PDF is right.


function topdf() {
  var foldersave=DriveApp.getFolderById('1dSOK6bBCRyEO0_fTuuMFH9sNa4wAWYW8');
  var d= new Date();


  var request = {
    "method": "GET",
    "headers":{"Authorization": "Bearer "+ScriptApp.getOAuthToken()},    
    "muteHttpExceptions": true
  };

  var key='1-e8dhOOZ5zwfeIBSTSH7yFNKgxVTxwqZvqcTNtQUQrA'; 
  var fetch='https://docs.google.com/spreadsheets/d/'+key+'/export?format=pdf&size=letter&portrait=false'
  var ss= SpreadsheetApp.getActiveSpreadsheet();
  var sheets=ss.getSheets()
  var name=ss.getRange("E5").getValue()

      sheets[0].hideSheet()
      sheets[1].hideSheet()
      sheets[2].hideSheet()
      sheets[3].hideSheet()
      sheets[4].hideSheet()
      sheets[5].hideSheet()
      sheets[6].hideSheet()

  var pdf = UrlFetchApp.fetch(fetch, request);
  pdf = pdf.getBlob().setName(name);
  var file = foldersave.createFile(pdf)

      sheets[0].showSheet()
      sheets[1].showSheet()
      sheets[2].showSheet()
      sheets[3].showSheet()
      sheets[4].showSheet()
      sheets[5].showSheet()
      sheets[6].showSheet()
}
Matt
  • 23
  • 3

1 Answers1

0

You're calling getRange() E5 from Spreadsheet ss, which may or may not be the sheet you're looking to get the range from. E5 may be yes from an another sheet. Try

ss.getSheetByName('Sheet1').getRange('E5') 
TheMaster
  • 45,448
  • 6
  • 62
  • 85