javascript - Google App Script ContentService downloadAsFile not working -
i have web app developed using google app script htmlservice
, html form, populating excel sheet in google drive using spreadsheetapp
. , 1 section calling contentservice
download data excel file.
function doget(e) { // read excel sheet //getappfile(); // render application 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 = ""; (var row = 0; row < data.length; row++) { (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 giving sheet header details object in console , not downloading file.
<button class="btn btn-success btn-sm" onclick="google.script.run.downloaddoublequatecsvfile()">export</button>
after adding return in second function, getting error this.
error: script completed returned value not supported return type.
note: have excel file in drive data
to downloadasfile() method work contentservice object has returned doget() or dopost() called published url.
example:
function doget(){ var output = contentservice.createtextoutput(); output.setmimetype(contentservice.mimetype.csv); output.setcontent(csvstring); output.downloadasfile(csvfilename); return output; }
in code returning contentservice object webpage via google.script.run. not request download browser. in fact returning contentservice object result in error not valid object return google.script.run call. native javascript objects allowed.
if want work need present users link click point script in tab. or can use 'download' attribute on anchor tag pointing script.
for example, , assumes keep return fix downloaddoublequatecsvfile():
function doget(e){ var servecsv = e.parameter.servecsv; if(servecsv){return downloaddoublequatecsvfile()} return htmlservice.createtemplatefromfile('index').evaluate() .settitle('go smart') .setsandboxmode(htmlservice.sandboxmode.iframe); }
in webpage:
<a href="//script.google.com/webappurl/exec?servecsv=true" target="_blank">click here download</a>
remeber not supported in browsers. (think chrome,opera,firefox supports autodownload).
Comments
Post a Comment