Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove formatting tags from string body of email

How do you remove all formatting tags when calling:

GmailApp.getInboxThreads()[0].getMessages()[0].getBody()

such that the only remainder of text is that which can be read.

Formatting can be destroyed; the text in the body is only needed to be parsed, but tags such as:

"&" 
<br>

and possibly others, need to be removed.

like image 608
Christopher Markieta Avatar asked Nov 30 '22 15:11

Christopher Markieta


1 Answers

Even though there's no DOM in Apps Script, you can parse out HTML and get the plain text this way:

function getTextFromHtml(html) {
  return getTextFromNode(Xml.parse(html, true).getElement());
}

function getTextFromNode(x) {
  switch(x.toString()) {
    case 'XmlText': return x.toXmlString();
    case 'XmlElement': return x.getNodes().map(getTextFromNode).join('');
    default: return '';
  }
}

calling

getTextFromHtml("hello <div>foo</div>&amp; world <br /><div>bar</div>!");

will return

"hello foo& world bar!".

To explain, Xml.parse with the second param as "true" parses the document as an HTML page. We then walk the document (which will be patched up with missing HTML and BODY elements, etc. and turned into a valid XHTML page), turning text nodes into text and expanding all other nodes.

This is admittedly poorly documented; I wrote this by playing around with the Xml object and logging intermediate results until I got it to work. We need to document the Xml stuff better.

like image 125
Corey G Avatar answered Dec 04 '22 07:12

Corey G