I am trying to print the log for following JavaScript. However, script is not printing the log value. I highlighted the variable value the script should identify in the if/else code, however after else it is not reading the $systemDate from outside the if/else statement; as a result it is not logging the value for $dueDate. I am trying following code:
_wait(_div("/date-display .* bwc-selected/"));
var $date = _getText(_div("/date-display .* bwc-selected/"));
_wait(2000);
_log($date);
_wait(2000);
var str = ($date)
var d = new Date(str)
var $systemDate = (d.getUTCMonth()+1) +"/"+ d.getUTCDate() + "/" + d.getUTCFullYear();
_log('System Date is:' + $systemDate);
var $dueDate = person.$dueDate;
if ($dueDate != null)
{
var $dueDate = person.$dueDate;
person.$dueDate = $dueDate;
_log('Due Date is:' + $dueDate);
}
else
{
var $days = 90;
var $theDate = new Date($systemDate);
var $pregnancyDueDate = new Date($theDate);
$pregnancyDueDate.setDate($pregnancyDueDate.getDate() + $days);
var due = ($pregnancyDueDate);
var n = new Date(due);
var $dueDate = (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();
_log('Due Date is:' + $dueDate);
person.$dueDate = $dueDate;
}
Do not redeclare your variable !
remove var from
var $dueDate = (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();
it should be:
$dueDate = (n.getUTCMonth()+1) +"/"+ n.getUTCDate() + "/" + n.getUTCFullYear();
You variable was already declared here before your if
var $dueDate = person.$dueDate;
and finally update your if condition to :
if ($dueDate != "")
because your cells can be just empty and not containing null values
Related
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 trying to read a file by date with JS
i tried to replace the varable TimeNow without success.
when fo example i make fixed filename it works fine, but replacing the filename with a variable 'TimeNow' it doesnt work.
var prex = document.querySelector('pre');
var DateNow = new Date();
DateNow.setHours(DateNow.getHours()-1);
var TimeNow=DateNow.getFullYear() + "" + (DateNow.getMonth()+1) + "" + DateNow.getDate();
loadCSV("counters_test_directory_${TimeNow}.log");
prex.innerHTML += '\n\n';
prex.innerHTML += TimeNow;
<pre>
</pre>
loadCSV(`counters_test_directory_${TimeNow}.log`);
var prex = document.querySelector('pre');
var DateNow = new Date();
DateNow.setHours(DateNow.getHours()-1);
var TimeNow=DateNow.getFullYear() + "" + (DateNow.getMonth()+1) + "" + DateNow.getDate();
alert(`counters_test_directory_${TimeNow}.log`);
// The source of this function is NOT given.
//loadCSV(`counters_test_directory_${TimeNow}.log`);
prex.innerHTML += '\n\n';
prex.innerHTML += TimeNow;
<pre>
</pre>
I have a problem with a field name that has a period "." to it
I can only make it work if the field name is something like input_1 or input_1_2
But when it is something like input_1.2 it doesn't work.
function CalculateSig(stringToSign, privateKey){
//calculate the signature needed for authentication
var hash = CryptoJS.HmacSHA1(stringToSign, privateKey);
var base64 = hash.toString(CryptoJS.enc.Base64);
return encodeURIComponent(base64);
}
var d = new Date;
var expiration = 3600;
var unixtime = parseInt(d.getTime() / 1000);
var future_unixtime = unixtime + expiration;
var publicKey = "mupubkey";
var privateKey = "pyprikey";
var method = "POST";
var route = "forms/6/submissions";
stringToSign = publicKey + ":" + method + ":" + route + ":" + future_unixtime;
sig = CalculateSig(stringToSign, privateKey);
var url = 'https://www.localhostlbahblah.com/gravityformsapi/' + route + '?api_key=' + publicKey + '&signature=' + sig + '&expires=' + future_unixtime;
var values = {input_values:{'input_1.3':"test",// not working first name value
"input_2":"testetest", // this is working
}}
values_json = JSON.stringify(values);
$.post(url, values_json, function(data){
console.log(data.response);
});
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.
I'm fairly new to javascript and I need to rename or add to an extension of a css and .png file. The script is embedded within an ETL process. I have a variable "prd" that holds the value of the file name that the style.css and picture.png file are derived from and I also need to add a date or time stamp to the end of the extension. Basically I'm wanting to concatenate prd+style_02_06_14.png
Desired results:
Prd = sales_report
File = style.css.
Result = "sales_report_style_02_06_14.png" and "sales_report_style_02_06_14.css"
Here is my code
var sourceCssFile = outputfolder + "style.css";
var destinationCssFile = outputfolder + css_pic;
if(isFolder(destinationCssFile) == false) {
createFolder(destinationCssFile);
var testvar = "inside";
}
destinationCssFile = destinationCssFile + "/style.css";
moveFile(sourceCssFile, destinationCssFile, true);
var sourceImageFile = outputfolder + "picture.png";
var destinationImageFile = outputfolder + css_pic + "/picture.png";
moveFile(sourceImageFile, destinationImageFile, true);
var cont = loadFileContent(output);
var replaceCss = css_pic + "tt+style.css";
var replaceImg = css_pic + "tt + picture.png";
cont = cont.replace("style.css", replaceCss);
cont = cont.replace("picture.png", replaceImg);
var filename = outputfolder + new_str;
Try this:
function formatResult(prd, filename, date, extension) {
return prd + '_' + filename + '_' + formatDate(d) + '.' + extension;
}
function formatDate(d) {
var year = d.getFullYear().toString().substr(2,2);
var month = (d.getMonth()+1) + '';
if (month.length == 1) {
month = "0" + month;
}
var day = d.getDate() + '';
if (day.length == 1) {
day = "0" + day;
}
return month + '_' + day + '_' + year;
}
Demo
http://jsfiddle.net/WHpuU/5/
Nota
alert( 'style.css'.replace(/\.css$/i, '') ); // shows 'style'