Javascript 更新的文档在Gmail中未附加为PDF
我正在使用谷歌应用程序脚本创建一个文档文件的副本,并将电子表格中的数据保存到新文件中,然后将其作为PDF附件发送到电子邮件。除了pdf文件显示的是源文件中的默认值,而不是更新后的值之外,其他一切都正常工作 这是我的密码Javascript 更新的文档在Gmail中未附加为PDF,javascript,google-apps-script,gmail,google-docs,Javascript,Google Apps Script,Gmail,Google Docs,我正在使用谷歌应用程序脚本创建一个文档文件的副本,并将电子表格中的数据保存到新文件中,然后将其作为PDF附件发送到电子邮件。除了pdf文件显示的是源文件中的默认值,而不是更新后的值之外,其他一切都正常工作 这是我的密码 var lr = dataSheet.getLastRow(); var dataRange = dataSheet.getRange(lr, 1, 1, 3).getValues(); var firstName = dataRange[0][1]; var lastName
var lr = dataSheet.getLastRow();
var dataRange = dataSheet.getRange(lr, 1, 1, 3).getValues();
var firstName = dataRange[0][1];
var lastName = dataRange[0][2];
var email = dataRange[0][3];
var emailText = "Hi "+firstName+",\n\nThank you for signing up.";
var emailSubject = "Test PDF";
var fileTemplate = DriveApp.getFileById("FILE_ID"); // Get Template File Id
var fileCopied = fileTemplate.makeCopy("Doc Copy-"+new Date()).getId(); // Make a copy of the template and get the id of the new file.
var doc = DocumentApp.openById(fileCopied); // Get destination doc
var dbody = doc.getBody(); // get destination doc's body
// Replace the fields with values from sheet.
dbody.replaceText("First Name", firstName);
dbody.replaceText("Last Name", lastName);
dbody.replaceText("EmailAddress", email);
Utilities.sleep(1000);
// Send Email with PDF attachment
MailApp.sendEmail(email, emailSubject, emailText, {
attachments: [doc.getAs(MimeType.PDF)]
});
我怎样才能在邮件中获得更新的DOC作为PDF附件?
< P>我认为,如果我们把“文件”当作“BLUB”,就可以做到这一点,那就是谷歌如何对待谷歌文档文件。请看这个例子,它转换和发送谷歌电子表格作为PDF文件/* Send Spreadsheet in an email as PDF, automatically */
function emailSpreadsheetAsPDF() {
// Send the PDF of the spreadsheet to this email address
var email = "amit@labnol.org";
// Subject of email message
// The date time string can be formatted in your timezone using Utilities.formatDate method
var subject = "PDF Reports - " + (new Date()).toString();
// Get the currently active spreadsheet URL (link)
// Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Email Body can be HTML too with your logo image - see ctrlq.org/html-mail
var body = "PDF generated using code at ctrlq.org from sheet " + ss.getName();
var url = ss.getUrl();
url = url.replace(/edit$/,'');
/* Specify PDF export parameters
// From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
exportFormat = pdf / csv / xls / xlsx
gridlines = true / false
printtitle = true (1) / false (0)
size = legal / letter/ A4
fzr (repeat frozen rows) = true / false
portrait = true (1) / false (0)
fitw (fit to page width) = true (1) / false (0)
add gid if to export a particular sheet - 0, 1, 2,..
*/
var url_ext = 'export?exportFormat=pdf&format=pdf' // export as pdf
+ '&size=letter' // paper size
+ '&portrait=false' // orientation, false for landscape
+ '&fitw=true&source=labnol' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false' // hide optional headers and footers
+ '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
+ '&fzr=false' // do not repeat row headers (frozen rows) on each page
+ '&gid='; // the sheet's Id
var token = ScriptApp.getOAuthToken();
var sheets = ss.getSheets();
//make an empty array to hold your fetched blobs
var blobs = [];
for (var i=0; i<sheets.length; i++) {
// Convert individual worksheets to PDF
var response = UrlFetchApp.fetch(url + url_ext + sheets[i].getSheetId(), {
headers: {
'Authorization': 'Bearer ' + token
}
});
//convert the response to a blob and store in our array
blobs[i] = response.getBlob().setName(sheets[i].getName() + '.pdf');
}
//create new blob that is a zip file containing our blob array
var zipBlob = Utilities.zip(blobs).setName(ss.getName() + '.zip');
//optional: save the file to the root folder of Google Drive
DriveApp.createFile(zipBlob);
// Define the scope
Logger.log("Storage Space used: " + DriveApp.getStorageUsed());
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {attachments:[zipBlob]});
}
/*自动以PDF格式通过电子邮件发送电子表格*/
函数emailSpreadsheetAsPDF(){
//将电子表格的PDF发送到此电子邮件地址
var电子邮件=”amit@labnol.org";
//电子邮件的主题
//可以使用Utilities.formatDate方法在时区中格式化日期时间字符串
var subject=“PDF报告-”+(新日期()).toString();
//获取当前活动的电子表格URL(链接)
//或者使用SpreadsheetApp.openByUrl(“”);
var ss=SpreadsheetApp.getActiveSpreadsheet();
//电子邮件正文也可以是HTML,带有您的徽标图像-请参阅ctrlq.org/HTML-mail
var body=“使用ctrlq.org上的代码从工作表”+ss.getName()生成的PDF;
var url=ss.getUrl();
url=url.replace(/edit$/,“”);
/*指定PDF导出参数
//发件人:https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
exportFormat=pdf/csv/xls/xlsx
网格线=真/假
printtitle=true(1)/false(0)
尺寸=法定/字母/A4
fzr(重复冻结行)=真/假
纵向=真(1)/假(0)
fitw(适合页面宽度)=真(1)/假(0)
如果要导出特定图纸,请添加gid-0、1、2、,。。
*/
var url_ext='export?exportFormat=pdf&format=pdf'//导出为pdf
+'&size=letter'//纸张大小
+“&trait=false”//方向,横向为false
+“&fitw=true&source=labnol”//适合宽度,实际大小为false
+“&sheetnames=false&printtitle=false”//隐藏可选的页眉和页脚
+“&pagenumbers=false&gridlines=false”//隐藏页码和网格线
+“&fzr=false”//不要在每页上重复行标题(冻结行)
+“&gid=”;//工作表的Id
var token=ScriptApp.getOAuthToken();
var sheets=ss.getSheets();
//创建一个空数组来保存获取的blob
var blobs=[];
对于(变量i=0;i 0)
sendmail(电子邮件、主题、正文,{附件:[zipBlob]});
}
这里有更多信息: