将电子表格的表格作为单独的 PDF 通过电子邮件发送
我无法通过创建 blob 并将它们放入数组来做到这一点。所以我为每个页面创建了单独的 PDF 文件,然后在最后将它们丢弃。我还添加了排除数组,以便您可以从整个过程中排除某些文件,并且只发送您希望的工作表。
当前版本包括在程序创建文件和发送电子邮件时跟踪程序进度的对话框提示。所以它和你的程序不完全一样。
这些行也是你的程序对我来说很难理解,因为你有一个 2 单元格数组,但你使用 getValue() 而不是 getValues();
var name = ss.getRange("Timesheet!J6:K6").getValue();
var agency = ss.getRange("Timesheet!B4:C4").getValue();
这是我的代码:
function savePDFFiles1() {
var ss=SpreadsheetApp.getActive();
var exclA=['Summary','Images','Globals','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21'];
var fA=[];
var html="";
var shts=ss.getSheets();
var pdfFldr=DriveApp.getFolderById('FolderId');//folder where I stored the files temporarily
for(var i=0;i<shts.length;i++) {
var sh=shts[i];
var name=sh.getName();
if(exclA.indexOf(name)==-1) {
sh.showSheet();
for(var j=0;j<shts.length;j++) {
if(shts[j].getName()!=name) {
shts[j].hideSheet();
}
}
SpreadsheetApp.flush();//I dont know if this is required
var file=pdfFldr.createFile(ss.getBlob().getAs('application/pdf').setName(Utilities.formatString('%s_%s.pdf',ss.getName(),name)));
html+=Utilities.formatString('<br />File: %s Created',file.getName());
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created')
fA.push(file);
}
}
GmailApp.sendEmail('recipient email', 'Plot Reports', 'Plot Reports Attached', {attachments:fA})
html+='<br />Email Sent';
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created');
for(var i=0;i<fA.length;i++ ) {
fA[i].setTrashed(true);
}
html+='<br />Files Trashed and Process Complete';
html+='<script>window.onload=function(){google.script.host.close();}</script>';
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Files Created');
}
在我的示例中,我使用的工作表包含一些以数字命名的工作表,并且我通常将某些工作表用作哈希表,因此我通常始终将它们隐藏起来。
我现在回去看看更新你的脚本。
好的,我认为你的脚本应该是这样的:
function emailGoogleSpreadsheetAsPDF() {
var email="email@gmail.com";
var ss=SpreadsheetApp.getActive();
var name=ss.getRange("Timesheet!J6").getValue();//trimmed the range down to match the getValue();
var agency=ss.getRange("Timesheet!B4").getValue();//same here
var fldr=DriveApp.getFolderById('folderId');
var fA=[];
var today=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy");
var subject=Utilities.formatString('%s has Submitted Their Timesheet and Notes',name);
var body=Utilities.formatString('This was submitted on %s',today);
var shts=ss.getSheets();
for(var i=0;i<shts.length;i++) {
var sh=shts[i];
var name=sh.getName();
sh.showSheet();
for(var j=0;j<shts.length;j++) {
if(shts[j].getName()!=name) {
shts[j].hideSheet();
}
}
SpreadsheetApp.flush();//this may not be necessary...not sure
var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s_%s_%s_timesheet_notes.pdf', name,agency,today));
fA.push(file);
}
GmailApp.sendEmail(email,subject,body, {attachments:fA});
for(var i=0;i<fA.length;i++) {
fA[i].setTrashed(true);
}
}
注意:我没有测试过最后一个,所以可能需要进行一些调试,但它与另一个示例基本相同。经过测试。
还有另一种使用 UrlFetchApp 的方法来执行此操作,已在 here 上进行了讨论。就我个人而言,我宁愿只创建文件并在最后将它们丢弃。
请求更改:
function emailGoogleSpreadsheetAsPDF() {
var email="email@gmail.com";
var exclA=['TimeSheet'];//and others
var ss=SpreadsheetApp.getActive();
var name=ss.getRange("Timesheet!J6").getValue();//trimmed the range down to match the getValue();
var agency=ss.getRange("Timesheet!B4").getValue();//same here
var fldr=DriveApp.getFolderById('folderId');
var fA=[];
var today=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy");
var subject=Utilities.formatString('%s has Submitted Their Timesheet and Notes',name);
var body=Utilities.formatString('This was submitted on %s',today);
var shts=ss.getSheets();
for(var i=0;i<shts.length;i++) {
var sh=shts[i];
var name=sh.getName();
if(exclA.indexOf(name)==-1) {
sh.showSheet();
for(var j=0;j<shts.length;j++) {
if(shts[j].getName()!=name) {
shts[j].hideSheet();
}
}
SpreadsheetApp.flush();//this may not be necessary...not sure
var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s_%s_%s_timesheet_notes.pdf', name,agency,today));
fA.push(file);
}
}
GmailApp.sendEmail(email,subject,body, {attachments:fA});
for(var i=0;i<fA.length;i++) {
fA[i].setTrashed(true);
}
for(var i=0;i<shts.length;i++) {
if(exclA.indexOf(shts[i].getName())==-1) {
shts[i].showSheet();
}
}
}
添加整个电子表格的 PDF(排除的隐藏工作表除外):
function emailGoogleSpreadsheetAsPDF() {
var email="email@gmail.com";
var exclA=['TimeSheet'];//and others
var ss=SpreadsheetApp.getActive();
var name=ss.getRange("Timesheet!J6").getValue();//trimmed the range down to match the getValue();
var agency=ss.getRange("Timesheet!B4").getValue();//same here
var fldr=DriveApp.getFolderById('folderId');
var fA=[];
var today=Utilities.formatDate(new Date(),Session.getScriptTimeZone(),"MM/dd/yyyy");
var subject=Utilities.formatString('%s has Submitted Their Timesheet and Notes',name);
var body=Utilities.formatString('This was submitted on %s',today);
var shts=ss.getSheets();
for(var i=0;i<shts.length;i++) {
var sh=shts[i];
var name=sh.getName();
if(exclA.indexOf(name)==-1) {
sh.showSheet();
for(var j=0;j<shts.length;j++) {
if(shts[j].getName()!=name) {
shts[j].hideSheet();
}
}
SpreadsheetApp.flush();//this may not be necessary...not sure
var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s_%s_%s_timesheet_notes.pdf', name,agency,today));
fA.push(file);
}
}
for(var i=0;i<shts.length;i++) {
if(exclA.indexOf(shts[i].getName())==-1) {
shts[i].showSheet();
}
}
SpreadsheetApp.flush();//this may not be necessary...not sure
var file=fldr.createFile(ss.getBlob().getAs('application/pdf')).setName(Utilities.formatString('%s.pdf',ss.getName()));
fA.push(file)
GmailApp.sendEmail(email,subject,body, {attachments:fA});
for(var i=0;i<fA.length;i++) {
fA[i].setTrashed(true);
}
}