使用 google 应用程序脚本将 HTML table 导入 google 文档
Importing a HTML table into a google doc using google app script
我正在将数据从 JIRA 导入 Google Doc。其中一个字段是描述,基本上是 HTML 文本。
有什么方法可以在 Google 文档上 "add" 这个 HTML 文本吗?或者我真的必须手动解析它并创建段落、表格等吗?
HTML 可能如下所示:
<p>Hello There</p>
<table class='confluenceTable'>
<tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Hours </th>
<th class='confluenceTh'> Cost </th>
</tr>
<tr>
<td class='confluenceTd'> Some Person</td>
<td class='confluenceTd'> 1 </td>
<td class='confluenceTd'> CHF 1</td>
</tr>
</tbody>
</table>
我能够根据我在 :
中的评论中提到的更改创建 new 具有样式 html 内容的文档
var blob = Utilities.newBlob(content, {mimeType:"text/html"});
var newfile = Drive.Files.insert(resource, blob, {"convert":"true"});
感谢您的提示 - 基于此我发现了以下内容 post:https://ctrlq.org/code/20102-convert-office-files-to-google-docs (plus http://scraping.pro/automaticly-converting-files-google-app-script/ 告诉您如何启用驱动器 API).
以下功能对我来说效果很好:
function convertHtmlToDocs(html) {
var uploadFile = JSON.parse(UrlFetchApp.fetch(
"https://www.googleapis.com/upload/drive/v2/files? uploadType=media&convert=true",
{
method: "POST",
contentType: "text/html",
payload: Utilities.newBlob("").setDataFromString(html, "UTF-8").getBytes(),
headers: {
"Authorization" : "Bearer " + ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
}
).getContentText());
var doc = DocumentApp.openById(uploadFile.id);
var body = doc.getBody().copy();
DriveApp.removeFile(DriveApp.getFileById(doc.getId()));
return body;
}
我正在将数据从 JIRA 导入 Google Doc。其中一个字段是描述,基本上是 HTML 文本。
有什么方法可以在 Google 文档上 "add" 这个 HTML 文本吗?或者我真的必须手动解析它并创建段落、表格等吗?
HTML 可能如下所示:
<p>Hello There</p>
<table class='confluenceTable'>
<tbody>
<tr>
<th class='confluenceTh'> Name </th>
<th class='confluenceTh'> Hours </th>
<th class='confluenceTh'> Cost </th>
</tr>
<tr>
<td class='confluenceTd'> Some Person</td>
<td class='confluenceTd'> 1 </td>
<td class='confluenceTd'> CHF 1</td>
</tr>
</tbody>
</table>
我能够根据我在
var blob = Utilities.newBlob(content, {mimeType:"text/html"});
var newfile = Drive.Files.insert(resource, blob, {"convert":"true"});
感谢您的提示 - 基于此我发现了以下内容 post:https://ctrlq.org/code/20102-convert-office-files-to-google-docs (plus http://scraping.pro/automaticly-converting-files-google-app-script/ 告诉您如何启用驱动器 API).
以下功能对我来说效果很好:
function convertHtmlToDocs(html) {
var uploadFile = JSON.parse(UrlFetchApp.fetch(
"https://www.googleapis.com/upload/drive/v2/files? uploadType=media&convert=true",
{
method: "POST",
contentType: "text/html",
payload: Utilities.newBlob("").setDataFromString(html, "UTF-8").getBytes(),
headers: {
"Authorization" : "Bearer " + ScriptApp.getOAuthToken()
},
muteHttpExceptions: true
}
).getContentText());
var doc = DocumentApp.openById(uploadFile.id);
var body = doc.getBody().copy();
DriveApp.removeFile(DriveApp.getFileById(doc.getId()));
return body;
}