I have a Google Form which I would like to automatically email someone when a new response is submitted. So far, I have just a simple HTML page with text in the body, however I would like the email content to include the form data as well.
Currently, this is what I have written:
function sendEmail(e) {
//response
var html = HtmlService.createTemplateFromFile("email.html");
var htmlText = html.evaluate().getContent();
var emailTo = "jeffreyabr#gmail.com"
var subject = "New SAP Role Request"
var textBody = "This email requires HTML support. Please make sure you open it with an email client that supports HTML"
var options = {htmlBody: htmlText};
GmailApp.sendEmail(emailTo, subject, textBody, options);
This came from following this basic YouTube tutorial.
Is there more Google Apps Script that I can add to accomplish this? Can I do this from Forms or must I do it from within Sheets?
The e.response object also contains the form data, which can be accessed by using e.response.getItemResponses().
Then to get the question, use getItem().getTitle(). To get the answer, use getResponse().
If you do not need the HTML response, then you can append the questions and answers to the textBody to display them on the email. Otherwise, you would have to add a script in your email.html using HTML scripts or google.script.run.
References:
Event Objects | onFormSubmit(e)
Class FormResponse
Related
I have an HTML form which saves its responses to a Google Sheet. The form contains the following fields:
Name:
Email:
Subject:
Message:
Now, what I want is I want to automate sending a thank you email to a recipient as soon as he/she fills the form to the address mentioned in the "Email" field. I don't want to use Google Forms as the backend as it is hard to bypass the "Response Recorded" Confirmation page. Also, avoiding PHP would be better for me!
Is there a way to send these e-mails automatically? The format of the email is as follows:
From: <my email address>
To: <email address from the "Email" field in the spreadsheet>
Subject: Re: Submission Received
Hey <name from the "Name" field in the spreadsheet>!
Body of the mail
If there is a way to bypass the confirmation page of Google Forms, please let me know! That would also do!
If you have the data in google sheets, then you can handle your automation there. I setup this sample file that you could probably base your data set on.
From there I attached the below script to the spreadsheet. You would then need to set a trigger to execute this script on a somewhat regular frequency (5 minutes? 1 minute?). I think it should accomplish what you are going for. There's a check built in to ensure that partial data is not sent.
const ss = SpreadsheetApp.getActiveSheet();
const sentMessageColumn = ss.getRange("E:E").getColumn();
function regularProcedure(){
var aCell = ss.getRange(ss.getLastRow(),sentMessageColumn)
while(aCell.isBlank()){
var eRow = aCell.getRow();
if(!(ss.getRange(eRow,2).isBlank() ||
ss.getRange(eRow,3).isBlank() ||
ss.getRange(eRow,4).isBlank()))
{
var newMail = GmailApp.createDraft(
ss.getRange(eRow,2).getValue(),
ss.getRange(eRow,3).getValue(),
ss.getRange(eRow,4).getValue());
newMail.send();
aCell.setValue(true);
}
aCell=aCell.offset(-1,0);
}
}
I have quite a long email template I want to automatically send to welcome new users.
The script copies the body of a Google Doc and uses that as the body of the email using MailApp.sendEmail.
The issue is that the email that arrives is very narrow and doesn't copy exactly what is in the template. Is there any way of formatting this to make it the same as the Google Doc template?
Any help much appreciated
var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/"doc id"/edit");
var body = doc.getBody().getText();
var message = body;
var subject = "subject line";
MailApp.sendEmail (user.primaryEmail, subject, message)
I learned something new with this:
When you send an email as a plain text email it won't let you control where the line breaks are
It actually adds line breaks when you call the .getText() method, as well as part of the .sendEmail method when you are sending it just as plain text.
The easiest solution is to send it as an HTML message. I experimented with this and have what I believe the easiest solution for it below:
//this will replace the line breaks with html line breaks
var htmlBody = doc.getBody().getText().replace(/\n/g,'<br/>');
var message =
{
to: user.primaryEmail,
subject: 'subject line',
htmlBody: htmlBody
}
MailApp.sendEmail ({message})
I've tested this and this should fix your problem.
I want to email a form to customers to fill out in the browser or ideally in rheir email client . I have created a form opened it by id , then sent it using
function openDialog() {
// Open a form by ID.
var form = FormApp.openById('kdhgsdfskfhs');
var url = form.getPublishedUrl();
var response = UrlFetchApp.fetch(url);
var htmlBody = HtmlService.createHtmlOutput(response).getContent();
MailApp.sendEmail({
to: 'me#gmail.com',
subject: 'hi',
htmlBody: htmlBody
});
}
You can see the result which I have put in screenshot . I'm starting to think that this is not doable in apps script. Is it possible ? if not what is the best way to send allow a user to enter info into a spreadsheet?
It's possible email html. But I think your going to need a website or a webapp or a link to the online form to submit it to a spreadsheet.
The following code isn't working, giving the following error:
TypeError: Cannot find function getFileByID in object Drive.
I'm trying to first test by directly providing ID in the script:
var atta = DriveApp.getFileByID('1XD...c'); //ID is Folder and Google Doc ID I want to attach
MailApp.sendEmail(emailto, subject, message, {
cc: emailcc,
attachments:[atta]
});
Ultimately, I want to read in the link from a Google Spreadsheet:
var range = active_actions.getRange(i+1,16) //active_actions is the spreadsheet and i+1,16 is the cell with the link
var link = range.getFormula();
var atta = DriveApp.getFileByID(link);
MailApp.sendEmail(emailto, subject, message, {
cc: emailcc,
attachments:[atta]
});
This isn't too difficult. Google Docs/Spreadsheets/etc. CANNOT be placed as an attachment in an E-mail. This is because it isn't actually a 'physical document'. It only exists on the cloud. Instead, put the URL in the email, and it will give the receiver a little box at the bottom with the Doc looking like an attachment.
It's not perfect, but it is good enough. For more details, you can see this issue request or Google's App Script Issue page:
https://code.google.com/p/google-apps-script-issues/issues/detail?id=585&q=attachment%20types&sort=-stars&colspec=Stars%20Opened%20ID%20Type%20Status%20Summary%20Component%20Owner
EDIT (To explain data fetching in SS):
To get data out of a range object, (text, number, etc.), you should use this format.
var range = getRange();
var rangeData;
var cellValue;
while (range.hasNext()) {
rangeData = range.next();
cellValue = rangeData.getValue();
}
If you an accumulator variable, you can get every bit of data in one list/string/etc.
Now that you have your data, you can E-mail it, or do anything else with it.
I have looked at other similar posts and got some help with setting up trigger correctly but still have an error.
I have tried to automate sending a pdf doc to be emailed in response to a form submission in google apps. I used a script shared on web to get started and have fixed some deprecated terms.
I created a document template with keyItems embedded as placeholders and I created a form to capture data. The spreadsheet associated with the form is capturing data as planned. I edited the script associated with the spreadsheet to hopefully do the work required on submission of the form. The trigger looks correctly set up to run on form submit from the spreadsheet. My question is about the error I get repeatedly regarding the e.values being undefined. I assumed that I would be labelling these as e.values[0] and onwards when I declared the variables - i.e. they would be based on their order in the array drawn from the spreadsheet fields. Sorry to be long winded. If i place dummy content in the variables it works and sends the pdf so the actual mechanism works ok but my e.values are not working.
Here is my script
// Certificate of Completion
// Get template from Google Docs and name it
var docTemplate = "1V_PqCBZ9vWmmIELp47X12iNtdwRCqHyOyey6VPL0o0Q";
var docName = "CertificateOfCompletion";
//When form submitted get info from form and set as variables
function onFormSubmit(e) {
var name = e.values[2];
var email = e.values[3];
var date = e.values[4];
//make temporary working copy of template doc for the submitter
var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName+' for '+name).getId();
//open the temporary doc
var copyDoc = DocumentApp.openById(copyId);
// tell script to go into the doc
var copyBody = copyDoc.getBody();
//replace placeholders in the doc with data from a submitted form
copyBody.replaceText('keyFullName', name);
copyBody.replaceText('keyDate', date);
copyDoc.saveAndClose();
//create PDf
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
//send email to recipient
var subject = "Certificate";
var body = "Congratulations!" + name + " Here is your certificate of completion " ;
MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});
//delete temporary document
DriveApp.getFileById(copyId).setTrashed(true); }
Have you tried using named values instead.
Heres an example
var handling = Number(e.namedValues["Call Handling"]);
var security = Number(e.namedValues["Security"]);
var agentName = String(e.namedValues["Agent Name"]);
These are questions in a form I have. The bit after the e.namedValues["text here"]); , replace text here with whatever your column is called in form responses, this will be the question name.
I'm not sure why e.values[0] onwards isn't working for you as it works for me.
My script is in the spreadsheet, with an onFormSubmit trigger installed via the menu.
Also bear in mind, to test your script, you must sumbit a form, hitting the play button won't work as there is no event to pass to your function.