I have a google spreadsheet that sends student information contained in a column that is queried and concatenated from another sheet. The queried information is separated by carriage returns. This column is then emailed to families triggered by a formula that calculates how many carriage returns should be in the cell.
That part I have taken care of in the sheet itself. I need assistance with emailing the column and maintaining the format of the cell. I've been able to do that with the code I have below, but it sends ALL of the data in column D in every email. I need it to only send what's located in the appropriate row for the email.
The main portion of the code that's giving me trouble, I think, is the following:
var OriginalString = SpreadsheetApp.getActive().getRange("D2:D").getValues();
var NewString = OriginalString.toString().replace(/\n/g, '<br>');
Any help would be appreciated!
function SendEmail3() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
var startrow = 2;
var lastRow = ss.getLastRow();
var lastCol = 10;
var range = ss.getRange(2, 1, lastRow, lastCol);
var data = range.getValues();
for (var i = 0; i < data.length; i++) {
var row = data[i];
var AccountName = row[0];
var email = row[1];
var Parent = row[2];
var Consent = row[4];
var StudentData = row[3];
var CarriageReturns = row[6];
var Trigger = row[7];
var emailSent = row[8];
var subject = "Your Application";
var OriginalString = SpreadsheetApp.getActive().getRange("D2:D").getValues();
var NewString = OriginalString.toString().replace(/\n/g, '<br>');
var message = "<HTML><BODY><font size=4>" +
"<P>" + "Hello " + Parent + ','
+"<BR>
+"<BR>" + NewString + "<BR>"
+"</HTML></BODY>";
var recipientsTo = email;
if (AccountName.length > 0 && Trigger >= CarriageReturns && emailSent != 'EMAIL_SENT') {
MailApp.sendEmail({
subject: subject,
to: recipientsTo,
htmlBody: message
});
ss.getRange(startrow + i, 9).setValue('EMAIL_SENT');
}
}
}
Is this what your looking for?
I set it up so that you can see the message on a modeless dialog rather than sending emails.
function SendEmail3() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet3');
var startrow = 2;
var lastRow = ss.getLastRow();
var lastCol = 10;
var range = ss.getRange(2, 1, lastRow, lastCol);
var data = range.getValues();
var html='';//Remove
for (var i = 0; i < data.length; i++) {
var row = data[i];
var AccountName = row[0];
var email = row[1];
var Parent = row[2];
var Consent = row[4];
var StudentData = row[3];
var CarriageReturns = row[6];
var Trigger = row[7];
var emailSent = row[8];
var subject = "Your Application";
var NewString = StudentData.split('\n').join('<br />');
//var message = "<HTML><BODY><font size=4>" + "<P>" + "Hello " + Parent + ',' + "<BR>" +"<BR>" + NewString + "<BR>" +"</HTML></BODY>";
var message=Utilities.formatString('<HTML><BODY><font size=4><P>Hello %s,<BR><BR>%s<BR></HTML></BODY>', Parent,NewString);
var recipientsTo = email;
if (AccountName.length > 0 && Trigger >= CarriageReturns && emailSent != 'EMAIL_SENT') {
//MailApp.sendEmail({subject: subject,to: recipientsTo,htmlBody: message});
html+=Utilities.formatString('%s- %s',i+1,message);//Remove
//ss.getRange(startrow + i, 9).setValue('EMAIL_SENT');
}
}
var userInterface=HtmlService.createHtmlOutput(html);//Remove
SpreadsheetApp.getUi().showModelessDialog(userInterface, 'Email Message');//Remove
}
Utilities.formatString()
Related
I'm looking on form submit to send an email to the submitter if they filled any of the questions as false, but the script is sending emails to any submitter who entered all true as well. I know the if script works with numbers as I used it in another project but when I changed the question to true false it seems to have broken the if statement. I also know the rest of the script works because it worked before I added the "if (certification == 'FALSE') {" line.
var EMAIL_SENT = 'EMAIL_SENT';
var FALSE = 'FALSE';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 20;
var dataRange = sheet.getRange(startRow, 1, numRows, 20);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[4];
var message = 'Test'
var emailSent = row[9];
var certification = row[5]
if (certification == 'FALSE') {
if (emailSent !== EMAIL_SENT) {
var subject = 'Self-Certification Alert';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
}
Please try amending to the following:
var EMAIL_SENT = 'EMAIL_SENT';
var FALSE = 'FALSE';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 20;
var dataRange = sheet.getRange(startRow, 1, numRows, 20);
var data = dataRange.getValues();
var error=0;
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[4];
var message = 'Test'
var emailSent = row[9];
var certification = row[5]
if (certification == 'FALSE') {
error++;
}
}
if (emailSent !== EMAIL_SENT && error >0) {
var subject = 'Self-Certification Alert';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
I ended up changing it to the following code and got it to work.
enter code here
var EMAIL_SENT = 'EMAIL_SENT';
var TRUE = 'true';
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 20;
var dataRange = sheet.getRange(startRow, 1, numRows, 20);
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[4];
var message = 'Test'
var emailSent = row[9];
var certification = row[5]
if (certification !== true) {
if (emailSent !== EMAIL_SENT) {
var subject = 'Self-Certification Alert';
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
}
I'm no expert with coding but I have a piece of code that works to send emails in bulk via Google Sheets. I want to change it so that it send from an alias that I've already set up in my Gmail settings. I would really appreciate it if someone would be able show me how I can change the piece of code below to do this. Thanks in advance!
function SendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var subject = sheet.getRange(2, 10).getValue();
var htmlBody = sheet.getRange(3, 10).getValue();
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
for (var i = 1; i < data.length; i++) {
(function(val) {
var row = data[i];
var emailAddress = row[1];
var name = row[0];
var CID = row[2];
var companyName = row[3];
var variable1 = row[4];
var variable2 = row[5];
var variable3 = row[6];
var newSubject = subject.replace(/COMPANY/g, companyName);
var replaced1 = htmlBody.replace(/CLIENT/g, name);
var replaced2 = replaced1.replace(/COMPANY/g, companyName);
var replaced3 = replaced2.replace(/CID/g, CID);
var replaced4 = replaced3.replace(/VAR1/g, variable1);
var replaced5 = replaced4.replace(/VAR2/g, variable2);
var replaced6 = replaced5.replace(/VAR3/g, variable3);
var aliases = GmailApp.getAliases()
MailApp.sendEmail({
to: emailAddress,
subject: newSubject,
htmlBody: replaced6,
})
})(i);
}
}
Instead of MailApp you need to use GmailApp.
function SendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var subject = sheet.getRange(2, 10).getValue();
var htmlBody = sheet.getRange(3, 10).getValue();
var dataRange = sheet.getDataRange();
var data = dataRange.getValues();
for (var i = 1; i < data.length; i++) {
(function(val) {
var row = data[i];
var emailAddress = row[1];
var name = row[0];
var CID = row[2];
var companyName = row[3];
var variable1 = row[4];
var variable2 = row[5];
var variable3 = row[6];
var newSubject = subject.replace(/COMPANY/g, companyName);
var replaced1 = htmlBody.replace(/CLIENT/g, name);
var replaced2 = replaced1.replace(/COMPANY/g, companyName);
var replaced3 = replaced2.replace(/CID/g, CID);
var replaced4 = replaced3.replace(/VAR1/g, variable1);
var replaced5 = replaced4.replace(/VAR2/g, variable2);
var replaced6 = replaced5.replace(/VAR3/g, variable3);
var aliases = GmailApp.getAliases()
GmailApp.sendEmail(emailAddress, newSubject, null, {
from: aliases[0], //Replace with aliase
htmlBody: replaced6,
})
})(i);
}
}
I'm having trouble getting a text to hyperlink on the script which sends through Gmail.
I've tested other articles on this site, but I don't seem to get the code right as it pulls the <a href code> in the actual email.
Below is my script (don't mind the content changes here).
For this example, I want to hyper link THIS PAGE in the var message to www.google.com.
Any idea how I can get this to work in an email coming from Gmail?
function sendemail() {
var spreadSheet = SpreadsheetApp.getActiveSheet();
var dataRange = spreadSheet.getDataRange();
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var text = text;
for (var i = 1; i < data.length; i++) {
(function(val) {
var row = data[i];
var emailAddress = row[1]; //position of email header — 1
var firstname = row[0]; // position of name header — 1
var price = row[2];
var content = row[25];
var contenttwo = row[24];
var html_link = "www.google.com";
var h = row[22];
var upcomingDate = Utilities.formatDate(row[3], "GMT+1", "MM/dd/yy");
//var date = row[3];
var options = {};
var subject = "Here's your info";
var message = "Dear " + firstname + ", " + "\n" + "\n" + content + " $" + price + " xxxxxxxxx" + upcomingDate + "." + "\n" + "\n" + "Please visit this page for more information.";
//MailApp.sendEmail(content)
MailApp.sendEmail(emailAddress, subject, message);
})(i);
}
}
May be something like this:
function sendemail() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var vA=rg.getValues();
var text='text';
for (var i=1;i<vA.length;i++) {
var emailAddress=vA[i][1];//col 2
var firstname=vA[i][0]; //col 1
var price=vA[i][2];//col 3
var content=vA[i][25];//col 24
var html_link="http://www.google.com";
var upcomingDate=Utilities.formatDate(new Date(vA[i][3]), "GMT+1", "MM/dd/yy");//if vA[i][3] is a string instead of a date then this may still work
}
var subject = "Here's your info";
var message=Utilities.formatString('Dear %s, <br /><br />%s $%s xxxxxxxxx%s.<br /><br />Please visit this page for more information.',firstname,content,price,upcomingDate,html_link);
var message1=Utilities.formatString('Dear %s, \n\n%s $%s xxxxxxxxx%s.\n\nPlease visit %s for more information.',firstname,content,price,upcomingDate,html_link);
MailApp.sendEmail(emailAddress, subject, message1,{htmlBody:message});
}
}
I have created a script which sends automated email based on cell values, created for an approval workflow.
Script is working fine, however, I am not able to prevent sending duplicate emails
Please help with some tips
//
var now = new Date();//mail semt date
var EMAIL_SENT = "EMAIL_SENT";// mail sent status
var approvalstatus = 'Approved';// Approved Status
function SecondaryApproval() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 100; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 16)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var Ticket = row[0];
var RequesterEmail = row[2];
var Item = row[3];
var emailAddress = row[6]; // First column
var message = row[7]; // Second column
var emailSent = row[8]; // Third column
var date = row[9];
var yesNo = row[5];
if (yesNo === approvalstatus) {
var subject = "Secondary Approval ";
GmailApp.sendEmail(emailAddress, subject, "Dear " + + "RequesterEmail" + Ticket + ", \n\nThis is an email report of your link. \n\nYour link is " + Item + " \n\nKind regards,\nName ", {
from: "Accounts#mydomain.com",
name: "Account"});
sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
sheet.getRange(startRow + i, 9).setValue(now);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
else (emailSent === EMAIL_SENT); { // Prevents sending duplicates
}
}
You need to check for emailSent !== EMAIL_SENT before sending email:
//
var now = new Date();//mail semt date
var EMAIL_SENT = "EMAIL_SENT";// mail sent status
var approvalstatus = 'Approved';// Approved Status
function SecondaryApproval() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 100; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 16)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var Ticket = row[0];
var RequesterEmail = row[2];
var Item = row[3];
var emailAddress = row[6]; // First column
var message = row[7]; // Second column
var emailSent = row[8]; // Third column
var date = row[9];
var yesNo = row[5];
if (yesNo === approvalstatus && emailSent !== EMAIL_SENT) {
var subject = "Secondary Approval ";
GmailApp.sendEmail(emailAddress, subject, "Dear " + + "RequesterEmail" + Ticket + ", \n\nThis is an email report of your link. \n\nYour link is " + Item + " \n\nKind regards,\nName ", {
from: "Accounts#mydomain.com",
name: "Account"});
sheet.getRange(startRow + i, 8).setValue(EMAIL_SENT);
sheet.getRange(startRow + i, 9).setValue(now);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
I have some code for my form that works well for emailing the manager of an expense report for approval, as well as receiving a URL back with information needed to send the response back to the person who filled out the information on the form.
all i need it to do now is to update the field in the form with the proper approval message (accept or reject)
I think this will have to happen in the doGet method, but will I have to pass in more then just the row number and approval message to get this done? (e.g. getting the spreadsheet, getting the sheet, etc.)
here is what i have so far, both emails, and the URL generated appear to be correct:
function sendEmail(e){
/**
var email = e.values[1];
var item = e.values[2];
var cost = e.values[3];
*/
var serviceInformation = e.values[1];
var language = e.values[2];
var meetingType = e.values[3];
var eventDate = e.values[4];
var clientName = e.values[5];
var detailedDirections = e.values[6];
var onSitePOCName = e.values[7];
var onSitePOCNumber = e.values[8];
var department = e.values[9];
var contactPhoneNumber = e.values[10];
var approval = e.values[11]; //the one we need to modify
var requestorEmail = e.values[12];
var managerEmail = e.values[13];
var Language2 = e.values[14];
var interpreterName = e.values[15];
var interpreterAgency = e.values[16];
var dateConformationSent = e.values[17];
var specialNotes = e.values[18];
var row = e.range.getRow();
var url = 'https://script.google.com/a/macros/richmond.k12.va.us/s/AKfycbwuRr1boKTH0v1mprWmc7PE66_mQ_dmPE0lyWb7vkfiyW3pn31b/exec';
//add on the parameters to URL to send to the manager
var approve = url + '?approval=true'+'&reply='+requestorEmail+'&row='+row;
var reject = url + '?approval=false'+'&reply='+requestorEmail+'&row='+row;
var html = "<HTML><body>"+
"<h2>please review</h2><br />"
+"<P>" + language +" " + serviceInformation
+"<p>" + meetingType+ " on "+ eventDate + " for " +clientName
+"<p>" + "Location: "+ department
+"<p>" + "requester: "+ requestorEmail+ " "+
"<p>"+
"Approve<br />"+
"<p>"+
"Reject<br />"+
"</HTML></body>";
MailApp.sendEmail(managerEmail, "Approval Request", "what no html?", {htmlBody: html});
}
function doGet(e){
var params = JSON.stringify(e);
return HtmlService.createHtmlOutput(params);
//added in an edit
var id = '1UzlJ6Uw12H5dGVcLz7ONp3Neehq1DL21hqZnrMx7mxs';
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getActiveSheet();
var column = 12;
var row = e.parameter.row;
var approval_cell = sheet.getRange(row, column);
Logger.log(row);
Logger.log(ss);
Logger.log(sheet);
Logger.log(approval_cell);
//logger logs the proper row. SS = Spreadsheet, sheet = sheet, approval_cell = Range
var aprovalResponce = (e.parameter.approval == 'true') ? 'Approved.' : 'Sorry, you need to reschedule';
//added values after aprovalResponce, in the email, they are both undefined.
var msg = "Your manager said : " + aprovalResponce;
var replyEmail = e.parameter.reply;
//MailApp.sendEmail(replyEmail, "Approval Request", msg);
//update the approval field
//changed in edit
if (e.parameter.approval == 'true') {
approval_cell.setValue("APPROVED"+;
} else {
approval_cell.setValue("DENIED");
}
//send the actual email out
MailApp.sendEmail(replyEmail, "Approval Request", msg);
}
It looks like you're trying to change the approval value in the spreadsheet? If so, I imagine this should work:
//update the approval field
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var column = 12;
var row = e.parameter.row;
var approval_cell = sheet.getRange(row, column);
if (e.parameter.approval == 'true') {
approval_cell.setValue("APPROVED");
} else {
approval_cell.setValue("DECLINED");
}