如何使用谷歌Apps脚本下载创建csv文件?(How to download created csv

2019-07-17 13:41发布

我创建了一个谷歌Apps脚本文件如下

DocsList.createFile(
   "test.csv", 
   "Row1Col1,Row1Col2 \r\n Row2Col1,RowCol2 \r\n Row3Col1,Row3Col2");

如何以编程方式下载(例如经由弹出下载对话框)?

Answer 1:

Try using ContentService, you can trigger a download and even set the file name via the TextOutput.downloadAsFile method. You can also set the content mime type of the returned data, though only to a predefined enum constant. See their examples.



Answer 2:

如果你想显示在客户端的下载对话框,然后下面的代码工作。 这是一个CSV文件的示例。

return ContentService.createTextOutput("bbb,aaa,ccc").downloadAsFile("MyData.csv")
    .setMimeType(ContentService.MimeType.CSV);


Answer 3:

要下载Apps脚本,你需要可以发布应用程序文件,并使用doGet()方法(否则“downloadFileAs”将无法正常工作)

要么...

创建驱动器的文件,并提供一个对话框界面的链接。 这里是连接两个选项,一个用户需要在其他中记录不从这里取

  var dat=getFileDataFromAnotherFunction();
  var file = DriveApp.createFile('name.csv',dat);
  var t = HtmlService.createTemplateFromFile('DownloadDialog');
  t.url1 = getDlUrl(file);
  t.url2 = file.getDownloadUrl().replace('&gd=true','') 
  rt =  t.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  uia.showModalDialog(rt,'Download the csv')

其中“DownloadDialog.html”

是以下文件(另在脚本编辑器)

<div>
<a target="_blank" href="<?= url1 ?>">  l1 </a>
<br>
<hr>
<a target="_blank" href="<?= url2 ?>"> link 2 </a> 
</div>


文章来源: How to download created csv file using Google Apps Script?