如何将单页下载为PDF(不导出到Google云端硬盘) [英] How to download single sheet as PDF (not export to Google Drive)
问题描述
我遇到了一些用于Google表格的脚本,这些脚本可以让我将单个表格导出到Google云端硬盘中的文件中.但是,我希望将其直接下载到我的计算机上,而不是将其发送到那里.
I've come across a few scripts to use with Google Sheets that will let me export a single sheet to a file on my Google Drive. However, instead of sending it there, I want it to download to my computer directly.
我正在寻找替换它...
I'm looking to replace this...
DriveApp.createFile()
带有其他名称的文件将以自定义名称发送该文件,作为要在我的浏览器中下载的文件.
with something else that will send the file, with a customized name, as a file to download in my browser.
推荐答案
- 您要以PDF文件的形式下载活动电子表格中的特定工作表.
- 脚本是电子表格的容器绑定脚本.
- 您要下载的表格位于活动的电子表格中.
- 运行脚本时,将打开一个对话框.单击该按钮后,活动工作表将作为PDF文件下载到本地PC.
- 在此脚本中,PDF文件是通过Javascript下载的.因此,我使用了一个对话框来执行Javascript.
- Script is the container-bound script of Spreadsheet.
- Sheet you want to download is in the active Spreadsheet.
- When the script is run, a dialog is opened. When the button is clicked, the active sheet is downloaded as a PDF file to the local PC.
- In this script, the PDF file is downloaded by Javascript. So I used a dialog to execute Javascript.
如果我的理解是正确的,那么该示例脚本如何?此示例脚本假设以下几点.
If my understanding is correct, how about this sample script? This sample script supposes the following points.
示例脚本:
使用此脚本时,请将该脚本复制并粘贴到脚本编辑器中.脚本是电子表格的容器绑定脚本.运行downloadSheetAsPDF()
时,将在电子表格上打开一个对话框.请检查一下.当您单击按钮时,将下载PDF文件.
Sample script:
When you use this script, please copy and paste this script to the script editor. Script is the container-bound script of Spreadsheet. When you run downloadSheetAsPDF()
, a dialog is opened on the Spreadsheet. Please check it. When you click the button, the PDF file is downloaded.
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();
. - This is a simple sample script. So please modify this for your situation.
- If you want to download the specific sheet name, please modify to
var sheetId = ss.getSheetByName("sheetName").getSheetId();
. - Class HtmlService
- Class Ui
- 下载文件时,您要使用PDF文件的特定文件名.
- 您希望在脚本运行时自动下载.
- 将PDF文件创建为临时文件.
- 创建要下载的URL.
- 打开一个对话框,并通过运行Javascript自动下载PDF文件.
- 删除临时文件.
- 关闭对话框.
Note:
如果这不是您想要的结果,我表示歉意.
If this was not the result you want, I apologize.
如果我的理解是正确的,那么该示例脚本如何?该示例脚本的流程如下.我认为您的情况可能有几个答案.因此,请仅将此作为几个答案之一.
If my understanding is correct, how about this sample script? The flow of this sample script is as follows. I think that there might be several answers for your situation. So please think of this as just one of several answers.
示例脚本:
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");
}
这篇关于如何将单页下载为PDF(不导出到Google云端硬盘)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!