Failures for Google Apps Script: time based trigger - javascript

I am consistently getting a failure report on a time-based trigger I attempted to put into my app script. The script is intended to fire every morning and send out email reminders based on day counts.
I have tried to adjust the script function name that it is calling and change the type of trigger from days to hours.
function loops() {
// Runs at approximately 8:30am in the timezone of the script
ScriptApp.newTrigger('loops')
.timeBased()
.atHour(8)
.nearMinute(30)
.everyDays(1)
.create();
var report = SpreadsheetApp.getActive();
var ss = SpreadsheetApp.getActiveSpreadsheet()
var emailList = ss.getSheetByName("Email Reminder");
var data = emailList.getRange(3, 19, emailList.getLastRow() - 1, 24).getValues();
//Logger.log(data);
data.forEach(function (row, i) {
var id = row[0];
var customer = row[1];
var user = row[2];
var URL = row[3];
var proposedCount = row[4];
var pendingCount = row[5];
if (proposedCount == 36) {
MailApp.sendEmail(user, customer, customer + " has been in the proposal stage for " + proposedCount + " days." +"\n" +"\n" + URL + "\n" + "\nThis is a reminder to keep this job in front of you :)" + "\n" + "\nYour friendly neighborhood accountability partner");
}
if (pendingCount == 15) {
MailApp.sendEmail(user, customer, customer + " has been in the pending stage for" + pendingCount + " days." + "\n" + "\n" + URL + "\n" + "\nThis is a reminder to keep this job in front of you :)" + "\n" + "\nYour friendly neighborhood accountability partner");
}
});
}
I expect an email to be sent but I keep receiving emails with this error:
Your script, Email Reminder, has recently failed to finish successfully. A summary of the failure(s) is shown below.
6/23/19 10:52 AM: myFunction Script function not found: myFunction time-based 6/23/19 10:52 AM
6/24/19 10:52 AM: myFunction Script function not found: myFunction time-based 6/24/19 10:52 AM

Related

Bold text in email - Google Spreadsheet

I'm looking some way to bold text in email and when I open the msgBox.
I want bold only headlines, like in picture below:
this is my script, you choose some cell in row that interests you and run the function. Function show information about data from every cell in row, like inforamtion about "Name" and "email". Then if you push send it will send email with this informations. I want bold headlines for better clarity.
function sendEmail(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var fr1 = ss.getSheetByName("Sheet1");
var cell = ss.getActiveCell().getRow();
var lastColumn = fr1.getLastColumn();
var lastRowValues = fr1.getRange(cell,1,1,lastColumn).getValues();
var Nr = lastRowValues[0][0];
var Data = lastRowValues[0][1];
var Information = lastRowValues[0][2];
var Name = lastRowValues[0][3];
var email = lastRowValues[0][4];
var urlOfSS = ss.getUrl();
var message = "Message" +
"\n " +
"\nNr: " + Nr +
"\nData: " + Data +
"\nInformation: " + Information +
"\nName " + Name +
"\nEmail: " + email +
"\n " +
"\n Link to spreadsheet:" +
"\n " + urlOfSS;
var emails = ss.getSheetByName("Sheet1");
var numRows = emails.getLastRow();
var emailTo = email;
var subject = "Zgłoszenie FAS - " + Nr;
if (email == ""){
Browser.msgBox('This row is empty - Choose another');
} else {
var ui = SpreadsheetApp.getUi();
var l = ss.getSheets()[0]
var response = ui.alert('Email', "Do you want email \nNr: " + l.getRange(cell, 1).getValue() + "\nData: " + l.getRange(cell, 2).getValue() + "\nInforamtion: " + l.getRange(cell, 3).getValue()
+ "\nName: " + l.getRange(cell, 4).getValue(), ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
GmailApp.sendEmail(emailTo, subject, message);
} else {
Logger.log('The user clicked "No" or the dialog\'s close button.');
}
}
}
Regards
If I understand the requirement, Only the side headers(underlined in the screenshot) needs decoration.
While going through the Google Apps Scripts - Documentation, I came through this.
Hope this helps you.
Using the HtmlServices & HtmlOutputFromFile() would fulfill your requirement.
Please refer to Custom Dialogs. This would help you
https://developers.google.com/apps-script/guides/dialogs

How to group emails from google sheets with javascript

