我的单元格 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 数据转换为纯文本数据。
修改要点:
- 方法为
getContent()
HtmlOutput 类中返回 HTML 数据。Ref https://developers.google.com/apps-script/reference/html/html-output#getContent()我认为这可能是您问题的原因。
为了检索没有 HTML 标签的渲染文本,在这个答案中,我想建议通过使用 Drive API v2 中的“文件:插入”方法将 HTML 数据转换为 Google 文档来检索文本数据。 (因为,Advanced Google 服务中 Drive API 的版本仍然是 v2。)
当你的脚本被修改后,它会变成如下所示。
修改后的脚本:
在使用此脚本之前,请在高级 Google 服务中启用 Drive API https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_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.
- 将 HTML 数据作为临时文件转换为 Google 文档。
- 从 Google 文档中检索文本数据。
- 删除临时文件。
- 将文本数据放入单元格中。
参考:
- 获取内容() https://developers.google.com/apps-script/reference/html/html-output#getcontent
- 文件:插入 https://developers.google.com/drive/api/v2/reference/files/insert
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)