我必须每天编辑一个Google电子表格文件。当我结束时,我想向人们发送信息,通知他们我已经完成了。附在该通知邮件后,我想向他们发送一份特定的表格(称为报告),作为PDF格式。
我找到了这个发送电子邮件的选项(并且工作正常):
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}
有没有办法将特定工作表添加为PDF?
第二个问题:如何在电子表格中创建某种按钮(“立即发送”),让我轻松发送此电子邮件,因此我不必每次都打开脚本编辑器?
答案 0 :(得分:1)
在Google表格用户界面中,菜单项是将其设置为按需运行的一种自然方式。 1 学习如何自行完成此操作的一个很好的资源是Google&# 39; s Quickstart: Macros, Menus, and Custom Functions。
从该教程开始,这里将添加一个"发送报告"电子表格中的菜单项,选中后会调用sendReport_()
函数:
/**
* A special function that runs when the spreadsheet is open, used to add a
* custom menu to the spreadsheet.
*/
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Send Report', functionName: 'sendReport_'}
];
spreadsheet.addMenu('Custom', menuItems);
}
sendReport_()
功能假设我们有一个getPdfBlob()
函数,它将返回一个适合附加到电子邮件的blob
。有了这个,这里有sendReport_()
所有需要做的事情:
// From https://stackoverflow.com/a/37149036/1677912
function sendReport_() {
// Configuration parameters; customize as you wish
var sheetName = "Report";
var subject = "Email subject line";
var recipients = "user1@example.com, user2@example.com";
var htmlMessage = "Greetings,<br/><br/>"
+ "Please find today's report attached as a PDF.<br/><br/>"
+ "Cheers,<br/><br/>Paranoia";
// Get the IDs of the spreadsheet & sheet-of-interest
var ss = SpreadsheetApp.getActive();
var sheetId = ss.getSheetByName(sheetName).getSheetId();
// Retrieve the PDF blob
var pdfBlobArray = getPdfBlobs(ss.getId(),sheetId);
// Send the email + attachment
MailApp.sendEmail(recipients,subject,"Report attached.", {
htmlBody: htmlMessage,
attachments: pdfBlobArray
});
}
getPdfBlobs()
效用函数生成电子表格PDF的实用程序显示在Convert all sheets to PDF with Google Apps Script中。
这可以适用于返回包含您之后单张的PDF的blob
。
您必须启用Advanced Drive Service至&#34;资源&gt;高级驱动器服务......&#34;和开发人员控制台。 (有关详细信息,请参阅this。)
注意:通过编辑此功能中嵌入的网址参数,可支持PDF格式的自定义设置。
/**
* Get one or all sheets in a spreadsheet as PDF file blobs.
*
* From: https://stackoverflow.com/a/37149036/1677912
* Adapted from https://stackoverflow.com/a/30492812/1677912
*
* @param {String} optSSId (optional) ID of spreadsheet to export.
* If not provided, script assumes it is
* sheet-bound and opens the active spreadsheet.
* @param {String} optSheetId (optional) ID of single sheet to export.
* If not provided, all sheets will export.
*/
function getPdfBlobs( optSSId, optSheetId ) {
// If a sheet ID was provided, open that sheet, otherwise assume script is
// sheet-bound, and open the active spreadsheet.
var ss = (optSSId) ? SpreadsheetApp.openById(optSSId) : SpreadsheetApp.getActiveSpreadsheet();
// Get URL of spreadsheet, and remove the trailing 'edit'
var url = ss.getUrl().replace(/edit$/,'');
// Get array of all sheets in spreadsheet
var sheets = ss.getSheets();
// Loop through all sheets, generating PDF blobs.
var blobArray = [];
for (var i=0; i<sheets.length; i++) {
var sheet = sheets[i];
// If provided a optSheetId, only save it.
if (optSheetId && optSheetId !== sheet.getSheetId()) continue;
//additional parameters for exporting the sheet as a pdf
var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ '&gid=' + sheet.getSheetId() //the sheet's Id
// following parameters are optional...
+ '&size=letter' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=true' // fit to width, false for actual size
+ '&sheetnames=false&printtitle=false&pagenumbers=false' //hide optional headers and footers
+ '&gridlines=false' // hide gridlines
+ '&fzr=false'; // do not repeat row headers (frozen rows) on each page
var options = {
headers: {
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
}
}
var response = UrlFetchApp.fetch(url + url_ext, options);
var blob = response.getBlob().setName(ss.getName() + ' - ' + sheet.getName() + '.pdf');
// Add blob to our array
blobArray.push(blob);
}
// Return array of PDF blobs
return blobArray;
}
/**
* Dummy function for API authorization only.
* From: https://stackoverflow.com/a/37172203/1677912
*/
function forAuth_() {
DriveApp.getFileById("Just for authorization"); // https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579#c36
}
1 这可以进一步扩展为附加组件,因此不需要将脚本附加到特定的电子表格。