Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created April 22, 2022 01:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/2779ee59a199011e3f48a1fa92879a68 to your computer and use it in GitHub Desktop.
Save tanaikech/2779ee59a199011e3f48a1fa92879a68 to your computer and use it in GitHub Desktop.
Simply Converting HTML to Plain Text using Google Apps Script

Simply Converting HTML to Plain Text using Google Apps Script

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment