I have been looking how to write the script to check and run on multiple tabs. What I have now, I would need to copy/paste and set execution triggers one by one.
Script I have:
function SendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("1");
var value = sheet.getRange("F3").getValue();
var subject = "subject";
var message = "message";
if (value >0){
MailApp.sendEmail("email", subject, message);
}
}
I have this running every 8 hours (trigger run).
I am looking to add more tabs so that for each tab the script can check and send an email if the value of individual tabs is more than 0.
Any ideas on how to do this?
The value in every tab is under F3. Tab names, for example, 1,2,3,4...
Thank you!!!
This should do what you looking for if I understood the issue correctly.
function SendEmail() {
//Get the Spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
//Get all Sheets in the Spreadsheet
var sheets = ss.getSheets();
//Create a variable for the email address, subject and message
var email = "";
var subject = "";
var message = "";
//Can change the starting sheet by changing the value of "i" - 0 will be the first sheet.
for (var i = 0; i < sheets.length; i++){
var sheet = sheets[i];
var value = sheet.getRange("F3").getValue();
switch(sheet.getSheetName) {
case "Sheet1":
email = "email1#domain.com";
break;
case "Sheet2":
email = "email2#domain.com";
break;
default:
value = 1
email = "yourEmailAddress#domain.com";
subject = "Message Failed to Send";
message = "There was an error sending email, please investigate";
}
if (value >0){
MailApp.sendEmail(email, subject, message);
}//END OF IF STATEMENT
}//END OF FOR LOOP
}//END OF FUNCTION
Related
I'm new to g-scripts and facing some issues in coming up with the code for the below. My goal is:
Want to send an automatic email to specific people once the information in any of the rows in the google sheet are edited.(note: the range is the whole sheet)
For each row in the google sheet, there's an email address next to it (under a new column) which specifies the person this automatic email should be sent to.
Can the changes be compiled once per day and sent via 1 email per email address?
Thank you in advance for your help.
Currently, this is my code:
function onEdit(e){
var rows = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1").getRange(30000,30).getValue();
var headerRow = rows.shift();
var editedRow = e.range.getRow();
var template = HtmlService.createTemplateFromFile("Template1.html");
template.headerRow = headerRow;
template.editedRow = editedRow;
template.rows = rangeValues;
var html = template.evaluate().getContent();
function sendEmails(){
var ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1").activate();
var ss=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr=ss.getLastRow();
//getting email body:
var sheet2Text = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet2").getRange(1,1).getValue();
// Logger.log(emailText);
var quotaLeft = MailApp.getRemainingDailyQuota();
Logger.log(quotaLeft);
if((lr-1)>quotaleft){
Browser.msgBox("You have"+quotaLeft+"left & you're trying to send"+(lr-1)+"emails. Emails not sent");
} else{
for(var i=2; i<lr; i++){
var currentEmail = ss.getRange(i,26).getValue();
var currentClientName = ss.getRange(i,1).getValue();
var currentTeamName = ss.getRange(i,27).getValue();
var messageBody = sheet2Text.replace("{name}",currentTeamName);
var subjectLine = "notification on" + currentClientName + "process list", html;
MailApp.sendEmail(currentEmail, subjectLine, messageBody);
//26 is the column number that contains the emails in the google sheet
} //closing the for loop
} // closing the else loop
} // closing the function sendEmails
} // closing the onEdit function
I'm having trouble with the "Email _Sent" text. It is supposed to appear next to the recipient who received the email from my spreadsheet. However, when I select certain emails to send the message to, the text "Email _Sent" starts filling out the first empty rows in my spreadsheet. In this way, the text appears beside some recipients who didn't receive the message.
I'll post the script that I'm using:
function sendGeneralEmail() {
var Email = 4;
var Name = 3;
var emailSent = 6;
var subject = "Sample Analysis Service"
var html = HtmlService.createTemplateFromFile("SUM.1");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var data = ws.getRange("A5:G" + ws.getLastRow()).getValues();
data = data.filter(function(r){ return r[5] == true});
var Email_Sent = 'Email_Sent';
var sheet = SpreadsheetApp.getActiveSheet();
var notBlank = sheet.getRange("D5:G5");
var lastRow = notBlank.getLastRow();
data.forEach(function(row){
html.en = row[Name];
var htmlMessage = html.evaluate().getContent();
if(emailSent !=Email_Sent) {
GmailApp.sendEmail(
row[Email],
subject, "Your Email doesn't support HTML", {
name: "MASAR Team",
htmlBody: htmlMessage},
);
sheet.getRange(lastRow, 7, data.length, 1).setValue(Email_Sent);
SpreadsheetApp.flush();
}
});
}
Can someone help me to fix that?
The second issue is that, if I run the script and then stopped and came back and run it again on the same day, the text "Email_sent" stops updating to the spreadsheet. It only appears the first I run the script during the day.
Screenshot
Hope to have a solution to this
I tried to replicate your code and found some issues:
using setValue on range with multiple cells will copy the value to all cells within the range. In your example, all cells in getRange(lastRow, 7, data.length, 1) will have the value "Email_Sent". This causes the unwanted write of "Email_Sent" in your spreadsheet.
if(emailSent !=Email_Sent) will always true because '6' is not always equal to 'Email_Sent'
Based on your example, your goal is to send an email and write "Email_Sent" to the Email Status column of the recipient.
Here, I fixed some part of your code and added features.
Code:
function sendGeneralEmail() {
var subject = "Sample Analysis Service"
// var html = HtmlService.createTemplateFromFile("SUM.1");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var data = ws.getRange("A5:G" + ws.getLastRow()).getValues();
var sheet = SpreadsheetApp.getActiveSheet();
for(var i=0; i<data.length; i++){
var selected = data[i][5];
var emailStatus = data[i][6];
if(selected && emailStatus != "Email_Sent"){
var email = data[i][4];
// html.en = row[Name];
// var htmlMessage = html.evaluate().getContent();
GmailApp.sendEmail(
email,
subject, "Your Email doesn't support HTML", {
name: "MASAR Team",
message: "ABCD",
// htmlBody: htmlMessage},
}
);
sheet.getRange(i+5, 7, 1, 1).setValue("Email_Sent");
}
}
}
Note: I commented the html parsing since I don't have the sample html and replaced the email message with a string.
Before running the script:
After running the script:
Reference:
setValue()
I have tried many different codes to send an email alert when column F in my Google sheet has the value "Yes". I am selecting this value from a drop down. But no emails are sent. I have tried different emails also. Nothing works. This is the code I am trying right now.
function Email(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("FY");
var row = s.getActiveRange().getRow();
var status = s.getRange(row,6).getValue();
var subject = 'subject'; var message = 'body';
var email ="";
if (e.value === "Yes" && e.range.getColumn() === 6) {
GmailApp.sendEmail(email,subject,message);
}
};
You need to use an installed trigger see here to use this on edit script.
Go to 'Edit' -> 'Current project's triggers' and set a trigger as shown below.
Code:
function onEdit(e){
var getRange = e.range;
var getValue = getRange.getValue();
var getCol = getRange.getColumn();
if(getValue == "Yes" && getCol == 6){
var subject = 'subject';
var message = 'body';
var email = "---------------";
MailApp.sendEmail(email, subject, message);
}
}
I'm trying to make a script to send an email every day , after checking in a spreadsheet, two conditions. If the first condition for True , the data to e- mail will be " subject1 ", otherwise, will be " subject2 ".
The script works correctly when I run manually , however, through the trigger, the value I would like to send via email, simply does not appear in the body.
I can not understand what could be wrong if manually , works perfectly .
The trigger is "time-driven/hour time/every 12 hours".
Can anyone help me?
This is the code:
function sendNotificationD() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var cellvalue = ss.getActiveCell().getValue().toString();
var column = ss.getRange('o13');
var cell = sheet.getRange('o13');
var value = column.getValue();
var emailSheet = ss.getSheetByName("totais");
var emails = emailSheet.getRange("q1:s1").getValues();
var range_bill_com = sheet.getRange('o13');
if (range_bill_com.getValue() !='CONGRATS'){
var recipients = emails;
var message = '';
var subject = 'subject1 here';
var body = 'IMPORTANT: ' + '\n\n' + 'your goal is' + ' ' + value;
MailApp.sendEmail(recipients, subject, body);
}
if (range_bill_com.getValue() =='CONGRATS'){
var recipients = emails;
var message = '';
var subject = 'subject2 here';
var body = 'you have reached your goal';
MailApp.sendEmail(recipients, subject, body);
}
};
At this line:
var sheet = ss.getActiveSheet();
The method getActiveSheet() gets the the active sheet that is being displayed in the spreadsheet UI when a user opens it, but when it runs from a time-driven trigger there is no active sheet, so you need to indicate the sheet, you can use getSheetByName() or getSheets() the line should look like this:
var sheet = ss.getSheetByName("Sheet1");
or
var sheet = ss.getSheets()[0]; // Index of the tab, 0 will return the first one
The function will still work if you fire it manually or from a trigger.
I am designing a questionnaire that requires respondents to input their name and a company code that is specific to their employer. I would like to have a script so that a different company will get an email notification depending on which company code is specified. I created a Google Sheet in the Form that lists company codes, corresponding emails, and messages.
I have found a script that does this but I have to tailor it to my own form. Can anyone do this or has a better code?
Link to Demo Form: Here
Link to demo spreadsheet: Here
//using the obtained settings to set up email info
function onOpen(e) {
FormApp.getUi().createMenu('Authorizer').addItem('Authorize', 'authorize').addToUi();
}
//Easily authorize script to run from Form Editor interface
function authorize(){
var respEmail = Session.getActiveUser().getEmail();
MailApp.sendEmail(respEmail,"Form Authorizer", "Your form has now been authorized to send you emails");
}
function setSettings(e){
var response = e.response;
var settings = getSettings();
var emailSubject = settings[1][2];
var emailMsg = settings[1][3];
var formCompany = getResponse(response,"Company Code");
var formName = getResponse(response,"Name");
var emailAddr;
var companycode;
//loop through and grab the selected school from the Settings spreadsheet.
for (i in settings){
if (settings[i][0] === formcompanycode){
school = settings[i][0];
emailAddr = settings[i][1];
}
}
//build email and send to email send function
emailMsg = emailMsg + '\n' + 'Company Code: ' + formSchool + '\n' + 'Name: ' + formName;
mailSendSimple(emailAddr,emailSubject,emailMsg);
}
//function to find the selected company code.
function getResponse(response,formItem){
var selected;
//loop through the form response to find the company code
for (var i = 0; i < response.getItemResponses().length; i++){
if (formItem == response.getItemResponses()[i].getItem().getTitle()){
selected = response.getItemResponses()[i].getResponse();
}
}
return selected;
}
//grabbing general settings from spreadsheet sheet Settings
function getSettings() {
var form = FormApp.getActiveForm();
var ssID = form.getDestinationId();
var ss = SpreadsheetApp.openById(ssID);
var sheet = ss.getSheetByName("Settings");
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var range = sheet.getRange(1,1,lastRow,lastColumn);
var values = range.getValues();
return values;
}
//function to send out mail
function mailSendSimple(emailAddr,emailSub, emailMsg){
MailApp.sendEmail(emailAddr,emailSub, emailMsg); //{cc: ccUser}
}
Partial answer
In formcompanycode used in the line
if (settings[i][0] === formcompanycode){
is not defined.
Remarks
See How can I test a trigger function in GAS?. The technique described there will help you to debug your code and find other errors.