根据单元格值发送电子邮件

时间:2021-01-18 19:26:38

标签: email google-apps-script google-sheets

email 功能正常,但是代码出现如下错误:

语法错误:添加函数 checkValue 时,意外的输入结束(第 90 行,文件“Code.gs”)(最后一行)。

基本上,我们希望在编辑 D2 时自动发送电子邮件。我们希望这将消除每次由助手复制模板时都允许发送电子邮件的需要。

    function checkValue () {
var check = sheet.getRange("D2").getValue();
var rangeEdit =e.range.getA1Notation();
if(rangeEdit == "D2") {
  {
   
function email(checkValue) {
 
 

  // Send the PDF of the spreadsheet to this email address
 

  // Get the currently active spreadsheet URL (link)
  // Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
   
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("ClassA")
   
  var lrow = sheet.getLastRow()
   
 
  var name = sheet.getRange("E4").getValue();
  var aid = sheet.getRange("E3").getValue();
  var email = sheet.getRange("E5").getValue();
  var pemail = sheet.getRange("E2").getValue();
 
  var period = sheet.getRange("C1").getValue();
 var og= sheet.getRange("D2").getValue();
 
 



  // Subject of email message
  var subject = "Grade Summary | " + og +"- " + period;

  // Email Body can  be HTML too with your logo image - see ctrlq.org/html-mail
  var body = "Hi " + name + ", "+ "<br><br> Please find the grade summary  attached for " + period + ". <br><br>  Let us know if you have any questions.<br><br>  Thank you,<br><br> " + aid;
     
  var aliases = GmailApp.getAliases()
Logger.log(aliases); //returns the list of aliases you own
Logger.log(aliases[0]); //returns the alias located at position 0 of the aliases array
 
  // Base URL
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());

  /* Specify PDF export parameters
  From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
  */

  var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
  + '&size=letter'                       // paper size legal / letter / A4
  + '&portrait=True'                    // orientation, false for landscape
  + '&fitw=true'           // fit to page width, false for actual size
  + '&sheetnames=false&printtitle=false' // hide optional headers and footers
  + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
  + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
  + '&gid=';                             // the sheet's Id

  var token = ScriptApp.getOAuthToken();


  //make an empty array to hold your fetched blobs  
  var blobs;


    // Convert your specific sheet to blob
    var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
      headers: {
        'Authorization': 'Bearer ' +  token
      }
    });

    //convert the response to a blob and store in our array
    blobs = response.getBlob().setName(sheet.getName() + '.pdf');


  // Define the scope
  Logger.log("Storage Space used: " + DriveApp.getStorageUsed());
   
    MailApp.sendEmail(email, subject, body, {
      htmlBody: body,
 
      name:'class',
      bcc: aid,
      noReply: true,
      attachments:[blobs]    
    });  
}   

2 个答案:

答案 0 :(得分:0)

您的代码没有足够的右括号来终止函数 checkValue,因此它返回一个语法错误。

由于您的意图是在发送电子邮件之前先检查该值,因此最好将 email 函数分开并从 checkValue 中调用它,而不是将其包含在 if 语句中。也不需要参数。

此外,由于您表示需要发送电子邮件,因此您需要一个可安装的触发器。通过运行 createSpreadsheetEditTrigger() 使用脚本服务创建触发器,然后运行 ​​checkValue()

function createSpreadsheetEditTrigger() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ScriptApp.newTrigger('checkValue')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

function checkValue(e) {
  var rangeEdit = e.range.getA1Notation();
    if(rangeEdit == "D2") {
      email();
    }
}

function email() {
          // Send the PDF of the spreadsheet to this email address

          // Get the currently active spreadsheet URL (link)
          // Or use SpreadsheetApp.openByUrl("<<SPREADSHEET URL>>");
          
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var sheet = ss.getSheetByName("ClassA")
          
          var lrow = sheet.getLastRow()
          
        
          var name = sheet.getRange("E4").getValue();
          var aid = sheet.getRange("E3").getValue();
          var email = sheet.getRange("E5").getValue();
          var pemail = sheet.getRange("E2").getValue();
        
          var period = sheet.getRange("C1").getValue();
        var og= sheet.getRange("D2").getValue();
        
        



          // Subject of email message
          var subject = "Grade Summary | " + og +"- " + period;

          // Email Body can  be HTML too with your logo image - see ctrlq.org/html-mail
          var body = "Hi " + name + ", "+ "<br><br> Please find the grade summary  attached for " + period + ". <br><br>  Let us know if you have any questions.<br><br>  Thank you,<br><br> " + aid;
            
          var aliases = GmailApp.getAliases()
        Logger.log(aliases); //returns the list of aliases you own
        Logger.log(aliases[0]); //returns the alias located at position 0 of the aliases array
        
          // Base URL
          var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());

          /* Specify PDF export parameters
          From: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3579
          */

          var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
          + '&size=letter'                       // paper size legal / letter / A4
          + '&portrait=True'                    // orientation, false for landscape
          + '&fitw=true'           // fit to page width, false for actual size
          + '&sheetnames=false&printtitle=false' // hide optional headers and footers
          + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
          + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
          + '&gid=';                             // the sheet's Id

          var token = ScriptApp.getOAuthToken();


          //make an empty array to hold your fetched blobs  
          var blobs;


            // Convert your specific sheet to blob
            var response = UrlFetchApp.fetch(url + url_ext + sheet.getSheetId(), {
              headers: {
                'Authorization': 'Bearer ' +  token
              }
            });

            //convert the response to a blob and store in our array
            blobs = response.getBlob().setName(sheet.getName() + '.pdf');


          // Define the scope
          Logger.log("Storage Space used: " + DriveApp.getStorageUsed());
          
            MailApp.sendEmail(email, subject, body, {
              htmlBody: body,
        
              name:'class',
              bcc: aid,
              noReply: true,
              attachments:[blobs]    
            });  
}

参考:

Installable Triggers | Apps Script

答案 1 :(得分:0)

<块引用>

基本上,我们希望在编辑 D2 时自动发送电子邮件。

var rangeEdit =e.range.getA1Notation();

从我在这里看到的情况来看,您似乎正在尝试在单元格更改时发送电子邮件,可能是使用 onEdit 触发器。

你不能那样做。

OnEdit 属于简单触发器。简单触发器不能调用需要授权的服务。在此处查看https://developers.google.com/apps-script/guides/triggers限制部分。


如果我对您的操作有误,请使用代码编辑您的帖子,因为“Carlos M”在他的回复中写道它无法编译。

相关问题