【发布时间】:2015-06-14 20:35:00
【问题描述】:
我有一个使用 Google App Script HtmlService 和从 html 表单开发的网络应用程序,使用 SpreadsheetApp 在 Google 驱动器中填充 excel 表。另一部分正在调用ContentService以将数据下载为excel文件。
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;
}
此脚本只是在控制台中提供工作表标题详细信息对象,并没有下载任何文件。
<button class="btn btn-success btn-sm" onclick="google.script.run.downloadDoubleQuateCsvFile()">Export</button>
在第二个函数中添加 return 后,我收到这样的错误。
Error: The script completed but the returned value is not a supported return type.
注意:我在驱动器中有包含数据的 excel 文件
【问题讨论】:
-
这只适用于从发布的 url 请求的 doGet() 或 doPost() 返回。
-
@SpencerEaston 从 doGet() 我已经在创建并返回 HtmlService。有没有其他方法可以实现?
-
我在您的示例中没有看到 doGet() 。顺便说一句,在您的示例中,您没有“返回” makeCSV(csvFile)。
-
@SpencerEaston 用 doGet 更新了问题。添加返回后,我收到错误
Error: The script completed but the returned value is not a supported return type. -
我认为您遇到了类型错误,因为您的脚本返回未定义。您的 return 语句取决于 csvFile 的值,但该变量未在任何地方声明,而是包装在 if 语句中,因此如果返回 false,您将返回 undefined。
标签: javascript google-apps-script google-sheets google-apps