问题描述
我有一个使用Google App Script HtmlService
开发的web应用程序,并且使用 SpreadsheetApp填充html表单中的Excel表单, code>。另一部分调用
ContentService
以excel文件的形式下载数据。
function doGet(e){
//读取表单
// getAppFile();
//从HTML模板
渲染应用程序返回HtmlService.createTemplateFromFile('index')。evaluate()
.setTitle('Go Smart')
.setSandboxMode(HtmlService。 SandboxMode.IFRAME);
}
函数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 ++){
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\\\
;
} else {
csv + = data [row];
}
}
csvFile = csv;
}
return makeCSV(csvFile);
}
函数makeCSV(csvString){
var csvFileName ='test.csv';
var output = ContentService.createTextOutput();
output.setMimeType(ContentService.MimeType.CSV);
output.setContent(csvString);
output.downloadAsFile(csvFileName);
返回输出;
$ / code>
这个脚本只是给控制台中的表头细节对象,而不是下载任何文件。
< button class =btn btn-success btn-smonclick =google.script.run .downloadDoubleQuateCsvFile()>出口与LT; /按钮>
在第二个函数中添加return之后,我收到了这样的错误。
错误:脚本已完成,但返回的值不是受支持的返回类型。
注意:我的驱动器中有excel文件
例子:
函数doGet(){
var output = ContentService.createTextOutput();
output.setMimeType(ContentService.MimeType.CSV);
output.setContent(csvString);
output.downloadAsFile(csvFileName);
返回输出;
$ b $ p
$ b 在你的代码中,你通过google.script将ContentService对象返回给一个网页。跑。它不会要求从浏览器下载。实际上,返回contentservice对象将导致错误,因为它不是返回到google.script.run调用的有效对象。只允许原生JavaScript对象。
如果你想让它工作,你需要向用户提供一个点击链接,指向另一个标签中的脚本。或者,您可以在指向脚本的定位标记上使用下载属性。例如,假设您将返回修复程序保留为downloadDoubleQuateCsvFile():
code> function doGet(e){
var serveCSV = e.parameter.servecsv;
if(serveCSV){return downloadDoubleQuateCsvFile()}
return HtmlService.createTemplateFromFile('index')。evaluate()
.setTitle('Go Smart')
.setSandboxMode HtmlService.SandboxMode.IFRAME);
$ b $ / code>
在您的网页中:
< a href =// script.google.com/WebAppURL/exec?servecsv=truetarget =_ blank>点击此处下载< / A>
请记住,这在所有浏览器中都不受支持。 (想想只有Chrome,opera,firefox支持自动下载)。
I have a web app developed using Google App Script HtmlService
and from the html form, populating excel sheet in the Google drive using SpreadsheetApp
. And one another section is calling ContentService
to download data as 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 is just giving the sheet header details object in console and it is not downloading any file.
<button class="btn btn-success btn-sm" onclick="google.script.run.downloadDoubleQuateCsvFile()">Export</button>
After adding return in the second function, I am getting error like this.
Error: The script completed but the returned value is not a supported return type.
Note: I have the excel file in drive with data
解决方案 To get downloadAsFile() method to work the contentservice object has to be returned from a doGet() or a doPost() called 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 your code you are returning the ContentService object to a webpage via google.script.run. It will not request a download from the browser. In fact returning a contentservice object will result in an error as it is not a valid object to return to a google.script.run call. Only native javascript objects are allowed.
If you want it to work you would need to present the users with a link to click that would point to your script in another tab. Or you can use the 'download' attribute on an anchor tag pointing to your script.
For example, and this assumes you keep the return fix to 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 your webpage:
<a href="//script.google.com/WebAppURL/exec?servecsv=true" target="_blank">Click here to download</a>
Remeber that this is not supported in all browsers. (Think only chrome,opera,firefox supports autodownload).
这篇关于Google App脚本ContentService downloadAsFile无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!