In my Spreadsheet, I have taken the answers from the users and added a column to grade them. Now, I want to mail them the whole data they have entered and their respective grades. To do that, I have to know the Row number of the variable "key2" which will be changing for every response of the user. I am providing my code here. Some one please help me.
/* Send Confirmation Email with Google Forms */
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
var s1 = SpreadsheetApp.getActiveSpreadsheet();
var s2 = SpreadsheetApp.setActiveSheet(s1.getSheetByName("Sheet2"));
try {
var cc, sendername, subject, column2;
var message, value, textbody, sender;
// This is your email address and you will be in the CC
cc = Session.getActiveUser().getEmail();
// This will show up as the sender's name
sendername = "Quiz2win";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Google Form Successfully Submitted";
// This is the body of the auto-reply
message += "We have received your details.<br />Thanks!<br /><br />";
column2 = s2.getRange(1,1,1,10).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["Email Address"].toString();
// Only include form values that are not blank
for ( var keys in column2 ) {
var key2 = column2[keys];
if ( e.namedValues[key2] ) {
message += key2 + ' :: '+ e.namedValues[key2] + "<br />";
} else {
var key1 = s2.getValues();
message += key2 + ' :: ' + key1 + "<br/>";
}
}
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sender, subject, textbody,
{cc: cc, name: sendername, htmlBody: message});
} catch (e) {
Logger.log(e.toString());
}
The range you are mentioning here s2.getRange(1,1,1,10) is basically getting only first row of data from the sheet.
Instead get all the rows and column values into a range variable and loop through the range for each row and send an email in the loop itself.
Check this documentation for more details.
Related
This is an offshoot of a similar post here. A good answer was provided by Rivero but his code sends the values on the Google Spreadsheet's response sheet. I was wondering if instead the script would return values of the same row and column range specified by the code BUT from other sheet in the same spreadsheet. I'm really new to this and can't seem to find the right combination of codes to do so. Here is Rivero's code, I hope someone can help me accomplish this. Thanks in advance!
/* Send Confirmation Email with Google Forms */
function Initialize() {
var triggers = ScriptApp.getProjectTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
try {
var ss, cc, sendername, subject, columns;
var header, message, value, textbody, sender, itemID, url;
// This is your email address and you will be in the CC
cc = "name#email.com";
// This will show up as the sender's name
sendername = "name to be displayed as sender";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Choose an approppiate subject";
// This is the body of the auto-reply
message = "";
ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["Username"].toString();
// Only include form values that are not blank
for ( var keys in columns ) {
var key = columns[keys];
//Use this to look for a particular named key
if ( e.namedValues[key] ) {
if ( key == "Username" ) {
header = "The user " + e.namedValues[key] + " has submitted the form, please review the following information.<br />";
} else {
message += key + ' ::<br /> '+ e.namedValues[key] + "<br />";
}
}
}
}
textbody = header + message;
textbody = textbody.replace("<br>", "\n");
Logger.log("Sending email");
GmailApp.sendEmail(cc, subject, textbody,
{cc: cc, name: sendername, htmlBody: textbody});
} catch (e) {
Logger.log(e.toString());
}
}
It would better if you can name your sheet and use it instead of using active spreadsheet.
var sheet = SpreadsheetApp.openById('spreadsheetId').getSheetByName('name');
OR you can also use the sheet number to access it.
var sheet = SpreadsheetApp.openById('spreadsheetId').getSheets()[0];
After you have reference to the sheet, get the range for the column followed by data array:
var columnRange = sheet.getRange('a1Notation');
var dataArray = columnRange.getValues();
then iterating the data as per your convenience
for(var i=1; i<=dataArray.length; i++)
{
}
Background: Google Script for Forms creating a phone message log that emails to a specific person as selected.
Need help with the email address portion of the code. I want to have a message sent to an email address based on the radio button with the person's name selected.
function Initialize() {
var triggers = ScriptApp.getScriptTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendGoogleForm")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendGoogleForm(e) {
try {
var email = HELPHELPHELP;
// Optional but change the following variable
// to have a custom subject for Google Form email notifications
var subject = "Google Docs Form Submitted";
var s = SpreadsheetApp.getActiveSheet();
var columns = s.getRange(1, 1, 1, s.getLastColumn()).getValues()[0];
var message = "";
// Only include form fields that are not blank
for (var keys in columns) {
var key = columns[keys];
if (e.namedValues[key] && (e.namedValues[key] != "")) {
message += key + ' :: ' + e.namedValues[key] + "\n\n";
}
}
// This is the MailApp service of Google Apps Script
// that sends the email. You can also use GmailApp for HTML Mail.
MailApp.sendEmail(email, subject, message);
} catch (e) {
Logger.log(e.toString());
}
}
Presumably the intended recipient's name comes from a question / column with a title like "Message for".
In that case, try this:
...
var email = resolveEmail( e.namedValues["Message for"] );
...
function resolveEmail( userName ) {
// List of userNames & (secret) email addresses
var userEmails = {
"Mickey" : "bigears#example.com",
"Donald" : "duckman#example.com" // etc.
};
if (userName in userEmails) {
return userEmails[userName];
}
else {
// Unknown user - send email to script owner
return Session.getEffectiveUser().getEmail();
// Alternatively, could throw an error to get notified
// throw new Error( "Could not resolve email for user: " + userName );
}
}
I'm sending confirmation mail with following code in google docs scripts.
But It is sending from my personal gmail address. I need define different mail address. Cause I will use this for my company and mail need show like that sometest#companytsite.com
How can i do that?
My Code:
function Initialize() {
var triggers = ScriptApp.getScriptTriggers();
for (var i in triggers) {
ScriptApp.deleteTrigger(triggers[i]);
}
ScriptApp.newTrigger("SendConfirmationMail")
.forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
.onFormSubmit()
.create();
}
function SendConfirmationMail(e) {
try {
var ss, cc, sendername, subject, columns;
var message, value, textbody, sender;
// This is your email address and you will be in the CC
cc = Session.getActiveUser().getEmail();
// This will show up as the sender's name
sendername = "Your Name Goes Here";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
subject = "Google Form Successfully Submitted";
// This is the body of the auto-reply
message = "We have received your details.<br>Thanks!<br><br>";
ss = SpreadsheetApp.getActiveSheet();
columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];
// This is the submitter's email address
sender = e.namedValues["Email Address"].toString();
// Only include form values that are not blank
for ( var keys in columns ) {
var key = columns[keys];
if ( e.namedValues[key] ) {
message += key + ' :: '+ e.namedValues[key] + "<br />";
}
}
textbody = message.replace("<br>", "\n");
GmailApp.sendEmail(sender, subject, textbody,
{cc: cc, name: sendername, htmlBody: message});
} catch (e) {
Logger.log(e.toString());
}
}
According to the documentation: https://developers.google.com/apps-script/reference/gmail/gmail-app#sendEmail(String,String,String,Object)
You should be able to pass in a from in the options.
GmailApp.sendEmail(sender, subject, textbody, {
from: "sometest#companytsite.com",
cc: cc,
name: sendername,
htmlBody: message
});
Nb. to avoid confusion sender should really be to
Now it also says that the email address you want to send it from must be an alias, to check you can print out getAliases() to see if it exists. If not you should be able to follow these instructions:
https://support.google.com/mail/answer/22370?hl=en
i'm new to google app scripting, and there's a lot i'm struggling to understand here.
i'm using this script below.
i'm trying to take a line from the form submission (that states the location of where it was submitted from) and add it to the email SUBJECT line that is sent by this script. this will allow me to sort in my email box by the location they are submitted from.
here are the contents below. is there an e.value i need to use? do i need to define another variable and add it with a + to the subject string?
function sendFormByEmail(e)
{ var email = "workemails#mydomain.com";
var subject = "Maintenance Submission Form";
var message = ""; for(var field in e.namedValues) { message += field + ' :: ' + e.namedValues[field].toString() + "\n\n";
} MailApp.sendEmail(email, subject, message);
}
Just concatenate it with the existing subject like this:
function sendFormByEmail(e) {
var email = "workemails#mydomain.com";
var subject = "Maintenance Submission Form";
var message = "";
for(var field in e.namedValues) {
message += field + ' :: ' + e.namedValues[field].toString() + "\n\n";
}
//get location from field named locationFieldNameGoesHere and
//prepend it to the standard subject line
subject=e.namedValues['locationFieldNameGoesHere'].toString() + subject;
MailApp.sendEmail(email, subject, message);
}
I've seen several scripts listed on how to delete rows that contain blanks or 0's however I have a form and once submitted the responses are entered into the last row. The form has required fields but also has fields that don't need to be answered. For the columns that don't have a response I would like to not have the headers show up on the email on once the form is submitted. If I use the scripts that deal with rows all the information is deleted.
My current script is:
function sendFormByEmail(e)
{
var email = "anyone#anywhere.com";
var subject = "Form Submitted";
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
for(var i in headers)
message += headers[i] + ' = '+ e.namedValues[headers[i]].toString() + "\n\n";
MailApp.sendEmail(email, subject, message);
}
Right now if the person filling out the form doesn't provide a response I get the following:
Cell Number =
I would like to get only headers on responses and eliminate any blanks or "0's".
You simply need to validate your conditions before adding a response value to the message.
Since a blank response can sometimes contain spaces (and thus be 'non-blank'), first use the .replace() method to strip any padding before testing the string.
You can adjust the if statement here to suit your needs:
function sendFormByEmail(e)
{
var email = "anyone#anywhere.com";
var subject = "Form Submitted";
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
for(var i in headers) {
var value = e.namedValues[headers[i]]
.toString()
.replace(/(^\s+|\s+$)/g, ''); // Remove padding
// Only report form responses that aren't blank or 0.
if (value.length > 0 && value != '0') {
message += headers[i] + ' = '+ value + "\n\n";
}
}
MailApp.sendEmail(email, subject, message);
}