Google Script ContentService downloadAsFile not working

I have a web application developed using the Google Script HtmlService and from an html form filling out an Excel sheet in a Google driver using SpreadsheetApp . And another section calls the ContentService to load the data as an excel file.

 function doGet(e) { // Read excel sheet //getAppFile(); // Render the application from HTML template return HtmlService.createTemplateFromFile('index').evaluate() .setTitle('Go Smart') .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function downloadDoubleQuateCsvFile() { var sheetId = PropertiesService.getScriptProperties().getProperty('sheetId'); var ss = SpreadsheetApp.openById(sheetId).getActiveSheet(); //var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var maxColumn = ss.getLastColumn(); var maxRow = ss.getLastRow(); var data = ss.getRange(1, 1, maxRow, maxColumn).getValues(); if (data.length > 1) { var csv = ""; for (var row = 0; row < data.length; row++) { for (var col = 0; col < data[row].length; col++) { if (data[row][col].toString().indexOf(",") != - 1) { data[row][col] = "\"" + data[row][col] + "\""; } } if (row < data.length - 1) { csv += data[row].join(",") + "\r\n"; } else { csv += data[row]; } } csvFile = csv; } return makeCSV(csvFile); } function makeCSV(csvString) { var csvFileName = 'test.csv'; var output = ContentService.createTextOutput(); output.setMimeType(ContentService.MimeType.CSV); output.setContent(csvString); output.downloadAsFile(csvFileName); return output; } 

This Script simply provides a sheet header detail object in the console and does not load the file.

 <button class="btn btn-success btn-sm" onclick="google.script.run.downloadDoubleQuateCsvFile()">Export</button> 

After adding return to the second function, I get an error like this.

 Error: The script completed but the returned value is not a supported return type. 

Note. I have an excel file on a data disc

+5
source share
1 answer

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).

+10
source

All Articles