我正在尝试使用GmailApp.sendEmail通过电子邮件发送Google工作表的PDF。当发件人具有@ gmail.com帐户时,该脚本可以工作,但是由于某种原因,GSuite帐户(而非@ gmail.com)发送的HTML文档中不包含工作表的PDF。
这是我的代码的一部分,到目前为止,我仅在此部分中使用GmailApp ..
function printForward() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheets()[3];
// if you change the number, change it also in the parameters below
var shName = sh.getName()
var ssname = ss.getName();
var length = ssname.length;
var date = ssname.substring(0,length-15);
const body = "All, <br><br>Attached is the invoice for the week of "+ date+"."+"<br><br> Thank you, <br> Elizabeth Moscoso";
var fnumber = 2;
sendSpreadsheetToPdf(3, shName, ss.getRange('Constants!T5').getValue(),"Invoices "+date, body, date, fnumber);
}
function printMidwest(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheets()[5];
var shName = sh.getName();
var ssname = ss.getName();
var length = ssname.length;
var date = ssname.substring(0, length-15);
const body = "All, <br><br>Attached is the invoice for the week of "+ date+"."+"<br><br> Thank you, <br> Elizabeth Moscoso";
var mnumber = 1;
sendSpreadsheetToPdf(5, shName, ss.getRange('Constants!T5').getValue(), "Invoices "+date, body, date, mnumber);
}
function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody, invcdate, foldNum) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;
var url_base = spreadsheet.getUrl().replace(/edit$/,'');
if(foldNum == 2){var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
// following parameters are optional...
+ '&size=A4' // 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
}
if(foldNum == 1)
{var url_ext = 'export?exportFormat=pdf&format=pdf' //export as pdf
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
// following parameters are optional...
+ '&size=A4' // 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: {method:"GET",
'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
}
}
var response = UrlFetchApp.fetch(url_base + url_ext, options);
var blob = response.getBlob().setName(pdfName+" Invoice "+ invcdate+ '.pdf');
var mailOptions = {};
var attachmentArray = [];
if(blob){
attachmentArray.push(blob);
mailOptions.attachments = attachmentArray;
}
if(htmlbody){
mailOptions.htmlBody = htmlbody;
}
if(email){
GmailApp.sendEmail(email,subject,"",mailOptions);
}
//save PDF
const nameFile = "Forward " + invcdate + ".pdf";
if(foldNum == 2){
var forwardFolder = DriveApp.getFolderById("1-n7O87J0zN7ZnOtErhcJR_JDbkrjSyj8");
forwardFolder.createFile(blob);}
if(foldNum == 1){
var midwestFolder = DriveApp.getFolderById("17cQrFnAzy3B6jmwZB5-Osa6G7oC3YOLw")
midwestFolder.createFile(blob);
}
}
我不确定为什么GSuite帐户无法发送电子邮件/ pdf。另外,如果我完全取出发送电子邮件的部分,而让GSuite帐户仅保存PDF,则它会提供相同的HTML文件。
答案 0 :(得分:1)
我刚刚尝试过,类似的URL应该可以导出PDF(假设有正确的oAuth2范围)。 PDF导出将需要范围https://www.googleapis.com/auth/spreadsheets.readonly
。
var url = 'https://docs.google.com/document/d/' + id + '/export?format=pdf'
这显然会导出整个文档,但是以后仍然可以缩小文档范围。
您还可以尝试使用类MailApp
而不是类GMailApp
,当对脚本进行更改时,这通常不会触发重新授权请求(这可能是一个因素)。除非有人需要访问用户的GMail邮箱,否则没有给定的理由使用GMailApp
。
oAuth2授权范围也有所不同;类GMailApp
使用范围很广:
https://mail.google.com/
虽然类MailApp
使用了非常特定的范围:
https://www.googleapis.com/auth/script.send_mail
从这个问题出发,还不清楚是否导入了“ OAuth2 for Apps Script”库。它的ID为:1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
。