我遇到了一些与 Google 表格一起使用的脚本,这些脚本可以让我将单个表格导出到我的 Google Drive 上的文件中。但是,我不想将其发送到那里,而是希望将其直接下载到我的计算机上。
我想换这个...
DriveApp.createFile()
与其他将具有自定义名称的文件作为要在我的浏览器中下载的文件发送的东西。
我遇到了一些与 Google 表格一起使用的脚本,这些脚本可以让我将单个表格导出到我的 Google Drive 上的文件中。但是,我不想将其发送到那里,而是希望将其直接下载到我的计算机上。
我想换这个...
DriveApp.createFile()
与其他将具有自定义名称的文件作为要在我的浏览器中下载的文件发送的东西。
如果我的理解是正确的,那么这个示例脚本怎么样?此示例脚本假定以下几点。
当您使用此脚本时,请将此脚本复制并粘贴到脚本编辑器中。Script 是Spreadsheet 的容器绑定脚本。运行时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");
}
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");
}
或者,您可以使用锚标记下载到具有自定义名称的本地驱动器:
function downloadPdfToDesktop() {
var ss = SpreadsheetApp.getActive(),
id = ss.getId(),
sht = ss.getActiveSheet(),
shtId = sht.getSheetId(),
url =
'https://docs.google.com/spreadsheets/d/' +
id +
'/export' +
'?format=pdf&gid=' +
shtId;
var val = 'PDFNAME';//custom pdf name here
val += '.pdf';
//can't download with a different filename directly from server
//download and remove content-disposition header and serve as a dataURI
//Use anchor tag's download attribute to provide a custom filename
var res = UrlFetchApp.fetch(url, {
headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
});
SpreadsheetApp.getUi().showModelessDialog(
HtmlService.createHtmlOutput(
'<a target ="_blank" download="' +
val +
'" href = "data:application/pdf;base64,' +
Utilities.base64Encode(res.getContent()) +
'">Click here</a> to download, if download did not start automatically' +
'<script> \
var a = document.querySelector("a"); \
a.addEventListener("click",()=>{setTimeout(google.script.host.close,10)}); \
a.click(); \
</script>'
).setHeight(50),
'Downloading PDF..'
);
}