草庐IT

javascript - Google App Script ContentService downloadAsFile 不工作

coder 2025-02-12 原文

我有一个使用 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 文件

最佳答案

要使 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 调用的有效对象。只允许使用 native javascript 对象。

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

例如,这假设您保留对 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支持自动下载)。

关于javascript - Google App Script ContentService downloadAsFile 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834266/

有关javascript - Google App Script ContentService downloadAsFile 不工作的更多相关文章

随机推荐