I have a function set up to look at a tab within a sheet, check for certain conditions and then send an email if those conditions are met. The body of the email is set up with certain variables that are comprised of columns from the sheet. The function seems to be working fine, but I'm receiving a warning from Apps Script about using "getValues()" instead of "getValue()" and minimizing calls to servers.
I've tried to grab the range of the spreadsheet into an array, but then I'm not sure how I can go ahead and define variables based off of individual columns within that array.
This is the code that I currently have. If it matters, I also have a separate function setup with this spreadsheet to move rows from one tab to another.
function emailNotification(){
var nowH=new Date().getHours();
var nowD=new Date().getDay();
if((nowD > 0 && nowD <6) && (nowH > 9.5 && nowH < 5.5)) {
var ss = SpreadsheetApp.openByUrl('spreadsheet url');
var dataSheet = ss.getSheetByName("tab name");
for (var i = 2; i <= dataSheet.getLastRow(); i++) {
var firstName = dataSheet.getRange(i, 5).getValue();
var lastName = dataSheet.getRange(i, 6).getValue();
var recipient = dataSheet.getRange(i, 3).getValue();
var isHired = dataSheet.getRange(i, 1).getValue();
var orgName = dataSheet.getRange(i, 11).getValue();
var emailSent = dataSheet.getRange(i, 18).getValue();
var body = "Hello" + "," + "\n" + "\n" + firstName + "
"+lastName + " " + "has been added to the"+ " " + orgName
+ "." + "\n" + "\n" + "Best," + "\n" + "Me";
var htmlText = body.replace(/\n/g,'<br\>');
var subject = "New Hire";
if (isHired == "Yes" && emailSent != "Email Notification
Sent" ) {
MailApp.sendEmail(recipient, "email", subject, body)
MailApp.sendEmail(recipient, subject, body,{
htmlBody : htmlText})
// MARK THE ROW AS COMPLETED
dataSheet.getRange(i, 18).setValue("Email Notification
Sent");
}
}
}
This is the message I receive:
Method Range.getValue is heavily used by the script File: Code Line: 57
Any assistant would be kindly appreciated.
Try this:
Your missing the recipient and I would use SpreadsheetApp.openById();
function emailNotification() {
var nowH=new Date().getHours();
var nowD=new Date().getDay();
if(nowD>0 && nowD<6 && nowH>9 && nowH<5) {//integers only
var ss=SpreadsheetApp.openByUrl('spreadsheet url');//need url
var sh=ss.getSheetByName("tab name");//need sheet name
var vA=dataSheet.getRange(2,1,sh.getLastRow(),sh.getLastColumn()).getValues();
for (var i=0;i<vA.length;i++) {
var recipient=vA[i][?];//no recipient defined
var firstName=vA[i][4];
var lastName=vA[i][5];
var recipient=vA[i][2];
var isHired =vA[i][1];
var orgName =vA[i][2=10];
var emailSent =vA[i][17];
var html=Utilities.formatString('Hello,<br /><br /> %s %s has been added to the %s.<br /><br />Best, <br />Me',firstName,lastName,orgName);
var subject="New Hire";
if (isHired=="Yes" && emailSent != "Email Notification Sent" ) {
MailApp.sendEmail(recipient, subject,'',{htmlBody:html});
sh.getRange(i+2,18).setValue("Email Notification Sent");
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.