Line Break in Google script [duplicate] - html

With Google Apps Script, how to make a line break in a variable to send mail?

If you're not sending an HTML formatted message, use "\n". I personally despise HTML formatted e-mail.

Newline in msgBox:
Browser.msgBox('line 1 \\n line 2');
Please note you need to escape '\n' with additional backslash.

You should use the <br> tag when sending the HTML portion of the email .
Below is a sample on how I compose the same email body, but formatted differently for HTML & plain text. (Not the best code but hopefully it illustrates the point)
function onFormSubmit(e) {
var subject = "Subject";
// Collect user data
var name = e.values[0];
var email = e.values[1]; // Where user enters his/her email address
// Generate content - Replace this with what you're composing
var content = [];
content.push("Hi " + name);
content.push("Thanks for submitting the survey!___LINE_BREAK___");
content.push("Survey Team");
// Combine content into a single string
var preFormatContent = content.join('___LINE_BREAK___');
// Replace text with \n for plain text
var plainTextContent = preFormatContent.replace('___LINE_BREAK___', '\n');
// Replace text with <br /> for HTML
var htmlContent = preFormatContent.replace('___LINE_BREAK___', '<br />');
MailApp.sendEmail(email ,
subject,
plainTextContent ,
{
name: "Survey Team",
html: htmlContent
});
}

I usually use table in my mail but I think <br /> should work

You can use \r to change line for the GmailApp
\\n to change line for the msgBox.

For HTML, <br> should work.
For javascript/appscript in other google apps like sheetApp,use template literal as MailApp does not respond \n. (i.e) Type the content between two backticks and use 'enter' for new line as follows. No need of using single or double quotes
const recipient = 'abc#xyz.com,cde#xyz.com'
const sub = 'some subject'
const line1 = 'some line1 content'// say 'Name'
const line2 = 'some line2 content'// say 'Contact Number'
const line3 = 'some line3 content'//say 'EMail Id'
const body = `Name: ${line1}
Contact Number: ${line2}
EMail Id: ${line3}`
MailApp.sendEmail(recipient,sub,body)
// There was a mistake with the original answer; I've added "$" for making the code above work.

I try all the answers but they don't work on my computer. I found it better to use GmailApp.sendEmail() instead of MailApp.sendEmail(). It is almost the same, but GmailApp.sendEmail() can change line using \n, while MailApp.sendEmail() can't.
var message = "";
message += "First line of email." + "\n";
message += "Second line of email." + "\n";
message += "Third line of email." + "\n";
message += "Fourth line of email." + "\n";
GmailApp.sendEmail(Session.getActiveUser().getEmail(),"Look at this Title, it work!",message)
This code will send an email to whoever runs this script. It works fine for me.

Related

Send a full formatted mail with google apps script

i am trying to send mails to some list of user using google apps-script.
I am taking the first draft mail and then mailing it to list of users present in the spreadsheet. But when i use ".getplainbody(); " function. It only copies the plain text in the draft.
function sendmail()
{
var drafts = GmailApp.getDraftMessages();
Logger.log(drafts.length);
var draft = drafts[0].getPlainBody();
Logger.log(draft);
GmailApp.sendEmail('abc#gmail.com', 'subject', 'Hello' + '\n ' + draft);
}
I have also tried using the getbody() and then html with the message.
function sendmail()
{
var drafts = GmailApp.getDraftMessages();
Logger.log(drafts.length);
var draft = drafts[0].getBody();
Logger.log(draft);
GmailApp.sendEmail('abc#gmail.com', 'subject', 'Hello' + '\n ' + {html: draft});
}
But this also gives me "[object Object]" in the inbox.
Is there any other option to send the draft mail (not in plain text format)with proper format.
Thanks
Try: GmailApp.sendEmail('abc#gmail.com', 'subject', 'plaintext body', {htmlBody: draft});
You are missing the fourth parameter, 'Hello' + '\n ' + {html: draft} actually concatenates Hello \n with the {html: draft} object.

Adding new line to text in Google spreadsheet cell via apps script

I have an apps script which takes email addresses from one spreadsheet from multiple cells and adds them them to another spreadsheet into 1 cell only.
Currently the email addresses are added to that cell and separated by a ", ".
I would like to add the email addresses into that same cell but add a new line after each address.
I know it is possible to have new lines in a cell when manually adding text, by typing CTRL-Enter.
How can this be achieved in apps script?
I have so far tried to append "\n" or "\r" or "\r\n" to the string, to no avail, so I have reverted my code back to adding ", " instead.
sheet = SpreadsheetApp.getActiveSheet();
sheet.clear();
sheet.appendRow(["Num Emails", "Emails"]);
var LMEmails = "";
var count = 0;
for (var i = 0; i < reviewers.LMEmails.length; i++) {
if (count) {
LMEmails += ", " + reviewers.LMEmails[i];
} else {
LMEmails += reviewers.LMEmails[i];
}
count++;
}
data = [count, LMEmails];
sheet.appendRow(data);
If anyone could help, I would very much appreciate it
Regards
Crouz
After searching for the same question, this was my solution:
var stringToDisplay = 'FirstBit' + String.fromCharCode(10) + 'SecondBit';
then
workSheet.getRange(1,1).setValue(stringToDisplay);
output will be in a single cell, :
FirstBit
SecondBit
Google Spreadsheets seems to ignore the new line if there are no characters after it.
Using "/n" works fine as long as you add at least one other character after it, this could be a space as shown below.
For example:
var myString = 'First Bit' + "\n " + "Second Bit";
It should be:
data = [[count, LMEmails]];

