从谷歌应用脚​​本启动下载

tbk*_*n23 4 google-apps-script google-drive-api

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

这可能吗?

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

谢谢

编辑:

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

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)
}
Run Code Online (Sandbox Code Playgroud)

Dr-*_*ket 11

OP 的答案已被弃用(2021 年),因此我基于它制作了一个更通用的答案。


Code.gs

// Runs when the spreadsheet starts, adds a tab at the top
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Script Menu')
    .addItem('Download a file!', 'dlFile')
    .addToUi();
}

// Run when you click "Download a file!"
function dlFile() {
  let file = DriveApp.getRootFolder().createFile('Hi.txt', 'Hello, world!');

  // Create little HTML popup with the URL of the download
  let htmlTemplate = HtmlService.createTemplateFromFile('Download.html');
  htmlTemplate.dataFromServerTemplate = { url: file.getDownloadUrl() };

  let html = htmlTemplate
    .evaluate()
    .setWidth(400)
    .setHeight(300);

  SpreadsheetApp.getUi()
    .showModalDialog(html, 'Download');
};


Run Code Online (Sandbox Code Playgroud)

Download.html

<!DOCTYPE html>
<html>
  <head>
    <script>
      let data = <?!= JSON.stringify(dataFromServerTemplate) ?>; // Stores the data directly in the javascript code

      function downloadFile() {
        document.getElementById("dlBtn").innerText = "Downloading..";

        window.open(data.url, '_blank');

        document.getElementById("dlBtn").disabled = true;
      }
    </script>
  </head>
  <body>
    <button id="dlBtn" onclick="downloadFile()">Download</button>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)


Ser*_*sas 8

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


我想你必须使用中间的Ui作为弹出窗口来确认下载.之后有2点可能的方法,我知道,一个很简单,另一个是相当麻烦,让您的选择,下面的代码显示了他们两个.

注意:要使用你需要部署应用程序的复杂的一个(即保存版本和部署Web应用程序作为),对于简单的只是用它"因为它是".(我在代码注释中显示简单).

代码 :

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);
}
Run Code Online (Sandbox Code Playgroud)

PS:我写这篇文章很有意思,"复杂的版本"真的很有趣imho :-)