I have a script to select an email address depending on the result of a dropdown selector on a form which is working as intended.
But I have another dropdown which if "08-Maintenance" is selected I want it to override the email selector. This bit doesn't work. Any help greatly appreciated.
//select THIS spreadsheet
var LogSheet = SpreadsheetApp.getActiveSpreadsheet();
//select correct worksheet
var sheet = LogSheet.getSheetByName("LOG");
//declare the row to look at (the last row)
var lastRowNumber = sheet.getDataRange().getNumRows();
//declare and obtain the variable for the Source Code in column 3
var SourceValue = sheet.getRange(lastRowNumber, 3).getValue();
//declare and obtain the variable for the Location Code in column 4
var LocationValue = sheet.getRange(lastRowNumber,4).getValue();
//declare and obtain the variable for the Hazard being reported in column 6
var HazardValue = sheet.getRange(lastRowNumber,6).getValue();
//declare the correct email addresses to use
var email01 = LogSheet.getSheetByName("LocationCodes").getRange(3, 5).getValue();
var email02 = LogSheet.getSheetByName("LocationCodes").getRange(4, 5).getValue();
var email03 = LogSheet.getSheetByName("LocationCodes").getRange(5, 5).getValue();
var email04 = LogSheet.getSheetByName("LocationCodes").getRange(6, 5).getValue();
var email05 = LogSheet.getSheetByName("LocationCodes").getRange(7, 5).getValue();
var email06 = LogSheet.getSheetByName("LocationCodes").getRange(8, 5).getValue();
var email07 = LogSheet.getSheetByName("LocationCodes").getRange(9, 5).getValue();
var email08 = LogSheet.getSheetByName("LocationCodes").getRange(10, 5).getValue();
var email09 = LogSheet.getSheetByName("LocationCodes").getRange(11, 5).getValue();
var email10 = LogSheet.getSheetByName("LocationCodes").getRange(12, 5).getValue();
var email11 = LogSheet.getSheetByName("LocationCodes").getRange(13, 5).getValue();
var email12 = LogSheet.getSheetByName("LocationCodes").getRange(14, 5).getValue();
var email13 = LogSheet.getSheetByName("LocationCodes").getRange(15, 5).getValue();
var email14 = LogSheet.getSheetByName("LocationCodes").getRange(16, 5).getValue();
var email15 = LogSheet.getSheetByName("LocationCodes").getRange(17, 5).getValue();
var email16 = LogSheet.getSheetByName("LocationCodes").getRange(18, 5).getValue();
var email17 = LogSheet.getSheetByName("LocationCodes").getRange(19, 5).getValue();
var email18 = LogSheet.getSheetByName("LocationCodes").getRange(20, 5).getValue();
var email19 = LogSheet.getSheetByName("LocationCodes").getRange(21, 5).getValue();
var email20 = LogSheet.getSheetByName("LocationCodes").getRange(22, 5).getValue();
//declare the correct LocationCodes to check
var depot01 = LogSheet.getSheetByName("LocationCodes").getRange(3, 4).getValue();
var depot02 = LogSheet.getSheetByName("LocationCodes").getRange(4, 4).getValue();
var depot03 = LogSheet.getSheetByName("LocationCodes").getRange(5, 4).getValue();
var depot04 = LogSheet.getSheetByName("LocationCodes").getRange(6, 4).getValue();
var depot05 = LogSheet.getSheetByName("LocationCodes").getRange(7, 4).getValue();
var depot06 = LogSheet.getSheetByName("LocationCodes").getRange(8, 4).getValue();
var depot07 = LogSheet.getSheetByName("LocationCodes").getRange(9, 4).getValue();
var depot08 = LogSheet.getSheetByName("LocationCodes").getRange(10, 4).getValue();
var depot09 = LogSheet.getSheetByName("LocationCodes").getRange(11, 4).getValue();
var depot10 = LogSheet.getSheetByName("LocationCodes").getRange(12, 4).getValue();
var depot11 = LogSheet.getSheetByName("LocationCodes").getRange(13, 4).getValue();
var depot12 = LogSheet.getSheetByName("LocationCodes").getRange(14, 4).getValue();
var depot13 = LogSheet.getSheetByName("LocationCodes").getRange(15, 4).getValue();
var depot14 = LogSheet.getSheetByName("LocationCodes").getRange(16, 4).getValue();
var depot15 = LogSheet.getSheetByName("LocationCodes").getRange(17, 4).getValue();
var depot16 = LogSheet.getSheetByName("LocationCodes").getRange(18, 4).getValue();
var depot17 = LogSheet.getSheetByName("LocationCodes").getRange(19, 4).getValue();
var depot18 = LogSheet.getSheetByName("LocationCodes").getRange(20, 4).getValue();
var depot19 = LogSheet.getSheetByName("LocationCodes").getRange(21, 4).getValue();
var depot20 = LogSheet.getSheetByName("LocationCodes").getRange(22, 4).getValue();
//send the email to the 'recipient'
//if SourceValue is recorded as 08 - Mantenance Request System, the recipient is engineering.support#websiteaddress
//otherwise check location code and send to corresponding H&S Co-ordinator's email address
if (SourceValue == "08 - Mantenance Request System") {
var recipient = "engineering.support#websiteaddress";
//Start with first depot in list
} else if (LocationValue == depot01) {
var recipient = email01;
} else if (LocationValue == depot02) {
var recipient = email02;
} else if (LocationValue == depot03) {
var recipient = email03;
} else if (LocationValue == depot04) {
var recipient = email04;
} else if (LocationValue == depot05) {
var recipient = email05;
} else if (LocationValue == depot06) {
var recipient = email06;
} else if (LocationValue == depot07) {
var recipient = email07;
} else if (LocationValue == depot08) {
var recipient = email08;
} else if (LocationValue == depot09) {
var recipient = email09;
} else if (LocationValue == depot10) {
var recipient = email10;
} else if (LocationValue == depot11) {
var recipient = email11;
} else if (LocationValue == depot12) {
var recipient = email12;
} else if (LocationValue == depot13) {
var recipient = email13;
} else if (LocationValue == depot14) {
var recipient = email14;
} else if (LocationValue == depot15) {
var recipient = email15;
} else if (LocationValue == depot16) {
var recipient = email16;
} else if (LocationValue == depot17) {
var recipient = email17;
} else if (LocationValue == depot18) {
var recipient = email18;
} else if (LocationValue == depot19) {
var recipient = email19;
} else if (LocationValue == depot20) {
var recipient = email20;
} else {
//and send to Engineering Support if there is no code as a catch all.
var recipient = "engineering.support#websiteaddress"; //CHANGE TO ENG SUPP
}
//with the 'LocationCode' & 'SourceCode' as the subject
var subject = SourceValue ;
//then write the'body' of the email including the link to see the changes
var body = 'A new [' + SourceValue + '] log entry has been recorded at [' + LocationValue + '], listed as [' + HazardValue + '], please click the link > > http://goo.gl/shortcode < < to view details.';
{
//then send the email
MailApp.sendEmail(recipient,subject,body);
}
I have changed the website address and goo.gl short code for obvious reasons!
It looks like your comparison value is misspelled:
Should it be Maintenance instead of Mantenance:
if (SourceValue == "08 - Mantenance Request System") {
var recipient = "engineering.support#websiteaddress";
//Start with first depot in list
} else if (LocationValue == depot01) {
var recipient = email01;
And I'd use triple equal signs for comparison:
if (SourceValue === "08 - Maintenance Request System") {
You can search a post like this one for the difference:
Stack Overflow Link
Related
function myFunction() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Welcome");
var targetSheet = ss.getSheetByName("Done");
var startRow = 2;
var lr = sheet.getLastRow();
var dataRange = sheet.getRange(startRow, 1, lr-1, 6);
var data = dataRange.getValues();
var colNumber = sheet.getLastColumn()-1;
var delRows = [];
for (var i = 0; i < data.length; i++) {
var row = data[i];
var id = row[0];
var emailAddress = row[1];
var date = row[2];
var city = row[3];
var bccmail = row[6];
var Sender = 'XXXXXX';
var reply = 'xxxxxx#xxxxxxx.com';
if (emailAddress.match('#') === null){
continue;
};
var subject = row[4];
var message = "Hey " + id + ", welcome in the team " + row[5];
MailApp.sendEmail(emailAddress, subject, message, {bcc: bccmail,name: Sender,replyTo: reply});
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
var sourceRange = sheet.getRange(i+startRow, 1, 1, colNumber);
sourceRange.copyTo(targetRange);
delRows.push(i+startRow);
}
delRows.reverse().forEach(ri=>{sheet.deleteRow(ri)});
Almost all the script works fine. When it comes to sendEmail, I have tried to follow these guidelines and use sendEmail(recipient, subject, body, options). 2 out of 3 options work fine but BCC doesn't work at the moment. Do you know what I am doing wrong? Can BCC be a variable?
The problem is in this line:
var bccmail = row[6];
dataRange is defined as a range with only 6 columns. data is a 2D array with the values of dataRange. row is a 1D array with a single row of data. JavaScript array indexes only start at 0, so the values are in row[0] to row[5].
Please check your sheet in which column does the bcc string is defined and count the index from 0.
Reference:
Arrays in JavaScript
I am trying to make an inventory sheet where an automated email will be sent out when the inventory falls below a specific limit. I have set it so B2 is < C2, but I am not getting an email.
function sendEmailAlert() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var rangeA = sheet.getRange('A2:A8');
var item = rangeA.getValues();
var rangeB = sheet.getRange('B2:B8');
var inventory = rangeB.getValues();
var rangeC = sheet.getRange('C2:C8');
var limit = rangeC.getValues();
var toEmail = 'XX#XX.com';
var subject = 'Inventory to Order';
var body = 'Item:' + item + 'needs to be ordered';
for (i in item){
if(inventory <= limit ) {
MailApp.sendEmail(toEmail,subject, body);
}
}
}
Try this:
function sendEmailAlert() {
var sheet=SpreadsheetApp.getActiveSpreadsheet();
var rangeA=sheet.getRange('A2:A8');
var item=rangeA.getValues();
var rangeB=sheet.getRange('B2:B8');
var inventory=rangeB.getValues();
var rangeC=sheet.getRange('C2:C8');
var limit=rangeC.getValues();
var toEmail='XX#XX.com';
var subject='Inventory to Order';
for (var i=0;i<item.length;i++){
if(inventory[i][0]<=limit[i][0]) {
var body='Item:' + item[i][0] + 'needs to be ordered';
MailApp.sendEmail(toEmail,subject, body);
}
}
}
You could also add something like this if you want to avoid sending duplicates
function sendEmailAlert() {
var sheet=SpreadsheetApp.getActiveSpreadsheet();
var rangeA=sheet.getRange('A2:A8');
var item=rangeA.getValues();
var rangeB=sheet.getRange('B2:B8');
var inventory=rangeB.getValues();
var rangeC=sheet.getRange('C2:C8');
var limit=rangeC.getValues();
var rangeD=sheet.getRange('D2:D8');
var sent=rangeD.getValues();
var toEmail='XX#XX.com';
var subject='Inventory to Order';
for (var i=0;i<item.length;i++){
if(inventory[i][0]<=limit[i][0] && sent[i][0]!="sent") {
var body='Item:' + item[i][0] + 'needs to be ordered';
MailApp.sendEmail(toEmail,subject, body);
sent[i][0]="sent";
}
}
rangeD.setValues(sent);
}
Im trying to send multiple attachments using Google Spreadsheet but I get the following error "Cannot retrieve the next object: iterator has reached the end."
It does work, because it sends the e-mail to the first email-address in the list, but it fails for the second one.
I have seen similar questions here about it, but no solutions resolve my issue.
Here is the script:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1;
var numRows = 2;
var dataRange = sheet.getRange(startRow, 1, numRows, 2);
var file1 = DriveApp.getFilesByName('Maandbrief December.pdf');
var file2 = DriveApp.getFilesByName('Weekendbrief 7-9 december.pdf');
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0];
var message = row[1];
var subject = "Maandbrief December en weekendbrief 7-9 december";
MailApp.sendEmail({to:emailAddress, subject:subject, body:message, attachments: [file1.next(), file2.next()]})
}}
Thank you for your help.
Try this:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1;
var numRows = 2;
var dataRange = sheet.getRange(startRow, 1, numRows, 2);
var files1 = DriveApp.getFilesByName('Maandbrief December.pdf');
while(files1.hasNext()){var file1=files1.next();}
var files2 = DriveApp.getFilesByName('Weekendbrief 7-9 december.pdf');
while(files2.hasNext()){var file2=files2.next();}
var data = dataRange.getValues();
for (var i=0;i<data.length;i++) {
var row = data[i];
var emailAddress = row[0];
var message = row[1];
var subject = "Maandbrief December en weekendbrief 7-9 december";
MailApp.sendEmail({to:emailAddress, subject:subject, body:message, attachments: [file1, file2]})
}
}
or this:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1;
var numRows = 2;
var dataRange = sheet.getRange(startRow, 1, numRows, 2);
var n1=0;
var n2=0;
var files1 = DriveApp.getFilesByName('Maandbrief December.pdf');
while(files1.hasNext()){var file1=files1.next();n1++;}
var files2 = DriveApp.getFilesByName('Weekendbrief 7-9 december.pdf');
while(files2.hasNext()){var file2=files2.next();n2++}
var data = dataRange.getValues();
if(n1==1 && n2==1){
for (var i=0;i<data.length;i++) {
var row = data[i];
var emailAddress = row[0];
var message = row[1];
var subject = "Maandbrief December en weekendbrief 7-9 december";
MailApp.sendEmail({to:emailAddress, subject:subject, body:message, attachments: [file1, file2]});
}
}else{
throw('More than one file with given name.');
}
}
First code is what I am currently using, I am trying to get my email address to reference in a cell. I've found another script that does this but when I try to combine them, it doesn't work.
function getGoogleSpreadsheetAsExcel(){
try {
var ss = SpreadsheetApp.getActive();
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key="
+ ss.getId() + "&exportFormat=xlsx";
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(ss.getName() + ".xlsx");
MailApp.sendEmail("Email Address Here", "Subject Here", "Body of Email
Here", {attachments: [blob]});
} catch (f) {
Logger.log(f.toString());
}
}
And trying to combine this to it to reference cells for me instead of edited the script.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('Email Test'))
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Again both work on there own, but wont work when combined.
This is what I have compiled and it does send the email and attachement, but just to myself, no other recipients. Needing at least it to go to 6 other people at once.
function getGoogleSpreadsheetAsExcel(){
try {
var ss = SpreadsheetApp.getActive();
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key="
+ ss.getId() + "&exportFormat=xlsx";
var params = {
method : "get",
headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
};
var blob = UrlFetchApp.fetch(url, params).getBlob();
blob.setName(ss.getName() + ".xlsx");
var sheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('Email Test'))
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message, {attachments: [blob]});
}
}
catch (f) {
Logger.log(f.toString());
}
}
I'm still very new to scripting and can't work out why the below script doesn't work
function sendReturnEmail(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
if(event.range.getA1Notation().indexOf("K") && sheet.getRange("K" + row).getDisplayValue() == "Approved")
{
var rowVals = getActiveRowValues(sheet);
var aliases = GmailApp.getAliases();
var to = rowVals.user;
var subject = "Allocation request approved";
var body = "Your allocation request of " +rowVals.quantity + " cases on " + rowVals.date + " from " + rowVals.depot + " has been approved. <br \> <br \>"
+ "Regards, <br \> <br \>" +rowVals.analyst + "<br \> CPF Team";
Logger.log(aliases);
GmailApp.sendEmail(to,subject,body,
{from: aliases[0]})
;
}}
function getActiveRowValues(sheet){
var cellRow = sheet.getActiveRange().getRow();
// get depot value
var depotCell = sheet.getRange("E" + cellRow);
var depot = depotCell.getDisplayValue();
// get date value
var dateCell = sheet.getRange("F" + cellRow);
var date = dateCell.getDisplayValue();
// get quantity value
var quantCell = sheet.getRange("G" + cellRow);
var quant = quantCell.getDisplayValue();
// return an object with your values
var analystCell = sheet.getRange("J" + cellRow);
var analyst = analystCell.getDisplayValue();
var userCell = sheet.getRange("O" + cellRow);
var user = userCell.getDisplayValue();
return {
depot: depot,
date: date,
quantity: quant,
analyst: analyst,
user: user
} }
}
The sheet is question has several scripts running in it, one of them being this:
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Allocation Requests" ) { //checks that we're on the correct sheet
var r = s.getActiveCell();
if( r.getColumn() == 2) { //checks the column
var nextCell = r.offset(0, 13);
nextCell.setValue(Session.getEffectiveUser());
}
}
I now want the sheet to send an email to the email address captured using the above script (which works fine) when the corresponding cell in column K is manually changed to "Approved."
Can anyone see what I'm doing wrong. I get no errors when it runs, but I'm not getting an email. I've searched other questions but none have helped. Many thanks in advance.