To get the downloadAsFile () method to work, the contentervice object must be returned from doGet () or doPost () from the published URL.
Example:
function doGet(){ var output = ContentService.createTextOutput(); output.setMimeType(ContentService.MimeType.CSV); output.setContent(csvString); output.downloadAsFile(csvFileName); return output; }
In the code, you return the ContentService object to the web page via google.script.run. It will not request download from the browser. In fact, returning the contentervice object will result in an error because it is not a valid object to return to the google.script.run call. Only native javascript objects are allowed.
If you want it to work, you will need to provide users with a link that will point to your script in another tab. Or you can use the upload attribute on an anchor tag pointing to your script.
For example, and this assumes that you keep the return fix for downloading DoubleQuateCsvFile ():
function doGet(e){ var serveCSV = e.parameter.servecsv; if(serveCSV){return downloadDoubleQuateCsvFile()} return HtmlService.createTemplateFromFile('index').evaluate() .setTitle('Go Smart') .setSandboxMode(HtmlService.SandboxMode.IFRAME); }
On the web page:
<a href="//script.google.com/WebAppURL/exec?servecsv=true" target="_blank">Click here to download</a>
Remember that this is not supported in all browsers. (Think only chrome, opera, firefox supports autodownload).
source share