【发布时间】:2020-06-17 14:58:12
【问题描述】:
我知道代码中有两个单独的电子邮件功能,但我想要开始工作的一个是“sendsheettopdfwithA2mailaddress”。每次我尝试删除另一个时,我都会收到一条错误消息,因此对于删除它的任何帮助以及一切仍然有效也将不胜感激。我遇到的主要问题是我只想通过电子邮件发送一张主要的或 0 的表格。当它发送电子邮件时,它会发送 12 张不同的表格,并且表格 0 非常压缩到大约四分之一的表格。
/** @OnlyCurrentDoc */
function onEdit(e) {
var ss = SpreadsheetApp.getActive() //gets the active spreadsheet
var sheet = SpreadsheetApp.getActiveSheet() //gets the active sheet
var cell = ss.getActiveRange() //gets the active cell
var cellContent = cell.getValue() //gets the value of the active cell
if(cellContent === 'BOXCHECKED') {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('B1:H4').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getRange('A1:H55').activate();
spreadsheet.getRange('A1').activate();
spreadsheet.getCurrentCell().setFormula('=H5');
spreadsheet.getRange('H56').activate();
spreadsheet.insertSheet(1);
spreadsheet.getRange('\'Quote Sheet\'!A1:H55').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
spreadsheet.getRange('\'Quote Sheet\'!A1:H55').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
spreadsheet.getActiveSheet().setHiddenGridlines(true);
spreadsheet.getActiveSheet().hideSheet();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Data Sheets'), true);
spreadsheet.getRange('2:35').activate();
spreadsheet.getActiveSheet().insertRowsBefore(spreadsheet.getActiveRange().getRow(), 34);
spreadsheet.getActiveRange().offset(0, 0, 34,
spreadsheet.getActiveRange().getNumColumns()).activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Quote Sheet'), true);
spreadsheet.getRange('B11').activate();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.NEXT).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.UP).activate();
currentCell.activateAsCurrentCell();
spreadsheet.getRange('A12:z44').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Data Sheets'), true);
spreadsheet.getRange('\'Quote Sheet\'!A12:z44').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Quote Sheet'), true);
spreadsheet.getRangeList(['C5:C10', 'H6:H10', 'B12:B44','D12:E44', 'H49:H50','B51:E55']).activate();
spreadsheet.setCurrentCell(spreadsheet.getRange('H49'));
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
spreadsheet.getRange('C5').activate();
SpreadsheetApp.getActive();
spreadsheet.getRange('G53').activate();
spreadsheet.getCurrentCell().setValue('False')
SpreadsheetApp.getActive();
spreadsheet.getRange('A1').activate();
spreadsheet.getCurrentCell().setValue('Quote Sheet')
SpreadsheetApp.getActive().getSheets().forEach(function (sh)
{
sh.setName(sh.getRange('a1').getValue())
})
}
};
function renameSheets() {
SpreadsheetApp.getActive().getSheets().forEach(function (sh) {
sh.setName(sh.getRange('A1').getValue())
})
};
/* Email Google Spreadsheet as PDF */
function emailGoogleSpreadsheetAsPDF() {
// Send the PDF of the spreadsheet to this email address
var email = "t.mitchellbivens@gmail.com";
// Get the currently active spreadsheet URL (link)
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Subject of email message
var subject = "PDF generated from spreadsheet " + ss.getName();
// Email Body can be HTML too
var body = "Install the <a href='http://www.labnol.org/email-sheet'>Email Spreadsheet add-on</a> for
one-click conversion.";
var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
blob.setName(ss.getName() + ".pdf");
// If allowed to send emails, send the email with the PDF attachment
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody: body,
attachments:[blob]
});
};
function sendSheetToPdfwithA2MailAdress() { // this is the function to call
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheets()[0]; // it will send sheet 0 which is the first sheet in the spreadsheet.
// if you change the number, change it also in the parameters below
var shName = sh.getName()
sendSpreadsheetToPdf(0, shName, sh.getRange('A2').getValue()," Test Run ", "Let's Hope This Works");
}
function sendSpreadsheetToPdf(sheetNumber, pdfName, email,subject, htmlbody) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var spreadsheetId = spreadsheet.getId()
var sheetId = sheetNumber ? spreadsheet.getSheets()[sheetNumber].getSheetId() : null;
var url_base = spreadsheet.getUrl().replace(/edit$/,'');
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
+ '&fith=true' // fit to height, false for actual size
+ '&sheetnames=true&printtitle=false&pagenumbers=true' //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_base + url_ext, options);
var blob = response.getBlob().setName(pdfName + '.pdf');
if (email) {
var mailOptions = {
attachments:blob, htmlBody:htmlbody
}
MailApp.sendEmail(
email,
subject+" (" + pdfName +")",
"html content only",
mailOptions);
}
};
【问题讨论】:
-
您遇到什么错误?你是如何调用函数的?
-
错误消息是“异常:docs.google.com 的请求失败,返回代码 401。截断的服务器响应:。对于我有 sendSheetToPdfwithA2MailAdress 的功能
-
如果可能的话,我希望它在 onEdit 函数下工作。
-
你的代码有效吗?我不明白你为什么要删除
sendSpreadsheetToPdf(),因为你在sendSheetToPdfwithA2MailAdress()中调用它(因此它是必需的)。因此,如果您删除该函数,代码会中断是有道理的。 -
宏在我单独运行时有效,但是当我将它放在 onEdit 函数中时它就不起作用了。
标签: javascript email google-apps-script html-email