当使用该脚本导出/保存时,如何将单元格的值获取并使用到PDF文件名。我想使用写在单元格中的字符串,我的意思是,将这些信息放入变量中,并自动使用它作为pdf文件名。此时,当我执行脚本时,会打开一个窗口,在“文件名”字段中有这样的内容:"Spreedsheet name“- "sheet name".pdf。我必须重写我在牢房里已经有的名字。
function ExportPDF() {
SpreadsheetApp.flush();
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getSheetId();
var pagina = 'https://docs.google.com/spreadsheets/d/' + spreadsheet + '/export?exportFormat=pdf&format=pdf&gid=' + sheet + '&portrait=false&scale=4';
var html = "<script>window.open('" + pagina + "');google.script.host.close();</script>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Tab');
};
发布于 2019-05-23 12:04:17
我使用了下面位于https://stackoverflow.com/q/56215898/7108653中的脚本,就像@Tanaike所建议的那样,并且只做了一些调整(第3、4和5行)。
function downloadSheetAsPDF2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetId = ss.getSheetByName("RECEITA PARA IMPRESSÃO").getSheetId();
var valor = SpreadsheetApp.getActiveSheet().getRange('M3').getValue();
var filename = valor;
// 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 + "&portrait=false&scale=4&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");
}
https://webapps.stackexchange.com/questions/130025
复制相似问题