我有一个电子表格,该电子表格始终有一个名为“时间表”的工作表,填写该表的人可以根据需要创建任意数量的其他笔记工作表。
除了“时间表”工作表之外,我还需要将所有工作表转换为单独的PDF并通过电子邮件发送给地址。
我在创建无法将其滚动到单个PDF中的脚本时遇到了麻烦。因此,它还在PDF中包括“时间表”表。
我需要将每个便签纸转换为单个PDF,然后将所有PDF作为单独的附件通过电子邮件发送出去。填写该文件的人还可以将便签重命名为他们想要的任何东西,因此我无法按名称获取该便签纸。< / p>
我有一些代码可以合并所有工作表,并将其重命名以用于其他目的,如果有帮助,我将在下面提供。
在此先感谢您的帮助
function emailGoogleSpreadsheetAsPDF() {
// Send the PDF of the spreadsheet to this email address
var email = "email@gmail.com";
// Get the currently active spreadsheet URL (link)
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Custom
var name = ss.getRange("Timesheet!J6:K6").getValue();
var agency = ss.getRange("Timesheet!B4:C4").getValue();
// Date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
today = mm + '/' + dd + '/' + yyyy;
// Subject of email message
var subject = name + " has Submitted Their Timesheet and Notes";
// Email Body can be HTML too
var body = "This was submitted on " + today;
var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
blob.setName(name + "_" + agency + "_" + today + "_" + "timesheet_notes.pdf");
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments:[blob]
});
}
答案 0 :(得分:0)
通过电子表格将电子表格发送为单独的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);
}
}