pdf - 邮件范围不是整个工作表 - 应用程序脚本
问题描述
我有一个谷歌表格。我想把它作为 PDF 邮寄出去。以下代码可将整张表格(一个标签)以 pdf 格式通过电子邮件发送。&range 不做任何事情。我想要它,但它没有。 问题:如何更改此代码以便通过电子邮件发送“选定的单元格”? 额外问题:为什么我必须输入 ?exportFormat=pdf 和 &format=pdf?
var url_base = ss.getUrl().replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + ssId))
+ '&range=E1:L25' // <<< THIS DOESN'T SEEM TO DO ANYTHING
+ '&format=pdf' //export format
解决方案
实际上它确实有效。找到我用作基础的源代码的原始定义,我看到我将参数设置为通过电子邮件发送整个表格文件而不是一个选项卡。指向一个选项卡后,&range 参数被激活,我得到了想要的结果。
电子邮件工作簿或选项卡参考: Google Script - 将活动工作表作为 PDF 发送到单元格中列出的电子邮件
还有我自己的扩展能力和其他一些,我认为,改进。
/*
shNum = 0 for whole workbook or '0', or 0,1,2,etc for specific tab/sheet
shRng = A1 address for desired range, eg 'E1:L25', ignored if not a single sheet shNum
pdfName = text on top of pdf
*/
function mailPdf(shNum,shRng,pdfName,email,subject,htmlbody) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssId = ss.getId();
var shId = shNum ? ss.getSheets()[shNum].getSheetId() : null;
var url_base = ss.getUrl().replace(/edit$/,'');
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (shId ? ('&gid=' + shId) : ('&id=' + ssId))
+ (shRng ? ('&range=E1:L25') : null)
+ '&format=pdf' //export format
+ '&size=letter' //A3/A4/A5/B4/B5/letter/tabloid/legal/statement/executive/folio
//+ '&portrait=false' //true= Potrait / false= Landscape
//+ '&scale=1' //1= Normal 100% / 2= Fit to width / 3= Fit to height / 4= Fit to Page
//+ '&top_margin=0.00' //All four margins must be set!
//+ '&bottom_margin=0.00' //All four margins must be set!
//+ '&left_margin=0.00' //All four margins must be set!
//+ '&right_margin=0.00' //All four margins must be set!
+ '&gridlines=false' //true/false
//+ '&printnotes=false' //true/false
//+ '&pageorder=2' //1= Down, then over / 2= Over, then down
//+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT
+ '&vertical_alignment=TOP' //TOP/MIDDLE/BOTTOM
//+ '&printtitle=false' //true/false
//+ '&sheetnames=false' //true/false
//+ '&fzr=false' //true/false frozen rows
//+ '&fzc=false' //true/false frozen cols
//+ '&attachment=false' //true/false
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
'muteHttpExceptions': true
}
}
var response = UrlFetchApp.fetch(url_base + url_ext, options);
var blob = response.getBlob().setName(pdfName + '.pdf');
if (email) {
var mailOptions = {
attachments:blob, htmlBody:htmlbody
}
MailApp.sendEmail(
// email + "," + Session.getActiveUser().getEmail() // use this to email self and others
email, // use this to only email users requested
subject+' (' + pdfName +')',
'html content only',
mailOptions);
}
}
推荐阅读
- grafana - 计算值在转换为 1 之前设置为 0 的平均时间
- java - 如何在java中使用条件拆分字符串
- c++ - 我可以在声明后初始化 char 类型变量吗?
- javascript - 使用 Axios 从 Vue 前端上传/下载 pdf 文件到 Spring 后端
- c# - 在某些条件下使用 SQL Server 表加载 datagrid itemsSource
- c# - 使用表达式和反射创建 WPF 绑定
- c# - 我应该在数据库中处理 excel 还是使用 aspose.cells 处理它?
- bash - 期望脚本不提示 sftp
- android - MotionLayout 扩展回收器列表视图 - 如何重置过渡
- c# - 如何将 web 表单与在 asp.net 中的另一台计算机上运行的 sql server 连接