将 HTML 内容呈现到 google 电子表格

Render HTML content to google spreadsheet

我在单元格 A1 中有一个 HTML 内容,我想呈现 HTML 内容并将呈现的 HTML 内容添加到单元格 B1

例如如果我在 A1

中有这个 HTML
<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>

我希望 B1 中的输出是

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

我试过了

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

它得到相同的 HTML 并将其与标签一起粘贴而不渲染任何内容

我相信你的目标如下。

  • 您想使用 Google Apps 脚本将 HTML 数据转换为纯文本数据。

修改点:

  • 在Class HtmlOutput returns HTML 数据中getContent() 的方法。 Ref 我认为这可能是您遇到问题的原因。

为了检索没有 HTML 标签的渲染文本,在这个答案中,我想建议通过将 HTML 数据转换为 Google 来检索文本数据在 Drive API v2 中使用“Files: insert”方法的文档。 (因为,Drive API 的版本在 Advanced Google 服务中仍然是 v2。)

当你的脚本修改后,变成如下。

修改后的脚本:

在您使用此脚本之前,please enable Drive API at Advanced Google services

从:
var htmlTarget = current.getRange('A1').getValue();
var htmlOutput = HtmlService.createHtmlOutput(htmlTarget);
var message = htmlOutput.getContent();
database.getRange('B1').setValue(message);
到:
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);
  • 在这个修改后的脚本中,下面的流程是运行。
    1. 将 HTML 数据转换为 Google 文档作为临时文件。
    2. 从 Google 文档中检索文本数据。
    3. 删除临时文件。
    4. 将文本数据放入单元格。

参考文献: