从谷歌应用脚​​本启动下载 [英] Initiate a download from google apps script

查看:17
本文介绍了从谷歌应用脚​​本启动下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Google Apps 脚本向我的电子表格添加了一个新菜单项.此菜单项创建一个文件,但我希望它在创建文件后启动文件的下载.

I added a new menu item to my spreadsheet using google apps script. This menu item creates a file, but I'd like for it to initiate the download of the file after creating it.

这可能吗?

请记住,这不是网络应用,而是我的电子表格中的一个菜单项.

Remember, this is not a web app, but a menu item in my spreadsheet.

谢谢

感谢 Serge insas 的建议,以下简单脚本完美运行,并打开一个下载窗口,其中包含我需要的链接:

Thanks to Serge insas' suggestion, the following simple script works perfectly, and opens a download window with the link I need:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var csvMenuEntries = [ {name: "Zip all CSVs", functionName: "saveAsCSV"} ];
  ss.addMenu("CSV", csvMenuEntries);
};

function saveAsCSV() {
  var folder = createCSVs(); // creates a folder with CSV for each Sheet
  var zipFile = zipCSVs(folder, "DI.zip"); // creates a zip of all CSVs in folder

  var ui = UiApp.createApplication().setTitle("Download");
  var p = ui.createVerticalPanel();
  ui.add(p);
  p.add(ui.createAnchor("Download", zipFile.getDownloadUrl()));
  SpreadsheetApp.getActive().show(ui)
}

推荐答案

EDIT : 阅读下面的评论,Zig Mandel 指出的局限性是完全正确的复杂"的版本,它真的是一个简单(有趣)的练习来展示其他方法.

EDIT : read the comments below, Zig Mandel is perfectly right when he points out the limitations of the "complicated" version, it was really a simple (and fun) exercice to show other methods.

我认为您必须使用中间 Ui 作为弹出窗口来确认下载.之后我知道有两种可能的方法,一种很简单,另一种很麻烦,你自己选择吧,下面的代码显示了这两种方法.

I think you'll have to use an intermediate Ui as a popup to confirm the download. After that there are 2 possible ways that I know, one is very simple and the other is quite cumbersome, make your choice, the code below shows both of them.

注意:要使用复杂的应用程序,您需要部署应用程序(即保存一个版本并部署为 webapp),对于简单的应用程序,只需按原样"使用它即可.(我在代码注释中显示了简单).

note : to use the complicated one you need to deploy your app (ie save a version and deploy as webapp), for the simple one just use it "as it is". (I show the simple in the code comments).

代码:

function onOpen() {
  var menuEntries = [ {name: "test download", functionName: "downloadFile"}
                     ];
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  sheet.addMenu("Utils",menuEntries);
}

function downloadFile(){
  var file = DriveApp.createFile('test file', 'Some content in this file to test it');
  var fileID = file.getId();
  var fileName = file.getName();
  var ui = UiApp.createApplication().setTitle('Download');
  var url = ScriptApp.getService().getUrl()+'?&ID='+fileID+'&name='+fileName;
  var p = ui.createVerticalPanel();
  ui.add(p);
  p.add(ui.createAnchor('click to download', url));
  p.add(ui.createAnchor('or use this link ',file.getDownloadUrl()));// this is the simple one, just get the file you created and use getDownloadUrl()
  SpreadsheetApp.getActive().show(ui)
}

function doGet(e){
  var fileId = e.parameter.ID;
  var fileName = e.parameter.name;
  var fileString = DocsList.getFileById(fileId).getContentAsString();
  return ContentService.createTextOutput(fileString).downloadAsFile(fileName);
}

PS:我写这篇文章很有趣,复杂的版本"真的很有趣恕我直言:-)

PS : I had some fun writing this, the "complicated version" is really funny imho :-)

这篇关于从谷歌应用脚​​本启动下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