从谷歌应用程序脚本启动下载 [英] Initiate a download from google apps script
问题描述
我使用Google应用程序脚本将新的菜单项添加到我的电子表格中。这个菜单项创建一个文件,但是我想让它在创建文件后下载文件。
这可能吗?
请记住,这不是一个网络应用程序,而是我的电子表格中的一个菜单项。
谢谢
编辑:
感谢Serge insas的建议,下面的简单脚本完美地工作,并打开一个下载窗口与我需要的链接:
pre $ function onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var csvMenuEntries = [{name:Zip all CSVs,functionName:saveAsCSV}];
ss.addMenu(CSV,csvMenuEntries);
};
function saveAsCSV(){
var folder = createCSVs(); //为每个Sheet创建一个带有CSV的文件夹
var zipFile = zipCSVs(文件夹,DI.zip); //创建文件夹
var ui = UiApp.createApplication()。setTitle(Download);
var p = ui.createVerticalPanel();
ui.add(p);
p.add(ui.createAnchor(Download,zipFile.getDownloadUrl()));
SpreadsheetApp.getActive()。show(ui)
}
我认为你必须使用中间Ui弹出确认下载。
之后有两种可能的方式我知道,一种非常简单,另一种非常麻烦,请选择,下面的代码显示了这两种方法。
注意:为了使用复杂的应用程序,您需要部署应用程序(即保存版本并将其部署为web应用程序),因为简单的应用程序按原样使用它。 (我在代码注释中展示了简单代码)。
代码:
function onOpen(){
var menuEntries = [{name:test download,functionName:downloadFile}
];
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.addMenu(Utils,menuEntries);
函数downloadFile(){
var file = DriveApp.createFile('test file','这个文件中的一些内容来测试它');
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('或使用此链接',file.getDownloadUrl())); //这是简单的,只需获取您创建的文件并使用getDownloadUrl()
SpreadsheetApp.getActive()。show(ui)
}
函数doGet(e){
var fileId = e.parameter.ID;
var fileName = e.parameter.name;
var fileString = DocsList.getFileById(fileId).getContentAsString();
返回ContentService.createTextOutput(fileString).downloadAsFile(fileName);
$ / code>
PS:写这个我有点乐趣,复杂版本真的搞笑imho: - )
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.
Is this possible?
Remember, this is not a web app, but a menu item in my spreadsheet.
Thanks
Edit:
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 : 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.
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.
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).
The code :
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 : I had some fun writing this, the "complicated version" is really funny imho :-)
这篇关于从谷歌应用程序脚本启动下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!