I would like to remove the html formatting in my google apps script. I am currently searching the email and printing the results to a google spreadsheet. I would like to know if there is a way to replace text.I am aware of regex but I dont think it works with the getBody function.
I would really appreciate some feedback or some help on this matter.
Code:
function Search() {
var sheet = SpreadsheetApp.getActiveSheet();
var row = 2;
// Clear existing search results
sheet.getRange(2, 1, sheet.getMaxRows() - 1, 4).clearContent();
// Which Gmail Label should be searched?
var label = sheet.getRange("F3").getValue();
// Get the Regular Expression Search Pattern
var pattern = sheet.getRange("F4").getValue();
// Retrieve all threads of the specified label
var threads = GmailApp.search("in:" + label);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var msg = messages[m].getBody();
// Does the message content match the search pattern?
if (msg.search(pattern) !== -1) {
// Print the message subject
sheet.getRange(row,3).setValue(messages[m].getBody());
From this answer: Google Apps Scripts - Extract data from gmail into a spreadsheet
You can forgo the getTextFromHTML function altogether by simply using getPlainBody(); instead of getBody();.
Replace this:
With this:
The
getTextFromHtml()
function has been adapted from this answer, with the addition of handling for some basic formatting (numbered & bullet lists, paragraph breaks).