I have read a lot of onEdit and Triggers script but still I cannot achieve what I want to achieve, I code a lot in excel VBA and google sheet is very different. So the thing is based on my screenshot, what I want is to send an email once the cell contains "Approved", "denied", "In progress" and the email address must be based on the parallel of the edited cell. I'm dying to get this work done.
The code is based on the internet but I cannot tweak it based on my data/sheet.
You can add a custom function to a dropdown menu in the Spreadsheets UI with the following script. This will allow you to circumvent the onEdit() restriction that doesn't allow one to use the MailApp class, but it is at the cost of having users manually call the script instead of the script running automatically.
Here the user will select "Send E-Mail" from the dropdown menu, and it will prompt him/her for the Primary Key via an input prompt modal. The row of the corresponding key will be identified and an e-mail sent out after status is automatically changed to "approved". This script assumes that the spreadsheet contains at least four columns with header rows "Primary Key", "Description", "Email", and "Status" in any order.
Please note: this code was tested successfully. Please update lines 20 and 21 by replacing the square brackets and text contained therein that defines sheetURL and workSheetName variables.
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addItem('Send E-Mail', 'sendEmail')
.addToUi();
}
function sendEmail(){
// Display a dialog box with a title, message, input field, and "OK" and "Cancel" buttons. The
// user can also close the dialog by clicking the close button in its title bar.
var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Pop-Up Prompt', 'Please enter primary key:', ui.ButtonSet.OK_CANCEL);
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
Logger.log('The user entered the following primary key:', response.getResponseText());
// Map the header rows in order that column position is not hard-coded.
var sheetURL = '[ENTER YOUR SHEET URL HERE]';
var workSheetName = '[ENTER YOUR WORKSHEET NAME HERE]';
var sheet = SpreadsheetApp.openByUrl(sheetURL).getSheetByName(workSheetName);
var lastColumn = sheet.getLastColumn();
var headerRange = sheet.getRange(1, 1, 1, lastColumn);
var headers = headerRange.getValues();
for (var i=1; i<headers[0].length+1; i++) {
switch (headers[0][i-1]){
case "Primary Key":
var primaryKeyIndex = i;
break;
case "Description":
var descriptionIndex = i;
break;
case "Email":
var emailIndex = i;
break;
case "Status":
var statusIndex = i;
break;
}
}
// Header rows mapped.
// Search for row corresponding to primary key.
var primaryKey = response.getResponseText();
var keyRow = findInColumn(columnToLetter(primaryKeyIndex), primaryKey);
if (keyRow == -1){
ui.alert('Primary Key "'+ primaryKey + '" not found.');
} else {
ui.alert('Primary Key "'+ primaryKey + '" found at row: ' +keyRow+ '.');
sheet.getRange(keyRow, statusIndex).setValue("Approved");
//Prepare Email
var subject = "test";
var email = sheet.getRange(keyRow, emailIndex).getValue();
var body = "Hi, \n\n Your entry with primary key " + primaryKey + " is now approved.";
MailApp.sendEmail(email, subject, body);
}
} else if (response.getSelectedButton() == ui.Button.CANCEL) {
Logger.log('The user clicked cancel.');
} else {
Logger.log('The user clicked the close button in the dialog\'s title bar.');
}
}
// Helper function to find corresponding row to data in column.
function findInColumn(column, data) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var column = sheet.getRange(column + ":" + column); // like A:A
var values = column.getValues();
var row = 0;
while ( String(values[row]) && String(values[row][0]) !== String(data) ) {
row++;
}
if (String(values[row][0]) === String(data))
return row+1;
else
return -1;
}
// Helper function to convert Column Number to Column Letter
function columnToLetter(column){
var temp, letter = '';
while (column > 0)
{
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
I'd suggest that you not use the onEdit trigger for sending email. I think it's over used by many users. If you are, you will have to go with the installable triggers. This is an example email solution that looks pretty clean that came in yesterday.
You can use most of this code below. Modify the email portions to suit your needs.
This code checks for sheet name to be 'Form Responses' and edited column header to be 'Status' as from pics given above.
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var aSheet = ss.getActiveSheet();
// check sheet name
if (aSheet.getName() != 'Form Responses') return;
var range = ss.getActiveRange();
var row = range.getRow();
var col = range.getColumn();
// Logger.log(col);
var headers = aSheet.getRange(1,1,1,aSheet.getLastColumn()).getValues()[0];
// Logger.log(headers[col-1]);
// check column header
if (headers[col-1] != 'Status') return;
var value = range.getValue();
var values = ["approved", "denied", "in progress"]; // values to check for
// check values
if (values.indexOf(value.toString().toLowerCase()) == -1) return;
// Logger.log(row);
var rowValues = aSheet.getRange(row, 1, 1, aSheet.getLastColumn()).getValues()[0];
Logger.log(rowValues);
// change as necessary
var recipient = rowValues[1]; // email is in 2nd column
var body = 'Email body'; // create body
var subject = 'Test'; // set subject
// send email
MailApp.sendEmail(recipient, subject, body);
}
Related
I have a Google Spreadsheet with 4 columns including Products, Salesperson, Category and Status. What I am trying to reach is whenever a user choose Yes option on Status column and if that product category is also G, then sending an email using the MailApp.
The e-mail should include the product value as well.
So far, I was able to sending an email. But I've really confused about the offset concept here and was not able to send the product in the email
The spreadsheet: https://docs.google.com/spreadsheets/d/1wVr0SGryNNvorVdDZEY1E6UDgh25_A5A2LhN1UNbIHE/edit?usp=sharing
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetName = sheet.getName();
var r = sheet.getActiveCell();
var cell = sheet.getActiveCell().getA1Notation();
if(ss.getActiveCell().getColumn() !== 5) return;//
var row = sheet.getActiveRange().getRow();
var cellvalue = sheet.getActiveCell().getValue().toString();
var prod = sheet.getRange().offset(0, -2).getValues();
var cat = cellvalue.offset(0, -1).getValues();
var recipients = "email#email.com";
var message = '';
if(cellvalue === 'Yes' && cat === 'G') {
message = cell + ' in Sheet ' + sheetName + ' was changed to YES.';
var subject = 'Cell Changed to YES';
var body = message + ss.getUrl() + ' to view the changes' + prod;
MailApp.sendEmail(recipients, subject, body);
}
}
It's probably easier to use the object passed with the onEdit event instead of manipulating active cells and offsets.
This event object gives you the new value, so you can check if it is the one that you want ('Yes'). It also gives you the range that was edited, so you can use it to check if the change was in the correct column ('D') and to get the row that was modified. Once you have the row, you can use it to get the values of the other columns ('Products' and 'Cat') in that row.
Something like this should work:
function onEdit(event) {
const statusColumnNumber = 4; // Indices of rows and columns start from 1, so column D is 4.
if (event.range.getColumn() === statusColumnNumber && event.value === 'Yes') {
const sheet = event.range.getSheet();
const rowValues = sheet.getRange(event.range.getRow(), 1, 1, sheet.getLastColumn()).getValues().flat();
const categoryColumnIndex = 2; // Indices of JavaScript arrays start from 0, so column C is in position 2 of the array.
if (rowValues[categoryColumnIndex] === 'G') {
const prodColumnIndex = 0;
const prodValue = rowValues[prodColumnIndex];
const recipients = "email#email.com";
const subject = 'Cell Changed to YES';
const message = event.range.getA1Notation() + ' in Sheet ' + sheet.getName() + ' was changed to YES. '
const body = message + '\n' + sheet.getParent().getUrl() + ' to view the changes for ' + prodValue;
MailApp.sendEmail(recipients, subject, body);
}
}
}
As mentioned in the comments of your question, since you use a service that needs permissions (MailApp), you'll need to create an installable trigger that calls this function.
Hi I am doing some codes in google script but the output that I am expecting did not happen.
Here is the code that i came up with hope you can help me solve this problem,
function sendEmail() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = activeSheet.getLastRow();
var dRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("March Cycle").getRange("I6")
var rData = dRange.getValue();
var templateTxt = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1,1).getValue();
for (var i = 6;i<=lr;i++) {
if(rData = "Touch Course Completed") {
var frstname = activeSheet.getRange(i,4).getValue();
var lstname = activeSheet.getRange(i,3).getValue();
var gradelvl = activeSheet.getRange(i,5).getValue();
var msgbody = templateTxt.replace("{name of student}",(frstname + " " + lstname)).replace("{Gr Lvl}",gradelvl);
MailApp.sendEmail("email add","Test Email",templateTxt);
}
Logger.log(msgbody);
}
}'
below is the data of picture that i wanted to automate.
enter image description here
Thank you
There are a couple of mistakes you have on your code. Let me show you in my comments on this working code.
Code:
function sendEmail() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var templateTxt = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
var lr = activeSheet.getLastRow();
// Compile list of students with "Touch Course Completed" in array before sending
var msgbody = [];
// You need to get the range of all data on column I for it to be optimized
var dRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("March Cycle").getRange("I6:I"+lr);
var rData = dRange.getValues();
// rData is equal to [[Touch Course Completed],[Touch Course Completed],[Not completed]]
// Loop all column I data
rData.forEach(function (data, i){
// Since rData is 2D array, the cell should be accessed by data[0]
if (data[0] == "Touch Course Completed") {
// i is the index of the rData array per loop, so 0 is equivalent to row 6
// We need to offset it to get the correct row
var frstname = activeSheet.getRange(i + 6, 4).getValue();
var lstname = activeSheet.getRange(i + 6, 3).getValue();
var gradelvl = activeSheet.getRange(i + 6, 5).getValue();
// My idea here is optional, but I prefer sending it on 1 email instead of separate per student
// That way, we do it faster and more efficient (note that there are quota/limits on sending mail thus doing this is better)
// But if you need it separate, then do what you did in your script
// I push all message first, then send as bulk outside the loop with join
msgbody.push(templateTxt.replace("{name of student}", (frstname + " " + lstname)).replace("{Gr Lvl}", gradelvl));
}
});
// Send an email only IF there is a "Touch Course Complete" student
if(msgbody)
MailApp.sendEmail("email", "Test Email", msgbody.join("\n"));
}
March Cycle:
Template:
Email:
Note:
Note that the earlier tests did send the email separately. If you need them separate then send it every loop (no need for arrays). But if not, then the code above should be better.
EDIT:
To only send the last row, there can be 2 approach that comes to mind:
Add a column for the identification of the data if it was already sent
You will need to add a column.
If new data doesn't contain "Touch Course Completed", it will not send anything.
If you added multiple rows that contains "Touch Course Completed", all of them will be sent.
Get the last row of msgbody
You will not need to add a column.
If new data doesn't contain "Touch Course Completed", it will send the last row with "Touch Course Completed" that was already sent before.
This will not send multiple rows if ever you added more than 1 "Touch Course Completed"
First approach:
function sendEmail() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var templateTxt = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Template").getRange(1, 1).getValue();
var lr = activeSheet.getLastRow();
var msgbody = [];
// Get all headers, check if there is "Already Sent" header
var lc = activeSheet.getLastColumn();
var headers = activeSheet.getRange(1, 1, 1, lc).getValues();
var sentColumn;
if(!headers[0].includes("Already Sent")){
// If not found, add header "Already Sent" right to the last column
sentColumn = lc + 1;
activeSheet.getRange(1, sentColumn).setValue("Already Sent");
}
else {
// If found, get column number of existing "Already Sent" header
sentColumn = headers[0].indexOf("Already Sent") + 1;
}
var dRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("March Cycle").getRange("I6:I"+lr);
var rData = dRange.getValues();
rData.forEach(function (data, i){
if (data[0] == "Touch Course Completed") {
var frstname = activeSheet.getRange(i + 6, 4).getValue();
var lstname = activeSheet.getRange(i + 6, 3).getValue();
var gradelvl = activeSheet.getRange(i + 6, 5).getValue();
// Check if column is already populated with "Y"
var isSent = activeSheet.getRange(i + 6, sentColumn).getValue();
if(isSent != "Y"){
// If column value is not "Y", then add row to the email to be sent, also put "Y" on the column after
msgbody.push(templateTxt.replace("{name of student}", (frstname + " " + lstname)).replace("{Gr Lvl}", gradelvl));
activeSheet.getRange(i + 6, sentColumn).setValue("Y");
}
}
});
// Modified condition for checking array
if(msgbody.length > 0)
MailApp.sendEmail("email", "Test Email", msgbody.join("\n"));
}
Sample data:
Output:
Email:
Note:
The script will automatically add/locate the header, so no need to adjust your sheet manually.
But you can still initialize a column to become "Already Sent" by writing the header name on the first row of the column and write "Y" to those rows you don't want to be sent anymore.
I am using a snapData script sourced externally and shown below:
function snapData() {
// get current sheet and tabs
var ss = SpreadsheetApp.getActiveSpreadsheet(); var current = ss.getSheetByName('Fees (management)'); var database = ss.getSheetByName('Fees (data)');
// count rows to snap var current_rows = current.getLastRow();
var database_rows = database.getLastRow() + 1;
var database_rows_new = current_rows + database_rows - 2;
var rows_new = current.getRange("A2:F" + current_rows).getValues();
// snap rows, can run this on a trigger to be timed
database.getRange("A" + database_rows + ":F" + database_rows_new).setValues(rows_new);
}
At the moment, it is triggered by pressing the 'Run' button, however I would like to have the script triggered by a new row appearing on another tab.
Help with this would be appreciated. Thanks!
"I would like to have the script triggered by a new row appearing on another tab."
By using the installable trigger onChange, it is possible to trigger the script as planned. source is an Event Object returned by onChange, though it is not included in the documnentation.
var sheetname = src.getActiveSheet().getSheetName(): get the sheet name
var ctype = e.changeType;: get the change type
if (sheetname === "target" && ctype === "INSERT_ROW"){: test if the type was inserting a row on "target"
function so5899606501(e) {
// return all the event objects
Logger.log(JSON.stringify(e));
// get the source
var src = e.source;
var spreadsheetname = src.getName();
var sheetname = src.getActiveSheet().getSheetName()
Logger.log("spreadsheet = "+spreadsheetname+", sheet name = "+sheetname);
var currentcell = src.getCurrentCell();
Logger.log("the current cell = "+currentcell.getA1Notation());
// get the change type
var ctype = e.changeType;
Logger.log("the change type is "+ctype);
// test for the type of change
if (ctype == "INSERT_ROW"){
Logger.log("A row was just inserted");
}
else
{
Logger.log("the change type wasnt a new row, it was "+ctype)
}
//test for the sheet where the change took place
if (sheetname == "target"){
Logger.log("the change took place on the sheet named 'target'");
}
else
{
Logger.log("the change took place on the sheet named" +sheetname+".")
}
// test for the change type AND the sheet name
if (sheetname == "target" && ctype == "INSERT_ROW"){
Logger.log("Eureka!. A new row was inserted on the sheet named 'target. Lets do stuff");
}
else{
Logger.log("Sigh. It either wasn't a new row AND/OR it wasn't on the sheet named 'target'. Either way, we can ignore it.");
}
}
Google App script not firing all JavaScript pop up boxes using on edit trigger. Does not fire when a change is made to the sheet for multiple users. Also only works when it feels like it. I have set the trigger to onEdit(). My code:
function Error() {
var sheet = SpreadsheetApp.getActiveSheet();
if (sheet.getName() == "Protocal Check List 2"){
var ui = SpreadsheetApp.getUi(); // Same variations.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var active = ss.getActiveCell();
var getactive = ss.getActiveCell().getDisplayValue();
var column = ss.getActiveRange().getColumn();
var row = ss.getActiveRange().getRow();
var msg = '';
var section = sheet.getRange('C'+ row);
switch (column) {
case 9:
var msg = "error 1";
break;
case 10:
var msg = "Error 2";
break;
default:
msg = "Error";
break;
}
if(getactive == "TRUE"){
if(column >= 9 && column <= 60
){
var result = ui.alert(
"pika Says",
msg,
ui.ButtonSet.YES_NO);
// Process the user's response.
if (result == ui.Button.YES) {
// User clicked "Yes".
window.alert('Task Complete: \n\n' + msg);
active.setValue('True');
} else {
var i = 0;
while (i < 1) {
var result = ui.prompt(
'Please detail cause of the problem:',
ui.ButtonSet.OK);
var text = result.getResponseText();
var textcount = text.length;
if(textcount > 0) {
i++;
}}
var cell = "H" + row;
var emailAddress = "email#gmail.com";
var d = new Date();
var n = d.toDateString();
var t = d.toTimeString();
var staffname = ss.getRange(cell).getValues();
var message = "Date: " + n +"\n\nTime: " + t +"\n\nStaff: " + staffname + "\n\nError: " + msg + "\n\nProblem: " + text;
var subject = msg;
var thedate = n + " / " + t;
ss.getRange('A1').setValue(thedate);
ss.getRange('B1').setValue(staffname);
ss.getRange('C1').setValue(msg);
ss.getRange('D1').setValue(text);
var s1 = ss.getRange('A1:D1'); //assign the range you want to copy
var s1v = s1.getValues();
var tss = SpreadsheetApp.openById('1mOzdRgKxiP5iB9j7PqUWKKo5oymWuAeQZ1jJ1s6qL9E'); //replace with destination ID
var ts = tss.getSheetByName('AB Protocal'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1, 1,4).setValues(s1v); //you will need to define the size of the copied data see getRange()
MailApp.sendEmail(emailAddress, subject, message);
// User clicked "No" or X in the title bar.
//ui.alert("The following note has been sent to the Duty Manager: \n\n" + text);
active.setValue('FALSE');
}}}}
}
Any help would be appreciated. I need it to run every signal time for users who have access to the sheet.
You'll have to
assume that onEdit triggers are best-effort, but may not catch all
edits done to the spreadsheet.
Bug thread is here.
To get around the bug as mentioned by Edward implemente a dumb router.
// create a new file -> router.gs
function routerOnEdit(e) {
myOnEditHook1(e);
myOnEditHook2(e);
myOnEditHook3(e);
}
I have submitted an issue, which is the updated version of a long-running 7 year issue. They closed it in 2022, saying the feature is intended. They have now added a feature where onEdit can queue up to 2 trigger events. The issue I'm having is that the second edit still does not trigger. Despite what the documentation says.
Note: The onEdit() trigger only queues up to 2 trigger events.
View the new issue here: https://issuetracker.google.com/issues/221703754
In an attempt to automate some of my work, I am beginning to learn basics of google scripts.
I have a spreadsheet in which I want to send an email notification when data is input into one column or another. There are also two tabs within this spreadsheet in which I would like this to occur.
The current result from the script is an email on the second 'sendnotification' function.
Question: How do I get the script to consider both functions?
I know this code can be condensed by likely using an IF fucntion in a better way but I am at a loss.
For some context: This is used in a manufacturing operation. The production is done by an offsite company and when they enter quantity into column 10 on either sheet, I want it to send a group of people an email that I work with. Similarly, when the product quality testing is done I want to be able to input into Column 12 and it send the offsite company an email.
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
//Define Notification Details
var recipients = "ryan.helms#company.com";
var subject = "Disc production was entered on the "+ss.getName();
var body = ss.getName() + " has been updated with an amount produced. Visit " + ss.getUrl() + " to view the quantities entered.";
//Check to see if column is A or B to trigger
if (cellcol == 10)
{
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
//End sendNotification
}
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get Active cell
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
//Define Notification Details
var recipients = "ryan.helms#company.com";
var subject = "A lot of disc has been RELEASED by XYZ Company";
var body = ss.getName() + " has been updated with a lot of disc that were released by XYZ Company. Visit " + ss.getUrl() + " to view this updated information.";
//Check to see if column is A or B to trigger
if (cellcol == 12)
{
//Send the Email
MailApp.sendEmail(recipients, subject, body);
}
//End sendNotification
}
You'll need to set up an onEdit installable trigger (if you haven't already) and assign it to the following function:
function onEditEmailSender(e) {
var sheetName = e.range.getSheet().getName();
if (sheetName === "tab1" || sheetName === "tab2") {
var col = e.range.getColumn();
if (col === 10)
//send col 10 email
else if (col === 12)
//send col 12 email
}
}
The onEdit trigger passes a parameter with all sorts of information, the most useful in your case being e.range. This Range object corresponds to the cell that was edited to trigger the onEdit event. You can use it to get the name of the sheet (what you call tab) and the column that was edited.
Using that information you can send the appropriate email. Good luck!