Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render HTML content to google spreadsheet

I've an HTML content in cell A1, I want to render the HTML content and add the rendered HTML content in cell B1

e.g. if I've this HTML in A1

<label class="s-label mb4 d-block" for="wmd-input">
                    Body

                        <p class="s-description mt2">Include all the information someone would need to answer your question</p>
                </label>

I want the output in B1 to be

Body
Include all the information someone would need to answer your question

I've tried

  var htmlTarget = current.getRange('A1').getValue();
  var htmlOutput = HtmlService.createHtmlOutput(htmlTarget);
  var message = htmlOutput.getContent();
  database.getRange('B1').setValue(message);

And it gets the same HTML and pastes it with the tags without rendering anything

like image 383
basel adel Avatar asked Feb 28 '26 06:02

basel adel


1 Answers

I believe your goal as follows.

  • You want to convert the HTML data to the plain text data using Google Apps Script.

Modification points:

  • The method of getContent() in Class HtmlOutput returns the HTML data. Ref I thought that this might be the reason of your issue.

In order to retrieve the rendered text without the HTML tags, in this answer, I would like to propose to retrieve the text data by converting the HTML data to Google Document using the method of "Files: insert" in Drive API v2. (Because, the version of Drive API is still v2 at Advanced Google service.)

When your script is modified, it becomes as follows.

Modified script:

Before you use this script, please enable Drive API at Advanced Google services.

From:
var htmlTarget = current.getRange('A1').getValue();
var htmlOutput = HtmlService.createHtmlOutput(htmlTarget);
var message = htmlOutput.getContent();
database.getRange('B1').setValue(message);
To:
var htmlTarget = current.getRange('A1').getValue();
var blob = Utilities.newBlob(htmlTarget, MimeType.HTML);
var id = Drive.Files.insert({title: "sample", mimeType: MimeType.GOOGLE_DOCS}, blob).id;
var message = DocumentApp.openById(id).getBody().getText();
DriveApp.getFileById(id).setTrashed(true); //  or Drive.Files.remove(id);
database.getRange('B1').setValue(message);
  • In this modified script, the following flow is run.
    1. Convert HTML data to Google Document as a temporal file.
    2. Retrieve text data from Google Document.
    3. Remove the temporal file.
    4. Put the text data to the cell.

References:

  • getContent()
  • Files: insert
like image 112
Tanaike Avatar answered Mar 02 '26 19:03

Tanaike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!