GmailApp.sendEmail 未使用 G Suite 帐户
GmailApp.sendEmail not working with G Suite Account
我正在尝试使用 GmailApp.sendEmail 通过电子邮件发送 google sheet 的 PDF。该脚本在发件人拥有@gmail.com 帐户时有效,但由于某种原因,GSuite 帐户(不是@gmail.com)发送的 HTML 文档不包含 [=] 的 PDF 25=].
这是我的代码片段,到目前为止我只在这部分使用 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 帐户无法发送 email/pdf。此外,如果我完全删除发送电子邮件部分并让 GSuite 帐户仅保存 PDF,它会提供相同的 HTML 文件。
我刚刚自己尝试过,类似的 URL 应该导出 PDF(假设有适当的 oAuth2 范围)。 PDF 导出需要范围 https://www.googleapis.com/auth/spreadsheets.readonly
.
var url = 'https://docs.google.com/document/d/' + id + '/export?format=pdf'
这显然会导出整个文档,但以后仍然可以缩小范围。
您也可以尝试使用 class MailApp
而不是 class GMailApp
,这不会经常触发 re-authorization 请求根据脚本制作(这可能是一个因素)。除非需要访问用户的 GMail 邮箱,否则没有理由使用 GMailApp
.
oAuth2 授权范围也各不相同; class GMailApp
使用范围比较广:
https://mail.google.com/
虽然 class MailApp
使用了一个非常具体的范围:
https://www.googleapis.com/auth/script.send_mail
如果您导入了“OAuth2 for Apps Script”库,从问题来看也不是很清楚;它的 ID 将是:1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
.
我正在尝试使用 GmailApp.sendEmail 通过电子邮件发送 google sheet 的 PDF。该脚本在发件人拥有@gmail.com 帐户时有效,但由于某种原因,GSuite 帐户(不是@gmail.com)发送的 HTML 文档不包含 [=] 的 PDF 25=].
这是我的代码片段,到目前为止我只在这部分使用 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 帐户无法发送 email/pdf。此外,如果我完全删除发送电子邮件部分并让 GSuite 帐户仅保存 PDF,它会提供相同的 HTML 文件。
我刚刚自己尝试过,类似的 URL 应该导出 PDF(假设有适当的 oAuth2 范围)。 PDF 导出需要范围 https://www.googleapis.com/auth/spreadsheets.readonly
.
var url = 'https://docs.google.com/document/d/' + id + '/export?format=pdf'
这显然会导出整个文档,但以后仍然可以缩小范围。
您也可以尝试使用 class MailApp
而不是 class GMailApp
,这不会经常触发 re-authorization 请求根据脚本制作(这可能是一个因素)。除非需要访问用户的 GMail 邮箱,否则没有理由使用 GMailApp
.
oAuth2 授权范围也各不相同; class GMailApp
使用范围比较广:
https://mail.google.com/
虽然 class MailApp
使用了一个非常具体的范围:
https://www.googleapis.com/auth/script.send_mail
如果您导入了“OAuth2 for Apps Script”库,从问题来看也不是很清楚;它的 ID 将是:1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF
.