How can I fix emails googlesheets - javascript

Help. I'm trying to send an automated email using this code.
function checkIn() {
var aemail=SpreadsheetApp.getActiveSpreadsheet();
var tracker= aemail.getSheetByName("Tracker");
var template= aemail.getSheetByName("Templates");
var message=template.getRange('B3').getValue();
var signature= template.getRange('C3').getValue();
for (var i=2; i<=2;i++) {
var sname =tracker.getRange('A'+i).getValue();
var subject = tracker.getRange('B'+i).getValue();
var eaddress = tracker.getRange('C'+i).getValue();
var finalmsg = ""
finalmsg="Hello"+""+sname+","+"\n"+"\n"+message+"\n"+signature;
MailApp.sendEmail(eaddress,subject,finalmsg);
}
}
The email sends but the text alignment is to the right and does not expand to the email space. How can I fix this? I fixed it once but can not remember how. I know it's not code-related.

It looks like it will depend on the data in the range B3. It probably has newlines included in it - if its not that then maybe its best to share a sample sheet so that it can be tested.
You can also try and add this code to replace newlines with spaces
Change
var message=template.getRange('B3').getValue();
To
var message=template.getRange('B3').getValue().replaceAll("\n", " ")
If you want more control over how things look, you can also try using an HTML body with the sendEmail method. Using it like this:
MailApp.sendEmail('mike#example.com', 'example', 'body without html', {
htmlBody:"<h1>Body with HTML</h1>"
});
The normal body is included since not all clients render HTML, so if a particular mail client does not render HTML, then it will use the plain text version.
That said, I do think that your issue is to do with newline characters \n making their way into your message.
Reference
replaceAll
sendEmail
EDIT
I was wrong.
After running some tests it is true that if your Email is completely plain text, then Gmail will automatically insert line breaks.
You will need to use the options and htmlBody parameters mentioned above.
You can use it like this:
function sampleEmail() {
var message="Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks Long Text without line breaks "
MailApp.sendEmail("[YOUR EMAIL]","test", message,{htmlBody:"<p>"+ message +"</p>"});
}
The arguments are as follows:
The email of the recipient
the subject
the plain text message (as a fall back in case the client's email does not support HTML)
the options parameter with one attribute htmlBody where the message is simply wrapped with <p> HTML tags.
This text will now adapt and fill the width of the screen in an email client that supports HTML, like Gmail.

Related

Preserve white space line breaks in tinymce wysiwyg editor

I have a tinymce editor implemented in my react project installed through the package
"#tinymce/tinymce-react": "^3.12.6".
I am trying to preserve the white space line breaks of the data which comes from external source (excel file) when being edited in the wysiwyg editor but I have not been able to do so. I have tried the options such as force_br_newlines and convert_newlines_to_brs but it does not seem to help
Scenario explanation in detail:
I have an excel file which has multiline text which gets imported to the app. The multiline text is preserved in the database and I get the text displayed in multi lines when I log it to the console. (The console does not output new line characters line \n, \r,etc and just white space line breaks like in the original text). But, when I edit the same data with tinymce editor, the tinymce editor puts all the data in one line.
The original text is not html text and we cannot expect the end user to type HTML tags inside the excel file such as
<p>...</p> or <br />
Example data in the excel file:
This is line one
This is line two
This is line three
Data when it gets displayed in the editor:
This is line one This is line two This is line three
I would like the editor to preserve the line breaks. Is that possible? How can it be achieved? Your help would be really appreciated. Thanks in advance.
As far as I know, there's no way to achieve that with TinyMCE, for the reasons outlined by #micheal-fromin.
The best you can do is search for new lines (\n and/or \r) in the source text and replace them with HTML line breaks (</br> ).
PHP solution
PHP has a very handy function that does exactly that: nl2br().
The problem with nl2br is that it will also replace newlines within HTML tags. If that's an issue for you, I recommend replacing blocks of text with paragraph tags (<p>) using either autop() (pure PHP) or wpautop() (in WordPress).
Javascript solution
In Javascript, nl2br translates to:
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>'; // Adjust comment to avoid issue on phpjs.org display
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}
TinyMCE is an HTML editor so when you pass it a string of plain text it is converting it to HTML. As the newline characters etc are not valid HTML they are simply removed.
If you have newlines in your content you could convert them to some sort of valid HTML.
When you extract content from TinyMCE you have an option to get plain text and it will convert paragraphs (<p> tags) to 2 newlines (\n\n) and line breaks (<br> tags) to one newline (\n). You could do the reverse with your text file to create HTML that represents the line breaks appropriately.

