Remove formatting tags from string body of email

前端 未结 5 591
臣服心动
臣服心动 2020-12-03 02:38

How do you remove all formatting tags when calling:

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

such that the only remainder o

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 03:01

    I noticed you are writing a Google Apps Script. There's no DOM in Google Apps Script, nor you can create elements and get the innerText property.

    getBody() gives you the email's body in HTML. You can replace tags with this code:

    var html = GmailApp.getInboxThreads()[0].getMessages()[0].getBody();
    html=html.replace(/<\/div>/ig, '\n');
    html=html.replace(/<\/li>/ig, '\n');
    html=html.replace(/
  • /ig, ' *'); html=html.replace(/<\/ul>/ig, '\n'); html=html.replace(/<\/p>/ig, '\n'); html=html.replace(//ig, '\n'); html=html.replace(/<[^>]+>/ig, '');
  • May be you can find more tags to replace. Remember this code isn't for any HTML, but for the getBody() HTML. GMail has its own way to format de body, and doesn't use every possible existing tag in HTML, only a subset of it; then our GMail specific code is shorter.

提交回复
热议问题