簡體   English   中英

使用 Google 腳本編輯器發送電子郵件

[英]Email with Google Script Editor

我知道代碼中有兩個單獨的電子郵件功能,但我試圖開始工作的是'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 wich 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);
  }
};

宏在我單獨運行時有效,但當我將它放在 onEdit 函數中時,它不起作用。

查看簡單觸發器限制

引用:

  • 他們無法訪問需要授權的服務。 例如,簡單觸發器無法發送電子郵件,因為 Gmail 服務需要授權,但簡單觸發器可以使用匿名的語言服務翻譯短語。

如果您改用可安裝的觸發器,您的限制就會減少(例如能夠發送電子郵件)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM