So I am using a script i found on line to send an email everytime a Google Form is completed, which includes the data in the form via a Google Sheet.
function sendFormByEmail(e)
{
var email = “email#address.com”;
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "A new travel request has been submitted.";
var subject = "New Travel Justification Request: ";
for(var i in headers)
message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";
subject += e.namedValues[headers[2]].toString() + " - starts " + e.namedValues[headers[15]].toString();
MailApp.sendEmail(email, subject, message);
// Based off of a script originally posted by Amit Agarwal - www.labnol.org
// Credit to Henrique Abreu for fixing the sort order
}
It works well, but I want it to exclude the headers for empty cells, as not all parts of the form require completion.
I know very little, so all help is appreciated.
I have updated the original Google Form script to not include fields that are empty. You can add a simple condition in the for loop:
for(var i in headers) {
if ( e.namedValues[headers[i]].toString() != "") {
message += headers[i] + ': '+ e.namedValues[headers[i]].toString() + "\n\n";
}
}
Related
I have created a spreadsheet for our coaches. They choose a sport and then spreadsheet fills with a roster of possible students. They click the check box next to the name of the student(s) attending the event, type the date, and click the Send button. It sends an email to the teachers listed (2nd tab has all rosters and emails). The script I wrote does all this no problem. The issue I am having deals with formatting. The names print out horizontally with a comma separating each name:
Student One, Student Two (etc.)
[This was in the original post, but I figured out how to skip blank spots in an array
If a student in the roster is skipped the printout looks like this:
Student One,,Student Three, Student Four,,Student Six (etc.) ]
I don't want the name to print if the checkbox isn't checked but I would like for the printout to look a little cleaner on an email. I used an array to read the names and I realize it's just printing out the array and it has an empty box. (solved the empty name part) I would like the email to look like:
Student One
Student Two
I am unsure how to accomplish this and have searched around quite a bit. What I have is functional, but doesn't look great. Another loop could accomplish this but I don't know how to do that while also formatting the email. It definitely doesn't like when I try to put a loop inside of there.
Here's the spreadsheet: Sample Sports Email Spreadsheet
Here is the code I have typed:
function emailRoster()
{
var teacher = SpreadsheetApp.getActive().getRange("Rosters!J2:J4").getValues();
var roster = SpreadsheetApp.getActive().getRange("Sheet1!A6:B").getValues();
var sport = SpreadsheetApp.getActive().getRangeByName("Sport").getValue();
var date = SpreadsheetApp.getActive().getRangeByName("Date").getValue();
var lenT = teacher.length;
var lenR = roster.length;
var playerTrue = [];
for(var i=0; i<lenR; i++){
if(roster[i][1])
playerTrue[i] = roster[i][0];
}
playerTrue = playerTrue.filter(String); //recently added...fixed the printout so it ignores blank parts of the array
playerTrue.forEach(function(name) {
Logger.log(name);
});
for(var p=0; p<lenT-1; p++){
var email = teacher[p];
var subject = "Students out for " + sport;
var body = "Teachers,\nThe following students will be out for " +sport+ " on " +date +": \n\n" + playerTrue +"\n";
GmailApp.sendEmail(email,subject,body);
}
};
EDIT
I have created another function to try and get it to return each name with a return after each name, but I can only get it to do the first name:
function createRoster(){
var roster = SpreadsheetApp.getActive().getRange("Sheet1!A6:B").getValues();
var playerTrue = [];
var lenR=roster.length;
for(var i=0; i<lenR; i++){
if(roster[i][1])
playerTrue[i] = roster[i][0]; }
playerTrue = playerTrue.filter(String);
Logger.log(playerTrue);
for(var b=0; b<lenR-1; b++){
return playerTrue[b] + "\n";
}
Logger.log(playerTrue);
};
So now the body variable in the original function looks like this:
var body = "Teachers,\nThe following students will be out for " +sport+ " on " +date +": \n\n" + createRoster() +"\n";
From your showing script, it seems that playerTrue is an array. When the array is directly used as the text body, such a situation occurs. When you want to align the value to the vertical direction, how about the following modification using join?
From:
var body = "Teachers,\nThe following students will be out for " +sport+ " on " +date +": \n\n" + playerTrue +"\n";
To:
var body = "Teachers,\nThe following students will be out for " + sport + " on " + date + ": \n\n" + playerTrue.join("\n") + "\n";
Or, when you want to put the value every 2 lines, how about the following modification?
var body = "Teachers,\nThe following students will be out for " + sport + " on " + date + ": \n\n" + playerTrue.join("\n\n") + "\n";
Reference:
join()
I am having problems with getting the current row information to be included in my email message when the email address that I'm sending to is listed on another spreadsheet. It works when I define the email address within the script.
I have 3 spreadsheets. The spreadsheet sheet names are Team, Issues, and Watchers. The Issues spreadsheet is the main sheet. When a new row is added on the Issues spreadsheet, column 34 is marked as "Ready to Email". If Column 34 is set to "Ready to Email" and Column 35 is not marked as "SENT", then I want to send an email notification using the email addresses listed on the WATCHERS spreadsheet then mark column 35 as "SENT". In the body of the email I want to include the URL of the main spreadsheet Issues spreadsheet) and information from the current row. I also want to include the value that is in cell "B2"on the TEAM spreadsheet. If no email addresses exist in the WATCHERS spreadsheet I want to mark column 35 as "No EMAIL ADDRESS EXIST".
Again, I'm able to send emails but the problem is that I'm not getting the subject info or the current row information in body of the email. Here is my script....
function sendEmail()
{
//setup function
var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Issues"); //the issues spreadsheet
var StartRow = 2; //first row of data to process
var LastRow = ActiveSheet.getLastRow();
var RowRange = LastRow - StartRow + 1;
var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,35);
var AllValues = WholeRange.getValues(); //getting the values defined in the range of the issues spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet(); //this is so that I can use the getUrl function to get the URL of the ISSUES spreadsheet
for (var i=0;i<AllValues.length;i++)
{
var watchers = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Watchers"); //the watchers spreadsheet
var wdata = watchers.getRange("A2:B" + watchers.getLastRow()).getValues(); //getting the values defined in the range in the watchers spreadsheet
var team = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Team"); //the team spreadsheet
var tdata = team.getRange("B2").getValues(); //getting the value defined in cell B2 in the team spreadsheet.
var CurrentRow = AllValues[i];
var EmailSent = CurrentRow[34]; //the value of column 35 on the issues spreadsheet
if (CurrentRow[33] == "Ready to Email" && EmailSent!= "sent")
{
wdata.forEach(function(row,CurrentRow,tdata,ss) //I think this is my problem. I'm trying to pass in CurrentRow, tdata, and ss so that I can use in my email.
{
var Subject = "New Issue reported: Issue# " + CurrentRow[3]; //set subject line
var message = "";
message += //CurrentRow, ss, and tdata is not working.
"<p><b>A new issue has been reported for </b>" + tdata + "<p><b> Reference the SRD for more details. </b>" + ss.getUrl() + "</p>" +
"<p><b>Issue Status: </b>" + CurrentRow[0] + "</p>" +
"<p><b>Group: </b>" + CurrentRow[1] + "</p>" +
"<p><b>LifeCycle Status: </b>" + CurrentRow[2] + "</p>" +
"<p><b>Issue Number: </b>" + CurrentRow[3] + "</p>" +
"<p><b>Priority: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Description: </b>" + CurrentRow[6] + "</p>" +
"<p><b>Opened By: </b>" + CurrentRow[10] + "</p>";
MailApp.sendEmail
({
to: row[1],
subject: Subject,
htmlBody: message,
});
});
var setRow = i + StartRow;
ActiveSheet.getRange(setRow, 35).setValue("sent"); //update the row if email is sent
}
}
}
Note that for the first line of the message, "ss.getUrl()" is failing with TypeError: Cannot call method "getUrl" of undefined.
This is what the body of the email looks like when I received it.
The below script runs without any visible error, but no emails are sent. Any ideas as to why?
function sendEmailLoop() {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
sheets.forEach(function(sheet) {
var range = sheet.getDataRange();
if (sheet.getName() == "Summary") //Disregard tab named 'Summary'
{
}
else {
var range = sheet.getDataRange(); //to set the range as array
var values = range.getDisplayValues(); //to get the value in the array
var lastRow = range.getLastRow();
var ss = SpreadsheetApp.getActiveSpreadsheet(); //declare the spreadsheet
var sheet = ss.getSheetByName("Sheet1");
var Title = values[0][0]; //[Title] cell A1
var URL = values[0][1]; //[URL] cell B1
var i;
var logContent = '';
for (i = 3; i < lastRow; i++) {
var Name = values[i][0]; //[Name] cell A++
var Email = values[i][1]; // [Email] cell B++
Logger.log('to: ' + Email);
Logger.log('subject: ' + Name + Title + 'Test');
Logger.log('message: ' + 'This is a test message for the training that can be found at ' + URL);
/*
MailApp.sendEmail({
to: Email,
subject: Name + Title + 'Test',
message: 'This is a test message for the training that can be found at ' + URL});
*/
}; //end for loop - email tab data
}; // end 'else'
}); // end function(sheet)
} // end SendEmailLoop()
Here is the Stackdriver log of a successful execution (success meaning no errors, but still no emails are sent):
The structure of the spreadsheet associated with the script:
Note - an earlier version of this script didn't include the sheets.forEach() method call (ie, to loop through each tab of the spreadsheet) and the emails were sent fine.
Could the lack of emails being sent or received be related to the fact that I have many sheets and this function is looping through them?
function sendEmailLoop() {
var exclA=['Summary'];
var ss=SpreadsheetApp.getActive();
var sheets=ss.getSheets();
sheets.forEach(function(sheet) {
if (exclA.indexOf(sheet.getName())==-1) {
var range=sheet.getDataRange();
var values=range.getDisplayValues();
var lastRow=range.getLastRow();
var Title=values[0][0];
var URL=values[0][1];
var logContent='';
for (var i=3; i <values.length; i++) {
var Name=values[i][0];
var Email=values[i][1];
Logger.log('to: %s\nsubject: %s %s Test\nmessage: %s This is a test message for the training that can be found at %s',Email,Name,Title,URL);
/*
MailApp.sendEmail({
to: Email,
subject: Name + Title + 'Test',
message: 'This is a test message for the training that can be found at ' + URL});
*/
}
}
});
}
Answer:
You need to uncomment your MailApp code.
Code changes:
I tested your code and it seems to run without issue for me, including the receipt of the emails, only that the code comments around your MailApp call need to be removed (the /* and the */).
I would also suggest adding a conditional line before you send the email in the event .getDataRange() obtains seemingly empty rows:
if (Email == "") {
continue;
};
MailApp.sendEmail({
to: Email,
subject: Name + Title + 'Test',
message: 'This is a test message for the training that can be found at ' + URL});
References:
JavaScript Comments
My application is designed to create a table which is later edited by the user. After this I need my application to send the page content via email.
I used URLHelper's trigger email() but through this I am able to trigger the email with to, cc, subject, text body but my ui5 application is not able to insert the table into the email.
Can someone please suggest something? or is it even possible?
I won't mind using plain javascript either, Point is I need to do this without using the backend.
We do something similar on one of our apps. I added a button to the screen which when clicked invokes a 'mailto', and populates the email client with the to, subject and body. The body is created as part of the script. We basically read the table contents into an array, then loop through the entries using a forEach. Keep in mind using mailto or even the URLHelper does not allow you to use HTML formatted text in the 'body' of the email. So, if you're looking for something pretty, you may be out of luck.
onNotifyUserPress: function(oEvent) {
var oItem = oEvent.getSource();
var oBinding = oItem.getBindingContext();
// Set some vars for the email package
var sEmpEmail = oBinding.getProperty("Smtp");
var sEmpName = oBinding.getProperty("STEXT_2");
var sEmailSubject = "Your Subject " + sEmpName;
// Create DateFormat Object
var oDateFormat = DateFormat.getDateTimeInstance({pattern: "dd/MM/yyyy"});
// Retrieve Table Data
var oTable = this.getView().byId("yourTable");
var aTableData = oTable.getBinding("items").getContexts();
// Build the email body
var sBody = sEmpName + " - Some Body Text\n\n";
sBody += "Field 1 | " + "Field 2 | " + "Field 3 | " + "Field 4" + "\n";
// Loop through table data and build the output for the rest of the email body
aTableData.forEach(function(oModel) {
var oModelData = oModel.getObject();
var sEndDate = oDateFormat.format(oModelData.Vendd);
var sStatus = this._formatStatus(oModelData.ZQ_STAT);
sBody += (oModelData.Essential === "X" ? "Yes" : "No") + " | " + oModelData.Ttext + " | " + sEndDate + " | " + sStatus + "\n";
}.bind(this));
// Open email client window and prepopulate with info
window.open("mailto:" + sEmpEmail + "&subject=" + sEmailSubject + "&body=" + encodeURIComponent(sBody), "_self");
},
You'll obviously need to update the code to point to your table data. In this particular instance, we have an object page with a couple of sections. Each section contains a table which loads a list of entities that are associated with the user. As the data is already loaded and exists in the model, this may not work in the same fashion as what you're trying to do (if I understand correctly), as you need to send an email after the data is entered/modified?
Hopefully this can at least get you started!
Cheers!
I'm trying to create a script for a student attendance spreadsheet that will look in Column E for the string "X". For each instance of "X", the string from column A (the student name) will be added to the body of an email. I'm pretty new to JavaScript, although I have been studying the basics. I've done a lot of research and found some scripts I was able to modify to send an individual email for each instance of X in E. However, I have not been able to figure out how to combine that information into a single email.
Here's what I have so far:
function Email_ReminderNS() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("July_August"),
EMAIL_SENT = "EMAIL_SENT",
statusArray = sheet.getDataRange().getValues();
var class = statusArray[0][8],
status = "X",
email = "XXXX"
for (i=7;i < statusArray.length;i++){
var emailSent = statusArray[i][84];
if (status == statusArray[i][4] & emailSent != EMAIL_SENT) {
var student = statusArray[i][0];
var body = "This is a No-Show Report for " +student+ " from " + class;
var subject = "No-Show Report for " + student+ " from " + class;
MailApp.sendEmail(email,subject,body,{NoReply : true});
sheet.getRange(i+1, 85).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
I realize I'll probably need to move the sendEmail function to be outside the IF statement. I tried to create an array with the names and join those into a string and add it to the body of the email, but I've had no luck. It just ended up sending the last name instead of all of them.
If anyone has any suggestions for me I would be deeply grateful.
First set up variables to keep track of which student did not show up:
var students = [];
var student_rows = [];
Then, add student to these arrays when X is found:
if (status == statusArray[i][4] & emailSent != EMAIL_SENT) {
var student = statusArray[i][0];
students.push(student);
student_rows.push(i+1);
}
Then send the email with all student names combined (outside of the for loop like you said)
var body = "This is a No-Show Report for " + students.join(', ') + " from " + class;
var subject = "No-Show Report for " + students.join(', ') + " from " + class;
MailApp.sendEmail(email,subject,body,{NoReply : true});
Finally update the spreadsheet indicating which names were in that email:
for (var i=0; i<student_rows.length; i++) {
sheet.getRange(student_rows[i], 85).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
Here's the complete script:
function Email_ReminderNS() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("July_August"),
EMAIL_SENT = "EMAIL_SENT",
statusArray = sheet.getDataRange().getValues();
var class = statusArray[0][8],
status = "X",
email = "francis#bposolutions.com";
var students = [];
var student_rows = [];
for (i=7;i < statusArray.length;i++){
var emailSent = statusArray[i][84];
if (status == statusArray[i][4] & emailSent != EMAIL_SENT) {
var student = statusArray[i][0];
students.push(student);
student_rows.push(i+1);
}
}
var body = "This is a No-Show Report for " + students.join(', ') + " from " + class;
var subject = "No-Show Report for " + students.join(', ') + " from " + class;
MailApp.sendEmail(email,subject,body,{NoReply : true});
for (var i=0; i<student_rows.length; i++) {
sheet.getRange(student_rows[i], 85).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
There are probably many ways to implement a new version of your code, the other answer probably works but I think it can be improved (a bit).
First of all, you can get rid of the flush method that does nothing else than slowing down the function (it was originally used in the Google example to check the sent status row by row, it is useless when we send only one mail with all the data in it)
Secondly, it might be a good idea to use html format to get a better looking result.
And lastly, it is good practice to write back to the sheet using one setValues instead of multiple setValue() in a loop.
Here is a possible replacement code, you'll have to "tune" it to your needs to eventually improve the message format but the main structure is there and working.
function Email_ReminderNS() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("July_August"),
statusArray = sheet.getDataRange().getValues();
var email = Session.getActiveUser().getEmail(); //replace with the email you want, this value will send mails to you I used it for test.
var class = statusArray[0][8],
status = "X",
students = [];
for (var i=7;i < statusArray.length; i++){
var emailSent = statusArray[i][84];
if (status == statusArray[i][4] & emailSent != "EMAIL_SENT") {
students.push(statusArray[i][0]);
statusArray[i][84]="EMAIL_SENT";
}
}
var subject = "No-Show Report for " + students.length + " from " + class;
var textBody = "This is a No-Show Report for " +students.length+ " from " + class+"\n";
var HTMLBody = "<b>This is a No-Show Report for " +students.length+ " from " + class+"</b><br><br>"
+'<table style="background-color:lightblue;border-collapse:collapse;" border = 1 cellpadding = 5><th>Sent Mails</th><tr>';
for(var n in students){
HTMLBody += '<tr><td>'+n+'</td><td>'+statusArray[n][0]+'</td></tr>';
textBody += '\n'+n+' - '+statusArray[n][0];
}
HTMLBody+='</table><BR> kind regards.' ;
textBody+='\n\nKind regards';
Logger.log(HTMLBody);
Logger.log(textBody);
MailApp.sendEmail(email,subject,textBody,{'NoReply' : true, 'htmlBody' : HTMLBody});
sheet.getRange(1,1,statusArray.length,statusArray[0].length).setValues(statusArray);
}