Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

google apps script - Sending Email Based on Cell Value

The email function works fine, but the code has the following error:

SyntaxError: Unexpected end of input (line 90, file "Code.gs") (last line) when the function checkValue was added.

Basically, we want the email to send automatically when D2 is edited. We are hoping this will eliminate the need to allow permissions to send email every time a template is copied by an aid.

    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]    
    });  
}   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basically, we want the email to send automatically when D2 is edited.

var rangeEdit =e.range.getA1Notation();

From what I see here it looks like you are trying to send an Email when the cell is changed, probably by using onEdit trigger.

You can't do that.

OnEdit belongs to simple triggers. Simple triggers can't call services that require authorization. Check here https://developers.google.com/apps-script/guides/triggers the Restrictions section.


If I'm wrong with what are you trying to do, please edit your post with the code, since "Carlos M" wrote in his reply that it can't compile.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...