Google Script not sending email as html

I have set up a script to notify users when a new entry has been submitted to a shared spreadsheet. I am trying the following, but the emails are getting sent as plain text:
function formSubmitReply(e) {
var emailAddress = ' *email addresses* ';
var URL = ' *URL to send as link* ';
var message = "<HTML><BODY>"
+ "<P>" + " A new guest complaint has been entered into the database."
+ '<P>To view the spreadhseet, <A HERF="' + URL + '">click here</A>.'
+ "</HTML></BODY>";
MailApp.sendEmail(emailAddress, "New Guest Complaint", "", {htmlBody: message});
}
Any suggestions?
See the sample in the official docs, where it uses the htmlBody parameter: https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(Object)
You are including html and body tags when the parameter expects the body html not an html page. Remove those tags. Also, note that your tags at the end are reversed (/html then /body)

How to remove manual line break

I am getting the data from a form and writing in a google Document. It has fields like Name, Age, Address and so on. Address is of type paragraph text. My question is that while writing the data in a google Doc through a on-submit trigger script, how do I remove manual line breaks in the Address as input by the person filling the form. i.e. I want address to be a continuous string, without the breaks that the person filling the form may have put while typing his address.
ok here are more details.. the Address field in my form is called 'Address', i have a onformsubmit triggered script that reads all the data entered by the person in the form and puts in a google Doc App.
i read the values typed in the form as :
for(var field in e.namedValues) {
message += field + ' :: ' + e.namedValues[field].toString().replace("\n",", ") + "\n";
}
But in my google doc, the lines for the value of Address sre still broken where the person filling the form has broken the lines. i want to eliminate these line breaks and substitute it with commas so the address is continuous.
Change part of the line with the replace function to
e.namedValues[field].toString().replace(/(\r\n|\n|\r)/gm, ", ");
It works!
Remove the line breaks by taking the string that has the text from the paragraph, and passing it through string.Replace("\n", ""); If that doesn't work, then you will need to provide more details.
Also you can work with the range class:
function editRow(r){
var sheet = SpreadsheetApp.getActiveSheet();
var relevantRange = sheet.getRange(r, 21, 1, 1);
artefactRange.setValue(relevantRange.getValue().replace(/(\n)/g, ', '));
}

Line break in a message

With Google Apps Script, how to make a line break in a variable to send mail?
If you're not sending an HTML formatted message, use "\n". I personally despise HTML formatted e-mail.
Newline in msgBox:
Browser.msgBox('line 1 \\n line 2');
Please note you need to escape '\n' with additional backslash.
You should use the <br> tag when sending the HTML portion of the email .
Below is a sample on how I compose the same email body, but formatted differently for HTML & plain text. (Not the best code but hopefully it illustrates the point)
function onFormSubmit(e) {
var subject = "Subject";
// Collect user data
var name = e.values[0];
var email = e.values[1]; // Where user enters his/her email address
// Generate content - Replace this with what you're composing
var content = [];
content.push("Hi " + name);
content.push("Thanks for submitting the survey!___LINE_BREAK___");
content.push("Survey Team");
// Combine content into a single string
var preFormatContent = content.join('___LINE_BREAK___');
// Replace text with \n for plain text
var plainTextContent = preFormatContent.replace('___LINE_BREAK___', '\n');
// Replace text with <br /> for HTML
var htmlContent = preFormatContent.replace('___LINE_BREAK___', '<br />');
MailApp.sendEmail(email ,
subject,
plainTextContent ,
{
name: "Survey Team",
html: htmlContent
});
}
I usually use table in my mail but I think <br /> should work
You can use \r to change line for the GmailApp
\\n to change line for the msgBox.
For HTML, <br> should work.
For javascript/appscript in other google apps like sheetApp,use template literal as MailApp does not respond \n. (i.e) Type the content between two backticks and use 'enter' for new line as follows. No need of using single or double quotes
const recipient = 'abc#xyz.com,cde#xyz.com'
const sub = 'some subject'
const line1 = 'some line1 content'// say 'Name'
const line2 = 'some line2 content'// say 'Contact Number'
const line3 = 'some line3 content'//say 'EMail Id'
const body = `Name: ${line1}
Contact Number: ${line2}
EMail Id: ${line3}`
MailApp.sendEmail(recipient,sub,body)
// There was a mistake with the original answer; I've added "$" for making the code above work.
I try all the answers but they don't work on my computer. I found it better to use GmailApp.sendEmail() instead of MailApp.sendEmail(). It is almost the same, but GmailApp.sendEmail() can change line using \n, while MailApp.sendEmail() can't.
var message = "";
message += "First line of email." + "\n";
message += "Second line of email." + "\n";
message += "Third line of email." + "\n";
message += "Fourth line of email." + "\n";
GmailApp.sendEmail(Session.getActiveUser().getEmail(),"Look at this Title, it work!",message)
This code will send an email to whoever runs this script. It works fine for me.