I need help with a script that I have created. I need to send an email to myself or manager any time cell "F" is changed to "Yes". I think my code is okay, but I don't think my triggers are allowing me to complete the function. Any help would be appreciated.
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var sendto = 'd.hammond#permasteelisagroup.com';
if(cell.indexOf('F')!=-1){
sendto = sheet.getRange('F'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var designer = '';
designer = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue()
var assignment = '';
assignment = sheet.getRange('B' + sheet.getActiveCell().getRowIndex()).getValue()
var comment = '';
comment = sheet.getRange('I' + sheet.getActiveCell().getRowIndex()).getValue()
var checked_by = '';
checked_by = sheet.getRange('G' + sheet.getActiveCell().getRowIndex()).getValue()
var mysubject = ss.getName() + ':' + 'Review Drawing: \n';
var mybody = 'Please review: ' + assignment + ' \n' + 'Drawn by: ' + designer + '\n' + 'Please review any comments' + comment + ' \n' + 'Due by: ' + checked_by;
MailApp.sendEmail({
to:sendto,
subject:mysubject,
body:mybody});
};
*
Related
thanks in advance for your time. I am new to google apps scripting, so apologies if the question below is a basic one.
I have created functions for a sheet, which individually seem to run just fine. When both functions are combined, only one actually runs while the other doesn't work.
I'm hoping for some help as I'm sure the way that I have both laid out is incorrect.
Please see examples below.
function onEdit(){
autoNotes();
moveTabs();
}
function autoNotes() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getActiveSheet()
var lr = sheet.getLastRow()
var data = sheet.getRange("A3:AQ"+lr).getValues()
var activeCell = sheet.getActiveCell()
var col = activeCell.getColumn()
var row = activeCell.getRow()
var mailAddress = data\[row-3\]\[11\]
var instructorName = '\<b\>Instructor: \</b\>'+ data\[row-3\]\[1\] + ' ' + data\[row-3\]\[2\]
var instructorFirst = '\<b\>\</b\>'+ data\[row-3\]\[1\]
var instructorFirstLast = data\[row-3\]\[1\] + ' ' + data\[row-3\]\[2\]
var instructorCourse = "\<b\>Course Build Training Track: \</b\>"+ data\[row-3\]\[5\] + ', ' + data\[row-3\]\[6\]
var totalDays = data\[row-3\]\[28\].toFixed(1)
var programrepFirstLast = data\[row-3\]\[10\]
var instructorEmail = data\[row-3\]\[3\]
var totalPercent = Number(data\[row-3\]\[29\]).toLocaleString(undefined, { style: "percent", minimumFractionDigits: 0,})
var formatDate1 = new Date(data\[row-3\]\[7\]).toLocaleDateString()
var courseDate = '\<b\>Course Start Date: \</b\>'+ formatDate1
var courseDate2 = formatDate1
var formatDate = new Date(data\[row-3\]\[27\]).toLocaleDateString()
var startDate = '\<b\>Training Start Date: \</b\>'+ formatDate
var startDate2 = formatDate
var urlsheet = "https://docs.google.com/spreadsheets/d/1DI074UNhvj_LNM35bb9WTsf9aFkxsTcmjOWnNT3nXts/edit?usp=sharing"
var recipients = mailAddress
var body = instructorName + '\\n' + instructorCourse + '\\n' + instructorFirst + '\\n' + startDate + '\\n' + courseDate + '\\n' + totalDays + '\\n' + programrepFirstLast + '\\n' + totalPercent + '\\n' + instructorEmail + '\\n' + startDate2 + '\\n' + courseDate2
// var contentHTML = "<body><p>"+ instructorName + "</p><p>" +instructorCourse + "</p><p>" +startDate + "</p><p><br> <a href='" + urlsheet + "'>MASTER Enrollment Sheet</a> </p><br> </body>"
if(row\>2 && col == 31 && activeCell.getValue()==true){
var subject = data\[row-3\]\[1\] + ' ' + data\[row-3\]\[2\]+ ' Training Update: Attended Kick-Off Meeting'
var contentHTML ="\<br\>" + instructorName + "\<br\>" + instructorCourse + "\<br\>" + courseDate + "\<br\>" + startDate + "\<br\>\<br\>" + "\<b\>Training Update:\</b\>" + "\<br\>\<br\>" + "Dear " + programrepFirstLast + "," + "\<br\>\<br\>" + "Instructor " + instructorFirstLast + " has attended the Canvas Course Development Kick-off Meeting with an instructional designer. The instructor has approximately \<b\>" + totalDays + " weeks\</b\> from their course start date to participate in training and build their Canvas course." + "\<br\>\<br\>" + "Next steps: \<ul\>\<li\>The instructor will use their personalized \<b\>Action Plan checklist\</b\> and reference the \<b\>CSGCB course\</b\> to learn Canvas and build relevant course elements.\</li\>\<li\>The instructor can attend live training sessions held each week to learn more about using Canvas, Zoom, and Panopto for video recording. \</li\>\<li\>The instructor can contact ATLI with questions at any point as they work their way through the Action Plan. \</li\>\</ul\> You will receive the next training update when " + instructorFirst + " completes the first set of major milestone tasks in the Action Plan." + "\<br\>\<br\>\<br\>" + "\<span style='background-color: #daebfd; font-size: 16px; padding: 1px;'\>\<b\>Please do not reply to this message via e-mail.\</b\> This address is automated, unattended, and cannot help with questions or requests. For assistance, contact UCLA Extension Learning Support at " + "\<b\>\<a href='mailto:atli#uclaextension.edu'\>atli#uclaextension.edu\</a\>\</b\>\</span\>." + "\<br\>\<br\>"
MailApp.sendEmail(recipients, subject, body,{htmlBody: contentHTML})
ss.toast("Email notification has been sent successfully.",'Status')
return
}
function moveTabs() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = e.source.getActiveSheet();
var r = e.source.getActiveRange();
if(s.getName() == "InProgress" && r.getColumn() == 35 && r.getValue() == "Y") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Completed");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
s.deleteRow(row);
}
}
Just hoping to get both functions to work. Right now, combined, only the top function works and triggers an email to go out.
I am getting an error ReferenceError: options is not defined at myfunctionFDI(OverallScript.gs:39:48)at onEdit(OverallScript.gs:3:1), when I am trying to run multiple myfunctions within onEdit, what am I doing wrong, it appears to run the first myfunction correctly. Help would be appreciated script is here
function onEdit(e) {
myfunctionMCR(e);
myfunctionFDI(e);
}
function myfunctionMCR(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetName = sheet.getName();
var cell = sheet.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = sheet.getActiveCell().getValue().toString();
var recipients = "joebloggs#blogs.com";
var message = '';
if(cellvalue === 'MCR Approved'){
message = 'Cell ' + cell + ' in Sheet MAN ' + sheetName + ' was changed to MCR Approved and is ready for your Review & Approval.';
var subject = 'Cell Changed to MCR Approved';
var body = message + ' Visit ' + ss.getUrl() + ' to view the change and approve';
var options = {
cc: "fredblogs#blogs.com,bobblogs#blogs.com"
}
MailApp.sendEmail(recipients, subject, body, options);
}
}
function myfunctionFDI(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetName = sheet.getName();
var cell = sheet.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = sheet.getActiveCell().getValue().toString();
var recipients = "fredblogs#blogs.com";
var message = '';
if(cellvalue === 'FDI'){
message = 'Cell ' + cell + ' in Sheet MAN ' + sheetName + ' was changed to FDI and is ready for your review.';
var subject = 'Cell Changed to FDI Review';
var body = message + ' Visit ' + ss.getUrl() + ' FDI to Review';
MailApp.sendEmail(recipients, subject, body, options);
};
}
I am very new to google script and cannot work out why my script is not functioning. When I try to run the code I get the following error code -
TypeError: Cannot read property "range" from undefined. (line 2, file "Code"
If anybody could give a novice some help that would be greatly appreciated!
function sendEmail(event){
var sheet = event.range.getSheet();
if(sheet.getName() !== 'Sheet1') {
// if it's not the correct sheet we end the function here
return;
}
var range = event.range;
var row = range.getRow(); // we get the index of the row of the
edited
cell
var dataRange = sheet.getRange("A" + row + ":C" + row);
var values = dataRange.getValues();
var rowValues = values[0];
var recipient = rowValues[0];
var email = rowValues[1];
var refillsNumber = rowValues[2];
if (refillsNumber = 2) {
// if 'refillsNumber' is not equal to 2 we end the function here
var message = 'Dear ' + recipient + ',\n\n'+ 'You have ' +
refillsNumber + ' remaining would you like to buy more?' + '\n\n' +
'Kind regards,' + '\n\n' + 'The revol team.';
var subject = 'Refill reminder';
MailApp.sendEmail(email,subject,message);
return
}
if (refillsNumber !== 0) {
return}
var message = 'Dear ' + recipient + ',\n\n'+ 'You have ' +
refillsNumber + ' remaining would you like to buy more? If not then
would you like to end your subsription?' + '\n\n' + 'Kind regards,'
+ '\n\n' + 'The revol team.';
var subject = 'Refill reminder';
MailApp.sendEmail(email,subject,message);
return
}
// in addition to this script which aims to send an email to the
customer when they only have 2 refill remaining -
/// I also want to set up a function that sends an email when they
have 0 refills remaining
Send Emails by Editing Column C
I changed the Sheet name and added an extra condition. I also commented out the MailApp.sends and replaced them with writes to the sidebar so that I could test it without trying to send emails.
You will need to create an onEdit trigger for this function.
function sendMailOnEdit(event){
var sheet = event.range.getSheet();
if(sheet.getName() !== 'Sheet2' || event.range.columnStart!=3) {return;}
var range=event.range;
var row=range.rowStart;
var dataRange = sheet.getRange(row,1,1,3);
var values=dataRange.getValues();
var rowValues=values[0];
var recipient = rowValues[0];
var email = rowValues[1];
var refillsNumber = rowValues[2];
if (refillsNumber==2) {
var message = 'Dear ' + recipient + ',\n\n'+ 'You have ' + refillsNumber + ' remaining would you like to buy more?' + '\n\n' + 'Kind regards,' + '\n\n' + 'The revol team.';
var subject = 'Refill reminder';
//MailApp.sendEmail(email,subject,message);
var html=Utilities.formatString('<br />Email: %s<br />Subject: %s<br />message: %s<br /><hr>', email,subject,message);
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showSidebar(userInterface);
return
}
if (refillsNumber !== 0) {return}
var message = 'Dear ' + recipient + ',\n\n'+ 'You have ' + refillsNumber + ' remaining would you like to buy more? If not then would you like to end your subsription?' + '\n\n' + 'Kind regards,' + '\n\n' + 'The revol team.';
var subject = 'Refill reminder';
//MailApp.sendEmail(email,subject,message);
var html=Utilities.formatString('<br />Email: %s<br />Subject: %s<br />message: %s<br /><hr>', email,subject,message);
var userInterface=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showSidebar(userInterface);
}
This is what my spreadsheet looks like:
And let me remind you that you cannot run this function without an event object.
I'd like to send a notification to someone when a particular cell (ie document) is updated. Specifically, when a cell in one row is updated, it would send a notification to the owner listed in the same row. For example B47 gets updated, it send an email to B3.
I've gotten the script to work when it's pointed to one sheet. But when I try to ask the script to run over multiple sheets/tabs, I get error 'Script function not found: sendNotification' Is it possible to have this script work for multiple sheets/tabs in the same google doc? Updates are pulling from a different source so I'd like to keep using getSheetbyName instead of getActiveSheet.
Any help would be much appreciated. Here's my code where it stands:
function shellFunction() {
var sheets = ['Arabic', 'Portuguese'];
for (var s in sheets) {
toTrigger(sheets[s]);
}
function toTrigger(sheetName) {
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheets[s]);
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = sheet.getRange('J' + sheet.getActiveCell().getRowIndex()).getValue();
var message = '';
if (cell.indexOf('B') != -1) {
message = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'The ' + sheet.getRange('F' + sheet.getActiveCell().getRowIndex()).getValue() + ' ' + sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' needs your Translation';
var body = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' has been updated. Can you please update ' + sheet.getRange('G' + sheet.getActiveCell().getRowIndex()).getValue() + '? Please remember to update the date column in the Resource Document when the translation is complete:' + ss.getUrl();
MailApp.sendEmail(recipients, subject, body);
}
}
}
Should this line
var sheet = ss.getSheetByName(sheets[s]);
be changed to
var sheet = ss.getSheetByName(sheetName);
since that is the name of the passed sheet?
EDIT
There were a couple other items which may have caused issue. I made edits for this result:
function shellFunction() {
var sheets = ['Arabic', 'Portuguese'];
for (var s in sheets) {
toTrigger(sheets[s]);
}
}
function toTrigger(sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = sheet.getRange('J' + sheet.getActiveCell().getRowIndex()).getValue();
var message = '';
if (cell.indexOf('B') != -1) {
message = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'The ' + sheet.getRange('F' + sheet.getActiveCell().getRowIndex()).getValue() + ' ' + sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' needs your Translation';
var body = sheet.getRange('A' + sheet.getActiveCell().getRowIndex()).getValue() + ' has been updated. Can you please update ' + sheet.getRange('G' + sheet.getActiveCell().getRowIndex()).getValue() + '? Please remember to update the date column in the Resource Document when the translation is complete:' + ss.getUrl();
MailApp.sendEmail(recipients, subject, body);
}
My first change was to not nest the functions. To do this I added a closing curly bracket before the toTrigger function and removed one of the last in your code. After that there was another nested function which actually made up all of the toTrigger function. It was called sendNotifocation which matched your error. I removed that by removing the line after the toTrigger function declaration and another curly bracket at the end of your code.
Last, I made the above edit. In my test it seemed to work fine, except that I commented out the call to actually send an email and only debugged to the line just prior to that point.
I could really use some help, and I'd really appreciate any pointers anyone can give. I know there are some other similar questions out there, but from what I can find, no one else has this specific issue/request.
I have a Google Sheet that three different people collaborate on. I have this notification script that will notify specific users via email about any changes to any of the cells in the F column. Importantly, this email notification also contains the value of the A cell in that row that has had it's F cell value changed.
However, what I'm really trying to achieve is a notification script that will email a specified person only when any cell in column F has a new value of "Yes" in Sheet2 of the master sheet.
The code I have so far is (that sends a notification when any change happens in the F column):
function sendNotification(e) {
var received = e.value; //Gets the value of the edited cell
};
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet 2");
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var received = ss.getRange('F2:F').getValue();
var recipients = "example#gmail.com";
var message = '';
if(received==="Yes"){
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body)};
Logger.log('received value: ' + received);
};
If it helps, below is a code that works in my tests : (set up an installable trigger to make it work)
note : I added a couple of Logger.log here and there to check the variables and a Browser message to avoid sending mails while testing.
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet 2");
var cell = ss.getActiveCell();
var row = sheet.getActiveRange().getRow();
var col = cell.getA1Notation().replace(/[0-9]/g,'').toLowerCase();
Logger.log('col = '+col);
var cellValue = cell.getValue().toString().toLowerCase();
Logger.log(cellValue);
var recipients = "example#gmail.com";
var message = '';
Logger.log(cellValue=="yes" && col=='f');
if(cellValue=="yes" && col=='f' ){
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellValue + '». For message: «' + message + '»';
// MailApp.sendEmail(recipients, subject, body)
Browser.msgBox('mail sent');
}
Logger.log('message body: ' + body);
};
To set up an event object use:
function sendNotification(e) {
var received = e.value; //Gets the value of the edited cell
};
The code that is sending the email, is outside of the if body. So the email is being sent no matter what.
Change to this:
if(received==="Yes"){
message = sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue()
var subject = 'A cell value '+ sheet.getRange('A'+ sheet.getActiveCell().getRowIndex()).getValue() + ' has been received';
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
};
In this line of code:
if(received="Yes"){
You are using an assignment operator to check for equality. Equality checks are done with double or triple equal signs.
Should be:
if(received==="Yes"){