I found a lot of useful stuff in this forum. I am new to GAS and JS coding in general and I cannot find a way to solve something that I am confident it's NBD.
I am attaching the code first:
function emailAlert() {
// today's date information
var today = new Date();
var todayMonth = today.getMonth() + 1;
var todayDay = today.getDate();
var todayYear = today.getFullYear();
// getting data from spreadsheet
var url = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Assignments");
var resultssn = SpreadsheetApp.openByUrl(url).getName();
//Emails addresses
var emailJonDoe = "example#example.com"
var emailEmilyDoe = "example#example.com"
var duedatesRange = sheet.getRange("H5:H9");
var duedates = duedatesRange.getValues();
var actionitRange = sheet.getRange("C5:C9");
var actionit = actionitRange.getValues();
var prjsRange = sheet.getRange("B5:B9");
var prjs = prjsRange.getValues();
var whosRange = sheet.getRange("D5:D9");
var whos = actionitRange.getValues();
//looping through all of the rows
for (var i = 0; i < duedates.length; ++i) {
var row = duedates[i];
for (var i = 0; i < actionit.length; ++i) {
var assignmenttext = actionit[i];
for (var i = 0; i < prjs.length; ++i) {
var project = prjs[i];
for (var i = 0; i < whos.length; ++i) {
var user = whos[i];
var expireDateFormat = Utilities.formatDate(
new Date(row[i]),
'ET',
'MM/dd/yyyy'
);
// email information
var subject = '';
var message =
" One of your action items is due today. " +
'\n' +
'\n' +
' Project: ' +
project[i] +
'\n' +
'\n' +
' Action Item: ' +
assignmenttext[i] +
'\n' +
'\n' +
'Check the Tracker now !!' +
'\n' +
url;
//expiration date information
var expireDateMonth = new Date(row[i]).getMonth() + 1;
var expireDateDay = new Date(row[i]).getDate();
Logger.log("Expire date is:" + expireDateDay);
//check for JD and send email to him if true
if (
user[i] === "JD" &&
expireDateMonth === todayMonth &&
expireDateDay === todayDay
) {
var subject =
'FEG AAAAAAManagement - An action item is due today!! : ' + assignmenttext[i];
MailApp.sendEmail(emailJonDoe, subject, message);
Logger.log('todayyyy!');
}
//check for ED and send email to him if true
if (
user[i] === "ED" &&
expireDateMonth === todayMonth &&
expireDateDay === todayDay
) {
var subject =
'FEG DDDDDManagement - An action item is due today!! : ' + assignmenttext[i];
MailApp.sendEmail(emailEmilyDoe, subject, message);
Logger.log('todayyyy!');
}
}
}
}
}
}
enter image description here
This is a simple snippet that send emails for each row that contains today's date in column H.
I have a similar one already working but I would like to implement the "user" feature.
Long story short, I want the code to:
Check for for each row that contains today's date in column H.
For each of above rows, check the content of it's respective cell in column D.
If it contains JD, send email alert to JD's email. If it contains ED, send email alert to ED's email.
The email should contain row's respective cell for column C (assignmenttext) and column B (project).
AFAIK, this code is not too heavy on the server side since I only "getRange" at the beginning and most of the code runs on the client side (is this correct?).
I am not confident with which loop function to use in this case and how to implement it with my code.
I am open to any comment or suggestion. Thanks in advance to anyone who would spend some time to help me. (:
This is the sort of thing I would have done. Admittedly you have to be comfortable with dealing with arrays but it makes for a nice compact code.
The two objects emails and prefix provide a place to enter new users all in one place and simplify the code by allow you to use a single send command.
function emailAlert() {
const sentcolumn=45;//I recommend adding some column to contain and indication of when an email is already been sent.
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName("Assignments");
const vA=sh.getRange(5,1,5,sentcolumn).getValues();//your data went from row 5 to row 9 that is five rows
const dt=new Date();
const today=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//this is a number
const emails={"JD":"jd#example.com","ED":"ed#example.com"};//emails
const prefix={"JD":"FEG AAAAAAManagement - An action item is due today!! : ","ED":"FEG DDDDDManagement - An action item is due today!! : "};//subject prefixes
vA.forEach(function(r,i){
let x=new Date(r[7]);
let duedate=new Date(x.getFullYear(),x.getMonth()+1,x.getDate()).valufOf();//this is a number
var body=Utilities.formatString('One of your action items is due today. \n\n Project: %s \n\n Action Item: %s\n\nCheck the Tracker now !!\n%s',r[1],R[2],ss.getUrl());
let subject=prefix[r[3]] + r[2];
if(today==duedate && r[sentcolumn-1]!='Sent') {
GmailApp.sendEmail(emails[r[3]],subject,body);
sh.getRange(i+5,sentcolumn).setValue('Sent');//adding something like this would keep you from sending duplicate emails by mistake
}
});
}
This could be done using a single for loop, checking data in each row:
function sendEmail() {
// today's date information
var today = new Date();
var todayMonth = today.getMonth() + 1;
var todayDay = today.getDate();
var todayYear = today.getFullYear();
Logger.log(todayDay);
// getting data from spreadsheet
var url = SpreadsheetApp.getActiveSpreadsheet().getUrl();
var sheet = SpreadsheetApp.openByUrl(url).getSheetByName("Assignments");
var resultssn = SpreadsheetApp.openByUrl(url).getName();
//Emails addresses
var emailJonDoe = "example#example.com"
var emailEmilyDoe = "example#example.com"
var lastRow = sheet.getLastRow();
var dataRange = sheet.getRange(2,1,lastRow,8).getValues();
//looping through all of the rows
for (var i = 0; i < lastRow-1; i++) {
var project = dataRange[i][1];
var assignmenttext = dataRange[i][2];
var user = dataRange[i][3];
var row = dataRange[i][7];
//expiration date information
var expireDateFormat = Utilities.formatDate(new Date(row),'ET','MM/dd/yyyy');
var expireDateMonth = new Date(row).getMonth() + 1;
var expireDateDay = new Date(row).getDate();
Logger.log("Expire date is:" + expireDateDay);
//check for expiry date
if (expireDateMonth === todayMonth && expireDateDay === todayDay) {
var subject =
'FEG AAAAAAManagement - An action item is due today!! : ' + assignmenttext;
// email information
var message =
" One of your action items is due today. " +
'\n' +
'\n' +
' Project: ' +
project +
'\n' +
'\n' +
' Action Item: ' +
assignmenttext +
'\n' +
'\n' +
'Check the Tracker now !!' +
'\n' +
url;
if (user === 'JD') {
MailApp.sendEmail(emailJonDoe, subject, message);
}
if (user === 'ED') {
MailApp.sendEmail(emailEmilyDoe, subject, message);
}
}
}
}
Running this on a sample sheet returns:
Note that I did not send emails, instead I just logged values in my test code.
I'm not sure why I can't print the message alongside the current time. The current time only prints when I comment out the if/else/else logic statement. I'm not sure why this is happening
let currentdate = new Date();
let tod = currentdate.getHours();
let dateTime =
currentdate.getDate() +
"/" +
(currentdate.getMonth() + 1) +
" " +
currentdate.getHours() +
":" +
currentdate.getMinutes();
window.onload = function() {
document.getElementById("time").innerHTML = dateTime;
};
if (tod < 11) {
message = "Good Morning.";
} else if (tod < 16) {
message = "Good Afternoon.";
} else {
message = "Good Evening.";
}
window.onload = function() {
document.getElementById("tod").innerHTML = message;
};
I am trying to get the date string value(mm/dd/yyyy)from a custom date and time field and set the returned value back into the custom field. I found this script and modified it but it does not seem to work. When I step through the code it breaks on var year = startDate.getFullYear() + ""; Any ideas what I am doing wrong? Thanks.
function ConcatChainsAuth() {
var startDate = Xrm.Page.getAttribute("new_dateauthorized").getValue();
if (startDate != null) {
var year = startDate.getFullYear() + "";
var month = (startDate.getMonth() + 1) + "";
var day = startDate.getDate() + "";
var dateFormat = month + "-" + day + "-" + year;
Xrm.Page.getAttribute("new_dateauthorized").setValue(dateFormat);
}
var lookupObject = Xrm.Page.getAttribute("new_chain");
if (lookupObject != null) {
var lookUpObjectValue = lookupObject.getValue();
if ((lookUpObjectValue != null)) {
var Chain = lookUpObjectValue[0].name;
}
}
var lookupObject = Xrm.Page.getAttribute("new_package");
if (lookupObject != null) {
var lookUpObjectValue = lookupObject.getValue();
if ((lookUpObjectValue != null)) {
var Package = lookUpObjectValue[0].name;
}
}
var concatedField = Chain + "-" + Package + "-" + dateFormat;
Xrm.Page.getAttribute("new_name").setValue(concatedField);
Xrm.Page.data.entity.save();
}
Assuming that new_dateauthorized is a CRM date field, then Xrm.Page.getAttribute("new_dateauthorized").getValue() will return a Date object.
In which case you can just manipulate the Date object, like so:
var currentDate = Xrm.Page.getAttribute("new_dateauthorized").getValue();
currentDate.setMonth(currentDate.getMonth() + 1);
Xrm.Page.getAttribute("new_dateauthorized").setValue(currentDate);
However, adding months in this fashion fails in some cases, check out the comments here for more info.
So I'm doing Google Calendar Api, that shows all the booked things for that day. I'm trying to get this code to print 'Vapaa' (means free in finnish and is shown if there's nothing booked in that time) only once but still print the rest of the appointments that come later that day. Here's the code that does the if else
if (events.length > 0) {
for (i = 0; i < events.length; i++) {
var event = events[i];
var when = new Date(event.start.dateTime);
var hs = addZero(when.getHours());
var ms = addZero (when.getMinutes());
var end = new Date(event.end.dateTime);
var he = addZero(end.getHours());
var me = addZero (end.getMinutes());
var now = new Date();
var hn = addZero(now.getHours());
var mn = addZero (end.getMinutes());
if (!when) {
when = event.start.date;
}
if (when.getTime() <= now.getTime() && now.getTime() <= end.getTime()){
appendPre(event.summary + ' ' + hs + (':') + ms + '' + (' - ') + '' + he + (':') + me + '');
} else {
appendPre('Vapaa');
appendPre(event.summary + ' ' + hs + (':') + ms + '' + (' - ') + '' + he + (':') + me + '');
}
return;
}
} else {
appendPre('Ei varauksia');
}
Also the appendPre is printed to html with this code
function appendPre(message) {
if (message != 'Vapaa'){
var pre = document.getElementById('booked');
var textContent = document.createTextNode(message + '\n' + '\n');
} else {
var pre = document.getElementById('free');
var textContent = document.createTextNode(message + '\n' + '\n');
}
pre.appendChild(textContent);
}
I'm so lost so any help would be awesome.
return; stops right where it is and exits the function, so your for loop will only run once with the return in there. See W3C for more info.
Typically, if you want to exit only the loop, but continue in the function (if you have code after it, it doesn't look like you've shown that here), you can use break;. If you want to skip the rest of the stuff in the for loop (nothing is shown in your example), but continue iterating onto the next entry, you can use continue;. See W3C for more details.
Today is my first day using Google Apps Script. I am trying to make a script to grab weather information from the Wunderground Api and paste it into a spreadsheet.
For some reason I am getting the error message "Missing ';' before statement. (Line 34)".
I've searched for a solution but cant find why I am getting this error in my code.
//Everyday this script gets weather data from Weather Underground and records it in this spreadsheet.
cDay = 0, cTemp = 1, cHumidity = 2, cPressure=3, cSparkline=4, cConditions=5;
nCols=7;
function getTemp() {
var url = 'http://api.wunderground.com/api/' + appKey + '/conditions/q/CO/Aspen.json';
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var response = UrlFetchApp.fetch(url);
var contentText = response.getContentText();
var conditions = Utilities.jsonParse(contentText);
var todaysConditions = conditions;
var temp = todaysConditions.current_observation.temp_c;
var humidity = todaysConditions.current_observation.relative_humidity;
var pressure = todaysConditions.current_observation.pressure_in;
var conditions = todaysConditions.response.features.conditions;
sheet.insertRowAfter(1);
var range = sheet.getRange(2,1,1, nCols);
var row = range.getValues()[0];
var d = new Date;
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();
var hour = d.getHours() + 1;
var minutes = d.getMinutes();
row[cDay] = month + '/' + day + '/' + year + '' + hour + ':' minutes; //here is the error
row[cTemp] = temp;
row[cHumidity] = humidity;
row[cPressure] = pressure;
row[cConditions] = conditions;
var nRows = numRows >= 10 ? 10 : numRows;
//row[cSparkline] = "=SPARKLINE(R[0]C[-3]:R[" + (nRows-1) + "]C[-3])";
range.setValues([row]);
}
Any help is appreciated
You have missed the '+' sign
row[cDay] = month + '/' + day + '/' + year + '' + hour + ':' + minutes;