首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Google脚本编辑器发送电子邮件

使用Google脚本编辑器发送电子邮件
EN

Stack Overflow用户
提问于 2020-03-05 10:51:41
回答 1查看 127关注 0票数 0

我知道在代码中有两个单独的电子邮件函数,但我尝试使用的是“sendsheettopdfwithA2mailaddress A2mailaddress”。每次我尝试删除另一个时,我都会收到一条错误消息,因此,如果能帮助我删除它,一切都会正常工作,我们将非常感激。我遇到的主要问题是,我只想通过电子邮件发送一张纸,这是主要的一张或0张。当它发送电子邮件时,它会发送12个不同的工作表,而工作表0被压缩到大约四分之一的工作表。

代码语言:javascript
运行
复制
/** @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);
  }
};
EN

回答 1

Stack Overflow用户

发布于 2020-03-06 21:55:37

当我单独运行这个宏时,它可以工作,但是当我把它放在onEdit函数中时,它就不能工作了。

看一看Simple Triggers Restrictions

报价:

  • 他们无法访问需要授权的服务。例如,简单触发器无法发送电子邮件,因为Gmail服务需要授权,但简单触发器可以使用语言服务匿名翻译短语。

如果你使用了可安装的触发器,你就会有更少的限制(比如能够发送电子邮件)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60537546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档