How to append newline to text value in javascript array?

I have a narrow button that contains an image and label text. The state of the button changes to one of several values which are stored as text in an array and then changed out with textContent.
One of the values forces a line break in the label text. I would like to reserve the "blank line" below my label so that layout isn't affected every time the label breaks to two lines of text. To accomplish this, I'm trying to append a newline to every single-line value. For layout reasons, I can't simply pad the container — I need it to match the height of a line of formatted text.
Is there any way to put a newline into a text array value? I've tried adding a CR to my text both within the array and prior to the array as a variable using:
Labelname + \n
Labelname\n
Labelname<br /> (HTML, I know)
var label = 'Labelname' + String.fromCharCode(13)
Nothing seems to make the newline "stick," and the console reveals the value "Labelname" without the newline.
HTML generally ignores whitespace (including newline characters). To have it rendered, you need to use an element that doesn't ignore whitespace (like <pre>), or opt in to <pre>-style whitespace handling via CSS with white-space: pre-wrap.
I ended up using innerHTML to put formatted text into the element. I was resistant to using it for various reasons, but it provided a straightforward solution to this problem.

Unable to append space to textarea content

This one is a strange one and I simply cannot get my head around it. We are trying to append space to a textarea content using the following jquery,
var checkThis ='help';
$('#msg').val(checkThis+' ');
$('#msg').focus();
But when we focus on the text area we can see that the space is not added. Also, the cursor doesn't focus automatically.
I am not able to figure out if there is any other script in the code doing this. This code is actually a large piece of maintenance code, is there anyway I can find what function may be trimming the text?
Try this:
$('#msg').append(checkThis+' ').focus();

How to initialize CKEditor textarea line breaks as like html textarea line breaks

HTML textareas can recognize line breaks (enter button) and i can save the element's text value into database. Then i can get those data, set it on any textarea, i still have line breaks (no problem until here).
CKEditor textarea puts <p> or <br /> after line breaks. When i remove these tags, CKEditor cannot recognize there's a line break.
Purpose : I'm using a word counter plugin with CKEditor, so i need to use CKEditor as like a textarea. I need the plain text value.
I tried these so far :
CKEDITOR.instances.editor1.document.getBody().getText()
Yes i can get the text value correctly, i put this value on DB, but when i reload my page, the line breaks are gone. Because ckeditor works only with html tags ?
config.autoParagraph = false;
This one just removes the <p> tag at the beginning of the content.
I need plain text+line breaks that's it. Any suggestions ? Or should i write down my own classes ?
Array.prototype.map
.call(CKEDITOR.instances.editor1.document.getBody().$.childNodes,
function(item){
return item.innerText;
})
.join("\n");
Just a crazy answer to convert <p> blocks to a string separated by \n

tinymce trims last space

I am writing a module that change content. I use tinymce & i get all content using
var txt = editor.getContent({format: 'text'});
when i do my fixes, i get content via AJAX & put my content using
editor.setContent(results.text);
setContent erases all previous text & put new one but it puts cursor at the start of the line so i used smthg like this:
editor.selection.select(editor.getBody(), true);
editor.selection.collapse(false);
I need to add one space after setContent(results.text) .I need to put cursor after space, but it(tinymce) trims last space & put cursor at last symbol.
Can U advertise me what to do?

Categories