0

I have email body where there is a table which has "Client Time" as the heading of first left Column. enter image description here

I want to extract this whole table but am getting Null with following exec.

let regex = /<tr><td><b>Client Time([\S\s]+)<table/;
    Logger.log(regex.exec(tempbody));

Here is the extra code but that should be fine.

if ((table = regex.exec(tempbody)) !== null) {
      row_regex = new RegExp(/<tr>(.+)<\/tr>/g);
      let data, tempdata, rows, cell;
      Logger.log(data);
      while ((rows = row_regex.exec(table[1])) !== null) {
        data = []
        cell_regex = new RegExp(/<td.*?>(.+?)<\/td>/g);
        while ((cell = cell_regex.exec(rows[1])) !== null) { 
          data.push(cell[1]);
        }
        if (!tempdata || (tempdata && tempdata.length === data.length)) { 
          sheet.appendRow(data);
        }
        tempdata = data;
      }
      inProcessLabel.removeFromThread(threads[i]);
    }

What change do I need to do in regex, sorry I don't understand regular expressions much but believe that this same code worked for me in past.

Abdul
  • 45
  • 6

1 Answers1

2

Using regular expressions to parse HTML is not a good idea (for a number of reasons).

We have V8 now so you can simply add a proper HTML/XML parser library (written in pure Javascript with minimal dependencies) to your Apps Script project. Just get the library source in full or minified form and add it as its own script file.

Here are a few good options:

TheAddonDepot
  • 8,408
  • 2
  • 20
  • 30
  • I understand but I have got this script work in past (on another gmail account where it still works) and also email html body is very much consistent. So for now I need to get this work here. – Abdul Apr 18 '20 at 21:22
  • Hello @Abdul, just like the TheAddonDepot has mentioned in their references: "Entire HTML parsing is not possible with regular expressions, since it depends on matching the opening and the closing tag which is not possible with regexps". So what exactly do you mean by "I have got this script to work in the past"? Cheers! – ale13 Apr 20 '20 at 09:03
  • Could you add a answer to this question? https://stackoverflow.com/questions/49204546 – TheMaster Aug 19 '20 at 15:40
  • 1
    @TheMaster [Use browserify to convert the npm module to code that can be embedded in a GAS script.](https://wzrd.in/standalone/string-similarity@latest) – TheAddonDepot Aug 21 '20 at 16:41
  • @TheAddonDepot I'm looking for a canonical question and answer, so others can be referred to that. I haven't used modules much. The answers in that question seem to be outdated. So, If you add a latest canonical(generalized) answer(You seem to have better experience here), that'll be helpful to the community. – TheMaster Aug 21 '20 at 16:51
  • Or even to this [question](https://stackoverflow.com/questions/63489071) – TheMaster Aug 21 '20 at 16:54