【问题标题】:Render HTML content to google spreadsheet将 HTML 内容渲染到谷歌电子表格
【发布时间】:2020-11-14 15:50:18
【问题描述】:

我在单元格 A1 中有一个 HTML 内容,我想渲染 HTML 内容并在单元格 B1 中添加渲染的 HTML 内容

例如如果我在 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 并将其与标签一起粘贴而不渲染任何内容

【问题讨论】:

  • 我没有包含单元格中的 HTML 的文件。所以我试过 var htmlTarget = current.getRange('A1').getValue(); var htmlOutput = HtmlService.createHtmlOutput(htmlTarget); var message = htmlOutput.getContent(); database.getRange('A2').setValue(message);而且它不起作用

标签: html google-apps-script google-sheets render


【解决方案1】:

我相信你的目标如下。

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

修改点:

  • HtmlOutput 类中getContent() 的方法返回HTML 数据。 Ref 我认为这可能是您的问题的原因。

为了检索没有 HTML 标记的渲染文本,在这个答案中,我想建议通过使用 Drive API v2 中的“文件:插入”方法将 HTML 数据转换为 Google 文档来检索文本数据. (因为,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. 将文本数据放入单元格中。

参考资料:

【讨论】:

  • @basel adel 感谢您的回复。很高兴您的问题得到解决。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多