0
votes

Whenever I use either MailApp or GmailApp in my Google Script, it does not work. I have tested both of these with other functions such as retrieving mail and creating a draft. It seems as if MailApp and GmailApp does not want to connect to my Gmail account. I am programming this script in a Google Spreadsheet. I did get the .sendEmails() to work once, but have not been able to replicate the scenario. Here is the final product that should work:

function onEdit(e){
var date = new Date()
var time = ":" + date.getMinutes()
var hours24 = date.getHours() + 1; // retrieve current hours (in 24 mode)
var dayMode = hours24 < 12 ? "am" : "pm"; // if it's less than 12 then "am"
var hours12 = hours24 <= 12 ? (hours24 == 0 ? 12 : hours24) : hours24 - 12;
// "0" in 24-mode now becames "12 am" in 12-mode – thanks to user @Cristian
SpreadsheetApp.getActiveSheet().getRange('J2').setValue(date);
SpreadsheetApp.getActiveSheet().getRange('J3').setValue(hours12 + time +        dayMode);


var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var mycell = ss.getActiveSelection();
  var cellrow = mycell.getRow();  
  var cellcol = mycell.getColumn();

var range = sheet.getRange(cellrow,cellcol); 
var data = range.getValue();

    SpreadsheetApp.getActiveSheet().getRange('K3').setValue("pass");

if( cellrow >= 2){
    var currinv = sheet.getRange(cellrow, 5)
    var plusinv = currinv.getValue() + data;
    var minusinv = currinv.getValue() - data;
    SpreadsheetApp.getActiveSheet().getRange('K4').setValue("pass");
if(cellcol == 7) {
  SpreadsheetApp.getActiveSheet().getRange(cellrow,5).setValue(plusinv);
  SpreadsheetApp.getActiveSheet().getRange(cellrow,7).setValue("");
  SpreadsheetApp.getActiveSheet().getRange('J4').setValue(cellcol);
  SpreadsheetApp.getActiveSheet().getRange('K5').setValue("pass");
}

if(cellcol == 9) {
  SpreadsheetApp.getActiveSheet().getRange(cellrow,5).setValue(minusinv);
  SpreadsheetApp.getActiveSheet().getRange(cellrow,9).setValue("");
  SpreadsheetApp.getActiveSheet().getRange('J5').setValue(cellcol);
  SpreadsheetApp.getActiveSheet().getRange('K6').setValue("pass");
}

}
 var rowcurr = 2
 var rowcheck = currinv.getValue() 
 if(rowcheck == 1){
 SpreadsheetApp.getActiveSheet().getRange('K7').setValue("pass");
 var recipient = "[email protected]";
   var subject = "Test Email Alert System: Toner Inventory";
   var body = "This is a test email from the Toner Invenoty Alert System";

  MailApp.sendEmail(recipient, subject, body)

  //or in case MailApp doesn't work anymore

  //GmailApp.sendEmail(recipient, subject, body)

 }
 }      
1
You can't send a email from a simple trigger onEdit() function. There are restrictions on the onEdit() simple trigger. Documentation - Restrictions You need to used an installed edit trigger. Link Installable triggersAlan Wells
In that case I know there is a sendEmail() trigger that should definitely allow it. How would I send cell information gathered from the onEdit() trigger and have it sent to the sendEmail() to be used in the body of the email.Zach Lesniewski
Actually thank you for those link. I looked into the installable triggers and was able to give the onEdit() trigger authorization and I am up and running thank you.Zach Lesniewski

1 Answers

0
votes

Actually thank you for those link. I looked into the installable triggers and was able to give the onEdit() trigger authorization and I am up and running thank you.