【问题标题】:Google App Script ContentService downloadAsFile not workingGoogle App Script ContentService downloadAsFile 不工作
【发布时间】: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


【解决方案1】:

要让 downloadAsFile() 方法工作,必须从已发布的 URL 调用的 doGet() 或 doPost() 返回 contentservice 对象。

例子:

 function doGet(){
        var output = ContentService.createTextOutput();
        output.setMimeType(ContentService.MimeType.CSV);
        output.setContent(csvString);
        output.downloadAsFile(csvFileName);
        return output;
}

在您的代码中,您通过 google.script.run 将 ContentService 对象返回到网页。它不会从浏览器请求下载。事实上,返回 contentservice 对象会导致错误,因为它不是返回 google.script.run 调用的有效对象。只允许使用本机 javascript 对象。

如果您希望它工作,您需要向用户显示一个链接,点击该链接将指向另一个选项卡中的脚本。或者,您可以在指向您的脚本的锚标记上使用“下载”属性。

例如,假设您将返回修复程序保留为 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);

}

在您的网页中:

<a href="//script.google.com/WebAppURL/exec?servecsv=true" target="_blank">Click here to download</a>

请记住,并非所有浏览器都支持此功能。 (认为​​只有chrome、opera、firefox支持自动下载)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多