使用谷歌应用程序脚本将HTML表格导入谷歌文档

时间:2016-02-28 21:35:22

标签: google-apps-script google-docs google-docs-api

我将数据从JIRA导入Google文档。其中一个字段是描述,基本上是HTML文本。

有没有办法#34;添加" Google Doc上的此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>

2 个答案:

答案 0 :(得分:0)

我能够根据this post中的评论中提到的更改,使用带样式的html内容创建文档:

var blob = Utilities.newBlob(content, {mimeType:"text/html"});
var newfile = Drive.Files.insert(resource, blob, {"convert":"true"});

答案 1 :(得分:0)

感谢您的提示 - 基于此,我发现了以下帖子:https://ctrlq.org/code/20102-convert-office-files-to-google-docs(加http://scraping.pro/automaticly-converting-files-google-app-script/,告知如何启用Drive 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;
}
相关问题