1

I have got a apps script that exports a range of my spreadsheet to Google Drive although I would actually like to download this file from the drive.

Does anyone know how I can do this?

I can easily call the pdf file ID as the end of my script is:

var newFile = folder.createFile(theBlob);

  return true;
}

This means I can start the next script:

var fileToExport = newFile.getId();

Does anyone know of a solution for this please?

Gav
  • 328
  • 5
  • 17

1 Answers1

0

Download PDF File created from Google Sheet

I sussed this out using a few different things I found, I use .getDownloadUrl() of theBlob that I created earlier in the script. I then used some script from: https://stackoverflow.com/a/47098533/12139658 which opens up a new url:

  var html = HtmlService.createHtmlOutput('<html><script>'
  +'window.close = function(){window.setTimeout(function(){google.script.host.close()},9)};'
  +'var a = document.createElement("a"); a.href="'+url+'"; a.target="_blank";'
  +'if(document.createEvent){'
  +'  var event=document.createEvent("MouseEvents");'
  +'  if(navigator.userAgent.toLowerCase().indexOf("firefox")>-1){window.document.body.append(a)}'                          
  +'  event.initEvent("click",true,true); a.dispatchEvent(event);'
  +'}else{ a.click() }'
  +'close();'
  +'</script>'
  // Offer URL as clickable link in case above code fails.
  +'<body style="word-break:break-word;font-family:sans-serif;">Failed to open automatically. <a href="'+url+'" target="_blank" onclick="window.close()">Click here to proceed</a>.</body>'
  +'<script>google.script.host.setHeight(40);google.script.host.setWidth(410)</script>'
  +'</html>')
  .setWidth( 90 ).setHeight( 1 );
  SpreadsheetApp.getUi().showModalDialog( html, "Opening ..." );

This worked absolutely brilliant.

Gav
  • 328
  • 5
  • 17