Situation: This is a Leave (or time off) approval/monitoring system for a small office of around 40 people. They put in for leave via google forms which is fed (eventually) into a google sheet. Once a month, we need to send out a single email to each person who took leave containing every entry they made for the month. I have altered the standard javascript from google that will send an email from a spreadsheet but it sends one email per row. I need to send a consolidated email to each person. Prior to running the script, the spreadsheet will only contain records with applicable dates, and will be sorted by [Email] and [Start Date] columns so there is no need to do any of that work in this script.
Here are the all columns in order: Timestamp, Email, Type of Leave Requested, Starting Date, Last Date, Number of Hours, Optional Note, Approval Decision, Optional Explanation.
I've tried creating a 2D array but my previous attempts at such an array have only resulted in using my 100/day google transactions in a matter of seconds. :/ Help is appreciated.
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menu = [{
name: "Send Email",
functionName: "uiSendEmail"
}];
ss.addMenu("Send Email Test", menu);
}
function uiSendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();
range= range.offset(1, 0, range.getNumRows()-1);
range.getValues().forEach( function( cell, key, data ){
var body = "Type of Leave - " + cell[2] + "\n\nStarting - " + cell[3] + "\n\nEnding - " + cell[4] + "\n\nHours - " + cell[5] + "\n\nNote Provided - " + cell[6] + "\n\n\n";
// "Type of Leave " + cell[2] \n\n;+ "Starting " + cell[3] \n\n + "Ending " + cell[4] \n\n + "Hours " + cell[5] \n\n + "Note provided " + cell[6]
GmailApp.sendEmail(cell[1], "Recent Requests for Time Off", body);
});
}
You could create an object, loop through the values and add them to the corresponding email.
function uiSendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();
var emails = {};
range= range.offset(1, 0, range.getNumRows()-1);
range.getValues().forEach( function( cell, key, data ){
var body = "Type of Leave - " + cell[2] + "\n\nStarting - " + cell[3] + "\n\nEnding - " + cell[4] + "\n\nHours - " + cell[5] + "\n\nNote Provided - " + cell[6] + "\n\n\n";
if(typeof emails[cell[1]] === 'undefined'){
emails[cell[1]] = body;
} else {
emails[cell[1]] += body;
}
});
//Now loop through the email object and send each completed one
for(var address in emails) {
GmailApp.sendEmail(address, "Recent Requests for Time Off", emails[address]);
}

Google-app-script: Date formating issue in a javascript email body using a cell row[] in Spreadsheet

