【发布时间】:2021-06-28 09:31:29
【问题描述】:
我正在尝试将特定工作表作为 PDF 电子邮件附件发送。
我有下面的代码,它能够将每个指定的工作表导出并作为单独的电子邮件发送,但我希望它能够接受多个用户指定的工作表名称并将它们作为单个附件在单个电子邮件中发送.一封电子邮件中的多个附件同样好。
我目前使用的代码:
function mainSendEmail(){
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('SEND EMAIL', ui.ButtonSet.YES_NO);
if (response.getSelectedButton() == ui.Button.YES) {
emailSpreadsheetAsPDF_CHARTS();
SpreadsheetApp.flush();
emailSpreadsheetAsPDF_PL();
SpreadsheetApp.flush();
emailSpreadsheetAsPDF_Wages();
} else {
Logger.log('Do not send');
}
}
function emailSpreadsheetAsPDF_CHARTS() {
const sheetToPrint = "CHARTS"; // name of the sheet to print
const ss = SpreadsheetApp.getActiveSpreadsheet(); // the sheets to use
const email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I89').getValue().toString(); // grab the email address
const date = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I91').getValue().toString(); // grab the date
const subject = `SF - Flotta (${date})`; // the subject of the email
const body = emailBody; // body of the email
const shID = ss.getSheetByName(sheetToPrint).getSheetId(); // the ID of the sheet
const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId()); // url of the spreadsheet
const exportOptions =
'exportFormat=pdf'
+ '&format=pdf'// export as pdf / csv / xls / xlsx
+ '&size=A4'// size of the PDF (legal / A4 / letter)
+ '&portrait=true'// orientation of the PDF (false for landscape)
+ '&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
+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT
+ '&vertical_alignment=MIDDLE' //TOP/MIDDLE/BOTTOM
+ '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
+ '&gid='+shID; // the sheet's Id
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
// generate the PDF file
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
// send the email to the specified address with the specified body text and attached PDF file
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [{
fileName: `SF - Flotta (${date})` + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
}]
});
}
function emailSpreadsheetAsPDF_PL() {
const sheetToPrint = "QPL"; // name of the sheet to print
const ss = SpreadsheetApp.getActiveSpreadsheet(); // the sheets to use
const email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I89').getValue().toString(); // grab the email address
const date = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I91').getValue().toString(); // grab the date
const subject = `SF - Flotta PL (${date})`; // the subject of the email
const body = emailBody; // body of the email
const shID = ss.getSheetByName(sheetToPrint).getSheetId(); // the ID of the sheet
const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId()); // url of the spreadsheet
const exportOptions =
'exportFormat=pdf'
+ '&format=pdf'// export as pdf / csv / xls / xlsx
+ '&size=A4'// size of the PDF (legal / A4 / letter)
+ '&portrait=true'// orientation of the PDF (false for landscape)
+ '&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
+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT
+ '&vertical_alignment=MIDDLE' //TOP/MIDDLE/BOTTOM
+ '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
+ '&gid='+shID; // the sheet's Id
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
// generate the PDF file
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
// send the email to the specified address with the specified body text and attached PDF file
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [{
fileName: `SF - Flotta PL (${date})` + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
}]
});
}
function emailSpreadsheetAsPDF_Wages() {
const sheetToPrint = "QWages"; // name of the sheet to print
const ss = SpreadsheetApp.getActiveSpreadsheet(); // the sheets to use
const email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I89').getValue().toString(); // grab the email address
const date = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('CHARTS').getRange('I91').getValue().toString(); // grab the date
const subject = `SF - Flotta Bérek (${date})`; // the subject of the email
const body = emailBody; // body of the email
const shID = ss.getSheetByName(sheetToPrint).getSheetId(); // the ID of the sheet
const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId()); // url of the spreadsheet
const exportOptions =
'exportFormat=pdf'
+ '&format=pdf'// export as pdf / csv / xls / xlsx
+ '&size=A4'// size of the PDF (legal / A4 / letter)
+ '&portrait=false'// orientation of the PDF (false for landscape)
+ '&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
+ '&horizontal_alignment=CENTER' //LEFT/CENTER/RIGHT
+ '&vertical_alignment=MIDDLE' //TOP/MIDDLE/BOTTOM
+ '&scale=' + 4 // 1 = Normal 100% -- 2 = Fit to width -- 3 = Fit to height -- 4 = Fit to Page
+ '&gid='+shID; // the sheet's Id
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
// generate the PDF file
var response = UrlFetchApp.fetch(url+exportOptions, params).getBlob();
// send the email to the specified address with the specified body text and attached PDF file
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments: [{
fileName: `SF - Flotta Bérek (${date})` + ".pdf",
content: response.getBytes(),
mimeType: "application/pdf"
}]
});
}
我知道那里有很多多余的代码,但我对编码很陌生,所以这是我能做的最好的。对于上述问题,如果能得到任何帮助,我将不胜感激。
【问题讨论】:
标签: email google-apps-script google-sheets gmail google-sheets-api