Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google应用程序脚本ContentService下载文件不工作_Javascript_Google Apps Script_Google Sheets_Google Apps - Fatal编程技术网

Javascript Google应用程序脚本ContentService下载文件不工作

Javascript Google应用程序脚本ContentService下载文件不工作,javascript,google-apps-script,google-sheets,google-apps,Javascript,Google Apps Script,Google Sheets,Google Apps,我有一个使用谷歌应用脚本HtmlService开发的web应用程序,从html表单,使用SpreadsheetApp在谷歌硬盘中填充excel表格。另一个部分调用ContentService,将数据下载为excel文件 function doGet(e) { // Read excel sheet //getAppFile(); // Render the application from HTML template return HtmlService.createTempla

我有一个使用谷歌应用脚本
HtmlService
开发的web应用程序,从html表单,使用
SpreadsheetApp
在谷歌硬盘中填充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;
}
注意:我在驱动器中有一个excel文件,其中包含数据

,要获得downloadAsFile()方法才能工作contentservice对象必须从发布的URL调用的doGet()或doPost()返回

例如:

 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()的返回修复:

在您的网页中:

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


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

这只能从发布的url请求的doGet()或doPost()返回。@SpenceEaston从doGet()返回,我已经创建并返回了HtmlService。还有其他方法可以实现吗?我在您的示例中没有看到doGet()。顺便说一句,在你们的例子中,你们并没有“返回”makeCSV(csvFile)。@SpenceEaston用doGet更新了这个问题。添加return后,我收到了错误
error:脚本已完成,但返回的值不是受支持的返回类型。
我认为您收到了类型错误,因为您的脚本返回的是未定义的。return语句取决于csvFile的值,但该变量没有在任何地方声明,而是封装在if语句中,因此如果返回false,则返回undefined。
 function doGet(){
        var output = ContentService.createTextOutput();
        output.setMimeType(ContentService.MimeType.CSV);
        output.setContent(csvString);
        output.downloadAsFile(csvFileName);
        return output;
}
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>