I tried to modify a script to my own needs but I get an error "TypeError: Function getDate not found in the object . (line 13, file "Code")".
In fact, I want to compare today's date to the date contained in row[7] and send a reminder to some people for each line of the spreadsheet containing Projects...
Here is my actual code:
function sendEmails(step) {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process, Start at second row because the first row contains the data labels
var numRows = sheet.getLastRow(); // Number of rows to process -> all rows which are not empty
var totalRows = numRows - startRow +1; // total rows to process is numRows on which we remove start row +1
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, totalRows, 20) // range of columns to process
// Fetch values for each row in the Range
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2] + ", " + row [3] + ", " + row [4]; // email addresses for email distribution
var messagePVEnd = "The PV of project " + "'"+ row[0] +"'" + " is ending the " + row[7].getDate() + "/" + (row[7].getMonth+1) + "/" + row[7].getFullYear() + " , please push " + row[1] + " to get the reports."; // Email content for PV End
var messagePVMidStatus = "The PV of project " + "'" + row[0] + "'" + " will be at 500h the " + row[6].getDate() + "/" + (row[6].getMonth()+1) + "/" + row[6].getFullYear() + " , please push " + row[1] + " to get the intermediate status."; // Email content for PV after 500h
var messagePVOut = "The PV of project " + "'"+ row[0] +"'" + " is supposed to be finished since " + row[7].getDate() + "/" + (row[7].getMonth()+1) + "/" + row[7].getFullYear() + " , please push " + row[1] + " to get the reports and confirm that all reports are received in PV follow up google sheet."; // Email content for PV Out
var subjectPVEnd = row [0] + " -- Reminder for PV ending to get report from " + row [1]; // Email subject for PV end
var subjectPVMidStatus = row [0] + " -- Reminder to get PV intermediate status after 500h from " + row [1]; // Email subject for PV after 500h
var subjectPVOut = row [0] + " -- Reminder, PV should be finished with all reports received from " + row [1]; // Email subject for PV Out
if (row[8]==5) { // if date of PV status after 500h is equal to 5 days
MailApp.sendEmail(emailAddress, subjectPVMidStatus, messagePVMidStatus)
}
else if (row[8]==1) { // if date of PV status after 500h is equal to 1 day
MailApp.sendEmail(emailAddress, subjectPVMidStatus, messagePVMidStatus)
}
else if (row[9]==5) { // if PV end date is equal to 5 days
MailApp.sendEmail(emailAddress, subjectPVEnd, messagePVEnd)
}
else if (row[9]==1) { // if PV end date is equal to 1 day
MailApp.sendEmail(emailAddress, subjectPVEnd, messagePVEnd)
}
else if (row[10]!="yes") {
for (var j=1; j<=9; j++) {
if (row[9]==-(j*10)) { // if PV end date is out of date by multiple of 10 days up to 100 days (except if report are received)
MailApp.sendEmail(emailAddress, subjectPVOut, messagePVOut)
}
}
}
}
}
The emails are sent properly but I get problems with the date format in the message and I cannot figure out what I did wrong.
Any support would be welcome!
Thanks in advance,
Edit 17/08:
Here is a picture of the corresponding spreadsheet.
enter image description here
Please note that row[7] this is cell value but not the object. So to compare with the current date you need var now = new Date(); and then compare it with the row[7]
It seems like the data returned from spreadsheet in row[7] is not a date type. In order to check manually, double click on the spreadsheet cell and see if a calendar pops up. If not, then the data returned from the sheet is not of date object type.
To do in code, first check if the row[7] is instanceof Date. If yes, then process as is, otherwise, convert the date string returned from that cell to a date object and then process further.
Part of modified code will look like this
var emailAddress = row[2] + ", " + row [3] + ", " + row [4]; // email addresses for email distribution
var dateStr = '';
if(row[7] instanceof Date){
dateStr = row[6].getDate() + "/" + (row[6].getMonth()+1) + "/" + row[6].getFullYear();
}
else {
dateStr = row[7];
}
// var messagePVEnd = "The PV of project " + "'"+ row[0] +"'" + " is ending the " + dateStr + " , please push " + row[1] + " to get the reports."; // Email content for PV End
var messagePVMidStatus = "The PV of project " + "'" + row[0] + "'" + " will be at 500h the " + dateStr + " , please push " + row[1] + " to get the intermediate status."; // Email content for PV after 500h
var messagePVOut = "The PV of project " + "'"+ row[0] +"'" + " is supposed to be finished since " + dateStr + " , please push " + row[1] + " to get the reports and confirm that all reports are received in PV follow up google sheet."; // Email content for PV Out
var subjectPVEnd = row [0] + " -- Reminder for PV ending to get report from " + row [1]; // Email subject for PV end

Auto-Confirmation Emails not sending using trigger "on form submit"

I just created a new Google form and want an automatic confirmation email to go out when the form is submitted. Essentially, an academic department uses the form to approve or deny transfer credit for a student, and then the automatic email confirmation goes to the student, so that he/she knows the decision without having to contact the student separately.
I did this before with 2 different forms about 5 months ago and never had a problem. But with this new form that I just created, the emails are not working. It appeared to be working yesterday, but I can't recreate the successful email confirmation today. I've looked through the message boards, but haven't found the same exact issue.
I'm using the script editor and creating a trigger "on form submit." I started with the code that I know works for my other form and just changed the values and the message. My script looks like this:
function myFunction(e) {
var UserName = e.values[1];
var LastName = e.values[2];
var FirstName = e.values[3];
var SAGEID = e.values[4];
var UserEmail = e.values[5];
var Dept = e.values[6];
var Coll1 = e.values[11];
var Course1 = e.values[12];
var Req1 = e.values[13];
var Decision1 = e.values[14];
var Coll2 = e.values[15];
var Course2 = e.values[16];
var Req2 = e.values[17];
var Decision2 = e.values[18];
var Coll3 = e.values[19]
var Course3 = e.values[20];
var Req3 = e.values[21];
var Decision3 = e.values[22];
var Coll4 = e.values[23];
var Course4 = e.values[24];
var Req4 = e.values[25];
var Decision4 = e.values[26];
var Notes = e.values[9];
var Subject = "Course Equivalency Request Decision - " + FirstName + " " + LastName + " (" + Dept + ")";
var Message = "The " + Dept + " department has made a decision regarding your course equivalency request. Any questions regarding the decision below should be directed to the " + Dept + " department. " +
"\n\n\nDepartment Comments: " + Notes +
"\n\nCourse 1: " + Coll1 + ": " + Course1 + "\nDecision: " + Decision1 + "\nRequirement: " + Req1 +
"\n\nCourse 2: " + Coll2 + ": " + Course2 + "\nDecision: " + Decision2 +
"\nRequirement: " + Req2 +
"\n\nCourse 3: " + Coll3 + ": " + Course3 + "\nDecision: " + Decision3 +
"\nRequirement: " + Req3 +
"\n\nCourse 4: " + Coll4 + ": " + Course4 + "\nDecision: " + Decision4 +
"\nRequirement: " + Req4 +
"\n\n\nPlease allow 3-4 business days for processing. If after 3-4 business days, your undergraduate degree audit does not reflect these substitutions, please contact the Office of the University Registrar at registrar#brandeis.edu or 781-736-2010." +
MailApp.sendEmail(UserEmail, Subject, Message);
}
I'm banging my head against the wall trying to figure out why a version of this script works perfectly well with a different form, but won't work for me now. I'm relatively new to writing Google scripts, so any help you can offer would be greatly appreciated.

