Wol*_*fie 4 pdf web-applications google-sheets google-apps-script google-drive-api
我遇到了一些与 Google Sheets 一起使用的脚本,它们可以让我将单个工作表导出到我的 Google Drive 上的文件中。但是,我不想将其发送到那里,而是希望它直接下载到我的计算机上。
我想换这个...
DriveApp.createFile()
Run Code Online (Sandbox Code Playgroud)
使用其他东西将带有自定义名称的文件作为要在我的浏览器中下载的文件发送。
如果我的理解是正确的,这个示例脚本怎么样?此示例脚本假设以下几点。
当您使用此脚本时,请将此脚本复制并粘贴到脚本编辑器中。脚本是电子表格的容器绑定脚本。运行时downloadSheetAsPDF(),会在电子表格上打开一个对话框。请检查一下。单击该按钮时,将下载 PDF 文件。
function downloadSheetAsPDF() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetId = ss.getActiveSheet().getSheetId();
var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + ss.getId() + "/export?exportFormat=pdf&gid=" + sheetId + "&access_token=" + ScriptApp.getOAuthToken();
var str = '<input type="button" value="Download" onClick="location.href=\'' + url + '\'" >';
var html = HtmlService.createHtmlOutput(str);
SpreadsheetApp.getUi().showModalDialog(html, "sample");
}
Run Code Online (Sandbox Code Playgroud)
var sheetId = ss.getSheetByName("sheetName").getSheetId();.如果这不是您想要的结果,我深表歉意。
如果我的理解是正确的,这个示例脚本怎么样?此示例脚本的流程如下。我认为您的情况可能有几种答案。所以请把这看作是几个答案之一。
function downloadSheetAsPDF2() {
var filename = "sampleFilename.pdf"; // Please set the filename here.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetId = ss.getActiveSheet().getSheetId();
// Creat PDF file as a temporary file and create URL for downloading.
var url = "https://docs.google.com/a/mydomain.org/spreadsheets/d/" + ss.getId() + "/export?exportFormat=pdf&gid=" + sheetId + "&access_token=" + ScriptApp.getOAuthToken();
var blob = UrlFetchApp.fetch(url).getBlob().setName(filename);
var file = DriveApp.createFile(blob);
var dlUrl = "https://drive.google.com/uc?export=download&id=" + file.getId();
// Open a dialog and run Javascript for downloading the file.
var str = '<script>window.location.href="' + dlUrl + '"</script>';
var html = HtmlService.createHtmlOutput(str);
SpreadsheetApp.getUi().showModalDialog(html, "sample");
file.setTrashed(true);
// This is used for closing the dialog.
Utilities.sleep(3000);
var closeHtml = HtmlService.createHtmlOutput("<script>google.script.host.close()</script>");
SpreadsheetApp.getUi().showModalDialog(closeHtml, "sample");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3598 次 |
| 最近记录: |