Simply Converting HTML to Plain Text using Google Apps Script

Gists

This is a sample script for simply converting HTML to plain text using Google Apps Script.

Sample values

HTML (input value)

<div id="sample1">sample text1</div>
<div id="sample2">sample text2</div>
<ul id="sample3">
  <li>sample list 1</li>
  <li>sample list 2</li>
</ul>
<table id="sample4">
  <tbody>
    <tr>
      <td>a1</td>
      <td>b1</td>
      <td>c1</td>
    </tr>
    <tr>
      <td>a2</td>
      <td>b2</td>
      <td>c2</td>
    </tr>
  </tbody>
</table>

Text (output value)

sample text1
sample text2

   - sample list 1
   - sample list 2

a1 b1 c1
a2 b2 c2

Sample script

function myFunction() {
  const sampleHTML = `<div id="sample1">sample text1</div>
<div id="sample2">sample text2</div>
<ul id="sample3">
  <li>sample list 1</li>
  <li>sample list 2</li>
</ul>
<table id="sample4">
  <tbody>
    <tr>
      <td>a1</td>
      <td>b1</td>
      <td>c1</td>
    </tr>
    <tr>
      <td>a2</td>
      <td>b2</td>
      <td>c2</td>
    </tr>
  </tbody>
</table>`;
  const temp = GmailApp.createDraft("", "", "", { htmlBody: sampleHTML });
  const plainText = temp.getMessage().getPlainBody();
  temp.deleteDraft();
  console.log(plainText);
}
  • This method uses GmailApp.createDraft for converting HTML to plain text. When a draft email is created with GmailApp.createDraft by giving an HTML body, when the message content is retrieved with getPlainBody(), the plain text is retrieved. This method uses this situation.
  • When this sample script is run, the result in “Sample values” section can be obtained.

Note

  • This method is a simple conversion from HTML to plain text. So I’m not sure whether this method can be used for all HTML data. Please be careful about this.

References

 Share!