Of course! You're running into two very common and frustrating issues with Google Apps Script: email formatting and date formatting.
The Line Break Issue: The \r\n characters are for plain text emails. Many modern email clients will ignore these and collapse all the text into a single block. The solution is to send the email as HTML, where you can use the <br> tag to force a line break.
The Date Style Issue: When a Google Form has a date or date-time question, Apps Script receives it as a standard JavaScript Date object. When you add this object to a string, it defaults to a very long, technical format (e.g., Fri May 24 2024 09:00:00 GMT+0100 (British Summer Time)). The solution is to detect when an answer is a date and use the Utilities.formatDate() service to style it exactly how you want.
Here is the revised version of your script that fixes both of these issues.
Copy and paste this code over your existing onFormSubmit function.
function onFormSubmit(e) {
// Get the form responses from the event object
var responses = e.response.getItemResponses();
// --- Start building the HTML content for the emails ---
// We use HTML to get proper line breaks and formatting.
var emailBody = "Your submission has been received.<br><br>" +
"Regards,<br>Main Reception<br><br>" +
"<i>Any issues? Please email mainreception as this email box is not monitored.</i><br><br>" +
"<b>Your submitted details:</b><br><br>";
var detailsForNotification = "<b>New request below:</b><br><br>";
var userEmail = "";
// Process responses
for (var i = 0; i < responses.length; i++) {
var question = responses[i].getItem().getTitle();
var answerObject = responses[i].getResponse();
var formattedAnswer; // We'll store the final, formatted answer here
// --- FIX FOR DATE FORMATTING ---
// Check if the answer is a Date object
if (answerObject instanceof Date) {
// It's a date! Let's format it nicely.
// You can change "dd/MM/yyyy HH:mm" to your desired format.
// Examples: "dd/MM/yyyy", "d MMMM yyyy", "yyyy-MM-dd"
formattedAnswer = Utilities.formatDate(answerObject, Session.getScriptTimeZone(), "dd/MM/yyyy HH:mm");
} else {
// It's not a date, so use the answer as is.
formattedAnswer = answerObject;
}
// --- FIX FOR LINE BREAKS ---
// Append the question and formatted answer using HTML <br> tag for a new line.
emailBody += "<b>" + question + ":</b> " + formattedAnswer + "<br>";
detailsForNotification += "<b>" + question + ":</b> " + formattedAnswer + "<br>";
// Capture user email if applicable (this logic remains the same)
if (question.toLowerCase().includes("email")) {
userEmail = formattedAnswer;
}
}
// --- Send emails using the 'htmlBody' option ---
// Send confirmation to visitor if email is found
if (userEmail) {
MailApp.sendEmail({
to: userEmail,
subject: "Thank you for your request",
htmlBody: emailBody // Use htmlBody instead of body
});
} else {
Logger.log("No email address found in the form submission.");
}
// Send notification emails to recipients
var recipients = ["mainre...@yourdomain.uk", "carpa...@yourdomain.uk"]; // Use your actual domains
recipients.forEach(function(email) {
MailApp.sendEmail({
to: email,
subject: "New visitor request",
htmlBody: detailsForNotification // Use htmlBody instead of body
});
});
}
// **Setup Trigger Automatically** (Run this manually once from the Script Editor)
// You do not need to run this again if your trigger is already working.
function createFormSubmitTrigger() {
var form = FormApp.openById('1B2tvU0AtJ4-waguZuldjibNlE2OT2fCU83jwkqCW6sc');
// Deletes any existing triggers to avoid duplicates
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() === "onFormSubmit") {
ScriptApp.deleteTrigger(triggers[i]);
}
}
// Creates a new trigger
ScriptApp.newTrigger("onFormSubmit")
.forForm(form)
.onFormSubmit()
.create();
}
Using HTML for Emails:
Old: Your email text was being built as a single string with \r\n for new lines.
New: The script now builds an HTML string. We replaced \n and \r\n with the HTML line break tag: <br>. I also made the questions bold with <b>...</b> to improve readability.
When sending the email, the key change is using the htmlBody option instead of body:
MailApp.sendEmail({
to: userEmail,
subject: "Thank you for your request",
htmlBody: emailBody // <--- THIS IS THE IMPORTANT CHANGE
});
Formatting the Date:
Inside the for loop, we now check if the answer from the form is a Date object using instanceof Date.
If it is a date, we use Utilities.formatDate(date, timezone, format) to convert it into a clean, readable string.
answerObject: The original date from the form.
Session.getScriptTimeZone(): This automatically gets the correct timezone from your project settings.
"dd/MM/yyyy HH:mm": This is the part you can customize!
dd/MM/yyyy would give you 24/05/2024
d MMMM yyyy would give you 24 May 2024
EEE, d MMM yyyy would give you Fri, 24 May 2024
If the answer is not a date (e.g., a name or a phone number), the script uses the answer as it is.
Go to your Apps Script project.
Replace your entire onFormSubmit function with the new code provided above.
Customize the date format string in the Utilities.formatDate() line if you wish.
Save the script project.
Your trigger is likely already set up, so you don't need to run the createFormSubmitTrigger function again. The next time someone submits the form, the emails should be perfectly formatted.
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-apps-script-community/88a4d1dc-adb6-49a5-bb13-b2add7a30cf1n%40googlegroups.com.
Not sure why you are using \r\n. Just use \n\n in the two places in your for loop.
That should do it.