Delay actions in Jquery

I have a function that will be processing the checkout for an order on my website. Right now I haven't added in any of the ajax and what not but I'm trying to make a type of "loading screen" using javascript and bootstraps modal. This is what I have:
Javascript
function printCheckout() {
// Show the progress Modal
$('#myModal').modal({
keyboard: false,
backdrop: 'static',
});
$('#myModal').modal('show');
// Billing Information
$(".stepText").delay(800).html('Processing Billing Information');
// I want this ^ to delay the process and make it seem longer.
var name = $("input[name='bill_name']").val();
var address = $("input[name='bill_address_1']").val() + $(
"input[name='bill_address_2']").val();
var city = $("input[name='bill_city']").val();
var state = $("input[name='bill_state']").val();
var zip = $("input[name='bill_zip']").val();
var email = $("input[name='bill_email']").val();
var phone = $("input[name='bill_phone']").val();
var billing = 'name=' + name + '&address=' + address + '&city=' + city +
'&state=' + state + '&zip=' + zip + '&email=' + email + '&phone=' +
phone;
// Shipping Informaiton
$(".stepText").delay(800).html('Processing Shipping Information');
var name = $("input[name='ship_name']").val();
var address = $("input[name='ship_address_1']").val() + $(
"input[name='ship_address_2']").val();
var city = $("input[name='ship_city']").val();
var state = $("input[name='ship_state']").val();
var zip = $("input[name='ship_zip']").val();
var email = $("input[name='ship_email']").val();
var phone = $("input[name='ship_phone']").val();
var shipping = 'name=' + name + '&address=' + address + '&city=' + city +
'&state=' + state + '&zip=' + zip + '&email=' + email + '&phone=' +
phone;
// Payment Information
$(".stepText").delay(800).html('Processing Payment Information');
var number = $("input[name='number']").val();
var expiry_month = $("input[name='expiry_month']").val();
var expiry_year = $("input[name='expiry_year']").val();
var cvv = $("input[name='cvv']").val();
var payment = 'number=' + number + '&expiry_month=' + expiry_month +
'&expiry_year=' + expiry_year + '&cvv=' + cvv;
return false;
}
I want the modal to show some text like shown above based on what step we are on.
Processing Billing Information
Processing Shipping Information
Processing Payment Information
I want this to be delayed because right now as soon as the modal opens it is already on the last step which is the payment statement above.
I hope my question makes sense! Thanks for the help.
You should be using
setTimeout(function(){
// Whatever code to give delay of 3s
},3000);
The simpliest way is to use a timeout
https://developer.mozilla.org/fr/docs/Web/API/Window.setTimeout
setTimeout(function(){ ... }, delayModal) // delayModal integer in ms
If you are using Bootstrap 3
events are namespaces and the show event for modal is show.bs.modal
See http://getbootstrap.com/javascript/#js-events for more details
Your code should be like this :
$('#myModal').on('show.bs.modal', function (e) {
// your script
})
Demo:
http://jsfiddle.net/5960g3xt/1/ <= How you should do
http://jsfiddle.net/5960g3xt/2/ <= I have added some delay on the second so you can see the change

Categories

Resources