I have been trying to find a way to send single row of data to a specific email address in a "live/running" spreadsheet that I am using within my domain, to keep track of truck drivers and their pick up numbers. I managed to piece together this little bit of code using the Google Apps Script Editor available in sheets:
function sendEmail()
{
var sheet = SpreadsheetApp.getActiveSheet();
var activeRow = sheet.getActiveCell().getRow();
var cellID = "H" + activeRow;
var dataRange = sheet.getRange(activeRow, 1, 1, 6);
var EMAIL_SENT = "EMAIL_SENT";
var data = dataRange.getValues();
Logger.log(data[0][0]);
var emailAddress = usercellnum@carrier.net;
var message = data;
var subject = "CG-PU#";
var emailSent = sheet.getRange(cellID).getValue();
if (emailSent != "EMAIL_SENT")
{
MailApp.sendEmail(emailAddress, subject, message);
var cell = sheet.getRange(cellID);
cell.setValue(EMAIL_SENT);
}
}
SpreadsheetApp.flush();
}
Any way I can call the function from a cell in my spreadsheet like =sendEmail()
and it should send the information from only the active row, and then flag that row as EMAIL_SENT
so I don't accidentally do it again.
The script runs perfect if I run it from inside script editor, and hard code the email address, but if I try and run it from the spread sheet itself I am getting the message:
error: You do not have permission to call sendEmail (line 19, file "CPS_sendEmail.gs").
Any assistance with this is greatly appreciated! It would help to keep my user from running back to MS Excel and Outlook.
When I get this part of it working, I would really like it if I could have the function display a list of addresses the user can select from, or some other way of keeping the user from having to remember and type in the drivers address and carrier, but I would love to just have this much of it working as expected.
The script would/should only be allowed to run in my Google Domain with my users, and the first time I executed it - I was sure I went through the authorization process, but don't truly understand that part of things.
They currently are using MS Excel/Outlook and they copy and paste the row into an email that they can use their contacts to select from. If I cannot get the function to use Googles Contacts, I was considering creating a sheet of drivers with a look up function or something - kind of a pain to have to keep a list when contacts are right there - but ya do what ya gotta do!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…