A mail merge script that we use for 5 of our google docs stopped working last week - a similar thing happened a few weeks ago when DocsList became obsolete and I was able to fix it, but now it has stopped working again.
var docTemplate = "15kvC3M8b0Me3Vi3GwErQvG60BlTC0qAHMXrlc3Ocky8";
var docName = "RefundByCheck"
function onFormSubmit(e) {
var first_name = e.values[1];
var last_name = e.values[2];
var customer_email = e.values[3];
var brand = e.values[4];
var amount = e.values[5];
var purchase_date = e.values[6];
var customer_address = e.values[7];
var rep_name = e.values[8];
var order_number = e.values[9];
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+'_'+order_number)
.getId();
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getActiveSection();
copyBody.replaceText('keyFirst', first_name);
copyBody.replaceText('keyLast', last_name);
copyBody.replaceText('keyBrand', brand);
copyBody.replaceText('keyAmount', amount);
copyBody.replaceText('keyPurchaseDate', purchase_date);
copyBody.replaceText('keyAddress', customer_address);
copyBody.replaceText('keyRep', rep_name);
copyBody.replaceText('keyOrder', order_number);
var todaysDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy");
copyBody.replaceText('keyTodaysDate', todaysDate);
copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId);
var theblob = pdf.getBlob().getAs('application/pdf');
var folder = DriveApp.getFolderById('0B3nrCN8N5OBcRnlWaUlHZUxZNE0');
var movefile = folder.createFile(theblob);
DriveApp.removeFile(movefile);
var subject = "Text goes here" + order_number
var body = "Hello " + first_name + " " + last_name + "," + "<br /><br />"
+ "Text goes here" + "<br /><br />"
+ "Text goes here"
+ "Text goes here"
+ "Text goes here"
+ "Text goes here"
+ "Text goes here"
+ "Text goes here"
+ "Text goes here"
+ "Text goes here"
var cc = "test#test.com";
MailApp.sendEmail(customer_email, subject, body, {htmlBody: body, attachments: pdf, cc: cc});
DriveApp.getFileById(copyId).setTrashed(true);
}
I have a feeling that Google updated something else and I need to change my code, but I'm not sure - I am pretty sure that it is something broken in this section due to the summary of failures notification I received: 5/25/15 8:59 AM onFormSubmit We're sorry, no servers are currently available. Please wait a bit and try again. (line 41, file "RefundByCheckCode")
var movefile = folder.createFile(theblob);
Any thoughts or suggestions?
This appears to be Issue 3206, "Sending mail with attachment of Drawing (as PDF) consistently fails". Visit that defect and star it for updates. There doesn't appear to be a new scenario in your script, but if you have any additional info that might help the Googler who is responsible for fixing this problem, you could add a comment to the issue tracker.
Since the problem seems to arise some time after a script has been running successfully, and is then persistent, I suggest duplicating your script, and decommissioning the original.
Edit: OP was able to get around the problem by deploying a replacement of their template file.
Related
I am trying to build a GSM Gmail addon that will open a card in the compose window and have several fields and then will generate a template and add it to the email. I have several variables containing HTML content and several containing fields from the card. I have almost gotten that done. The last thing that I need to do is to specify a subject, that will be the same every time, and specify recipients that will be based on a text field in the card.
Here is my code. I have 2 files, one gs code file, and one json manifest file.
Manifest.json:
{
"timeZone": "America/Chicago",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": ["https://www.googleapis.com/auth/gmail.addons.current.action.compose", "https://www.googleapis.com/auth/gmail.addons.current.message.readonly", "https://www.googleapis.com/auth/gmail.addons.execute", "https://www.googleapis.com/auth/script.locale"],
"runtimeVersion": "V8",
"addOns": {
"common": {
"name": "Review Published Email Template",
"logoUrl": "https://goodbookreviews.page/Logo.png",
"useLocaleFromApp": true,
"universalActions": [{
"label": "Book Review ",
"openLink": "https://www.goodbookreviews.page"
}]
},
"gmail": {
"contextualTriggers": [{
"unconditional": {
},
"onTriggerFunction": "onGmailMessage"
}],
"composeTrigger": {
"selectActions": [{
"text": "Use Template",
"runFunction": "onGmailCompose"
}],
"draftAccess": "NONE"
}
}
}
}
code.js:
function onGmailCompose(e) {
console.log(e);
var header = CardService.newCardHeader()
.setTitle('Use Template')
.setSubtitle('Use the template for sending an email after a review has been published.');
// Create text input for entering the cat's message.
var input = CardService.newTextInput()
.setFieldName('email')
.setTitle('Email')
.setHint('What is the readers email address?');
var input2 = CardService.newTextInput()
.setFieldName('FName')
.setTitle('First Name')
.setHint('What is the readers first name?');
var input3 = CardService.newTextInput()
.setFieldName('BookTitle')
.setTitle('Reviewed Book Title')
.setHint('What is the title of the book reviewed?');
var input4 = CardService.newTextInput()
.setFieldName('BookAuthor')
.setTitle('Reviewed Book Author')
.setHint('Who is the author of the book reviewed?');
// Create a button that inserts the cat image when pressed.
var action = CardService.newAction()
.setFunctionName('useTemplate');
var button = CardService.newTextButton()
.setText('Use Template')
.setOnClickAction(action)
.setTextButtonStyle(CardService.TextButtonStyle.FILLED);
var buttonSet = CardService.newButtonSet()
.addButton(button);
// Assemble the widgets and return the card.
var section = CardService.newCardSection()
.addWidget(input)
.addWidget(input2)
.addWidget(input3)
.addWidget(input4)
.addWidget(buttonSet);
var card = CardService.newCardBuilder()
.setHeader(header)
.addSection(section);
return card.build();
}
function useTemplate(e) {
console.log(e);
var email = e.formInput.email;
var FName = e.formInput.FName;
var Title = e.formInput.BookTitle;
var Author = e.formInput.BookAuthor;
var now = new Date();
var htmlIntro = '<p>Hello, </p>';
var html2 = '<p> Thank you for writing a book review at Good Book Reviews on</p>';
var html3 = '<p>by</p>';
var html4 = '<p>. You Review has been published to our site. Any personal information you included was NOT published, including first name, last name, age, and email address. Only info you wrote about the book was published. You can see it right here! If you need anything else, feel free to contact us at support#goodbookreviews.page or reply to this email to contact us. <br> Happy Reading,<br> The Book Review Team</p>';
var message = htmlIntro + FName + html2 + Title + html3 + Author + html4;
var response = CardService.newUpdateDraftActionResponseBuilder()
.setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction()
.addUpdateContent(message, CardService.ContentType.MUTABLE_HTML)
.setUpdateType(CardService.UpdateDraftBodyType.IN_PLACE_INSERT))
.build();
return response;
}
function onGmailMessage(e) {
console.log(e);
var header = CardService.newCardHeader()
.setTitle('Unavailable')
.setSubtitle('Open the compose window to use template');
var card = CardService.newCardBuilder()
.setHeader(header);
return card.build();
}
Can someone please tell me how to do this? Thanks!
Your "p" tags create extra newlines. Try something like
var htmlIntro = '<p>Hello, ';
var html2 = 'Thank you for writing a book review at Good Book Reviews on ';
var html3 = ' by ';
var html4 = '. You Review has been published to our site. Any personal information you included was NOT published, including first name, last name, age, and email address. Only info you wrote about the book was published. You can see it right here! If you need anything else, feel free to contact us at support#goodbookreviews.page or reply to this email to contact us. <br> Happy Reading,<br> The Book Review Team</p>';
var message = htmlIntro + FName + html2 + Title + html3 + Author + html4;
Or, if you use V8 engine, even simpler
var message = `<p>Hello, ${FName} Thank you for writing a book review at Good Book Reviews on ${Title} by ${Author}. Your Review has been published to our site. Any personal information you included was NOT published, including first name, last name, age, and email address. Only info you wrote about the book was published. You can see it right here! If you need anything else, feel free to contact us at support#goodbookreviews.page or reply to this email to contact us.</p>
<br>
<p>Happy Reading,</p>
<br>
<p>The Book Review Team</p>
`
I am using a google form to send out emails to parents and coaches if students are missing work. The problem I am running into is that the sometimes the coach email is not listed because the student is not in a sport, so it has a blank cell and then the script will stop running. I just want it to skip over and continue moving down the sheet. Also is there a way that I can get a cell to confirm that it sent? like having the cell next to the email say "sent" or "error" if it fails? I'm not really concerned with this part it just would be nice. I'm really new to this. Thanks!
function SendCoachEmails() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Homework Hour 4.0").activate();//take off 4.0 when done with texting
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DO NOT DELETE! Parent Email Template").getRange(1, 1).getValue();
var quotaLeft = MailApp.getRemainingDailyQuota();
if((lr-1) > quotaLeft) {
Browser.msgBox("You have " + quotaLeft + " left and you're trying to send" (lr-1) + " emails. Emails were not sent")
} else {
for (var i = 2;i<=lr;i++){ // i is the current row so in this i is row 2
var currentEmail = ss.getRange(i,12).getValue(); // 15 is the email column
var currentStudent = ss.getRange(i, 4).getValue(); // 4 is student name column
var currentMessage = ss.getRange(i, 2).getValue(); // 2 is the message column
var currentMissingAssingment = ss.getRange(i, 9).getValue(); // 9 is the missing assingment column
var massageBody = templateText.replace("<<Student Name>>",currentStudent).replace("<<Message>>",currentMessage).replace("<<Missing Assingment>>",currentMissingAssingment);
var subjectLine = currentStudent +" Has Homework Hour"; // change whats in the "" to what you need the subject on the email to be
MailApp.sendEmail(currentEmail, currentStudent, massageBody); // to change subject on email change "Has Homework Hour" Part//
}
}
}
You can use a simple condition statement, i.e. check if there is an email before you send
if (currentEmail.trim() !== '')// check if email is not blank
MailApp.sendEmail(currentEmail, currentStudent, massageBody); // to change subject on email change "Has Homework Hour" Part//
}
I have email template which I 'parse' and send (from current Lead form) as a parameter to new email form (from JavaScript).
var parameters = {};
parameters["subject"] = 'Subject name';
parameters["description"] = '<font face="Tahoma, Verdana, Arial" size=2 style="display:inline;"><br></font>';
Xrm.Utility.openEntityForm("email", null, parameters);
or
let serverUrl = "https://companyname.crm4.dynamics.com";
let extraqs = "subject=Subject name";
extraqs += '&description=<font face="Tahoma, Verdana, Arial" size=2 style="display:inline;"><br></font>';
let targetUrl = serverUrl.replace(/\/$/, "") + "/main.aspx?etn=email&pagetype=entityrecord&extraqs=" + encodeURIComponent(extraqs);
parent.open(targetUrl);
or
let serverUrl = "https://companyname.crm4.dynamics.com";
let extraqs = 'subject=' + encodeURIComponent('Subject name');
extraqs += '&description=' + encodeURIComponent('<font face="Tahoma, Verdana, Arial" size=2 style="display:inline;"><br></font>');
let targetUrl = serverUrl.replace(/\/$/, "") + "/main.aspx?etn=email&pagetype=entityrecord&extraqs=" + extraqs;
parent.open(targetUrl);
I get error every time I want to send anything look like as html tag (anything contains '<' or '>' sign).
Is it possible at all, to send my html markup trough the parameters, is there any security issue with this ?
This is solvable with encodeURIComponent / decodeURIComponent like this:
parameters["description"] = encodeURIComponent('<html here>');
And on the other side:
var description = decodeURIComponent(incomingParameterHere);
In this fashion your HTML passes through as a simple string. This can(should?) be applied to all strings being passed around via JS.
What I found what that for description to contain html tags it needs the pId and pType values defined (wonder if this is by design or a bug)
var entityFormOptions = {
entityName: "email"
};
var emailFormParams = {
subject: "subject",
description:"<p1>html here</p1>",
//sets the regarding - needed for description to be html
pId:"{GUID}",
pType:112 //objectTypeCode for the party
};
Xrm.Navigation.openForm(entityFormOptions, emailFormParams);
I have been trying to edit a script that I have to bold certain text. This script pulls information from a Google Form Spreadsheet and returns an email that looks like this:
Teacher Engagement:
At initial observation, the teacher is appropriately engaged (Direct Instruction, Modeling, Constructing Knowledge, Guided/Independent Practice, etc.)
Technology Integration:
No evidence of technology use or integration
I would like for the headers "Teacher Engagement:" & "Technology Integration" to be Bold and maybe underlined.How can I implement HTML into my code?
My code is:
function sendEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRowIndex();
var userEmail = sheet.getRange(row,
getColIndexByName("Username")).getValue();
var body = "Below are the results of a recent Walkthrough: ";
body += "\n\nTeacher Engagement: \n" + sheet.getRange(row,
getColIndexByName("Teacher Engagement")).getValue();
body += "\n\nTechnology Integration: \n" + sheet.getRange(row,
getColIndexByName("Technology Integration")).getValue();
MailApp.sendEmail(userEmail, subject, body, {name:"Classroom Walkthrough"});
}
The only viable solution to format an E-mail with the MailApp is to use the HTMLbody option and to write your texte as HTML.
example :
function sendEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRowIndex();
var userEmail = sheet.getRange(row,
getColIndexByName("Username")).getValue();
var body = "<HTML><BODY>"
+"Below are the results of a recent Walkthrough: "
+"<P>Teacher Engagement: <BR>"
+sheet.getRange(row,getColIndexByName("Teacher Engagement")).getValue()
+"</P>"
+"<P>Technology Integration: <BR>"
+sheet.getRange(row,getColIndexByName("Technology Integration")).getValue()
+"</P></BODY></HTML>";
MailApp.sendEmail({
to:userEmail,
subject:subject,
htmlBody:body,
name:"Classroom Walkthrough"
});
}
result :
Below are the results of a recent Walkthrough:
Teacher Engagement:
stuff
Technology Integration:
other stuff
I made some modifications to a Google Script I found online, but not knowing how to script so well, I'm sure I'm missing something here.
My goal is to have all the information submitted through a Google Form to then be emailed to me or a group I'll create.
This script here does email me the info, but it's all added as a single line without even mentioning the questions.
I'm a total newb, but I'm sure this could be solved with some kind of command or event that I'm unaware of.
Take a look:
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First column of data to process
var numRow = 1; // Number of columns to process
var dataRange = sheet.getRange(startRow, 1, numRow, 10)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var message1 = row[1,2,3,4,5];
var message2 = row[2];
var message3 = row[3];
var message4 = row[4];
var message5 = row[5];
var message6 = row[6];
var message7 = row[7];
var message8 = row[8];// Second column
var emailSent = row[10]
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "New Hire On The Way!";
MailApp.sendEmail("itgroup#mycompany.com",subject,message1+message2+message3+message4+message5+message6);
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
}
}
}
That whole "EMAIL_SENT" was my attempt at having the script not resend info that was already entered.
If there is a better way of doing this, I'd love to hear it.
Thank you so much!
You can set this function as a trigger for onFormSubmit in the response sheet.
Source: Get Google Forms data in an Email Messages
function SendGoogleForm(e) {
var email = Session.getActiveUser().getEmail();
var subject = "Google Docs Form Submitted";
var s = SpreadsheetApp.getActiveSheet();
var columns = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
for ( var keys in columns )
message += columns[keys] + ' :: '+ e.namedValues[columns[keys]] + "\n\n";
MailApp.sendEmail(email, subject, message);
}
if your just wanting the email to have a little more structure you can just add html to the individual messages. Depending how crazy your want to get is up to you. Here is a simple change that you can try it should have a little more structure. Replace this with your if statement
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "New Hire On The Way!";
MailApp.sendEmail("itgroup#mycompany.com",subject,message1+"<br />"+"<br />"+"<br />"+message2+"<br />"+"<br />"+"<br />"+message3+"<br />"+"<br />"+"<br />"+message4+"<br />"+"<br />"+"<br />"+message5+"<br />"+"<br />"+"<br />"+message6+"<br />"+"<br />"+"<br />");
sheet.getRange(startRow + i, 10).setValue(EMAIL_SENT);
}
All I did here is add breaks between your messages. This should create some spaces. You can always go crazy with inline css and html. www.w3schools.com is always a great place to start for beginners. Good Luck and happy coding!