Importing a HTML table into a google doc using goo

2019-03-03 13:22发布

I'm importing data from JIRA into a Google Doc. One of the fields would be the description, which is basically HTML text.

Is there any way to "add" this HTML text on a Google Doc? Or do I really have to parse it manually and create paragraphs, tables, etc.?

The HTML could look as follows:

<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条回答
可以哭但决不认输i
2楼-- · 2019-03-03 13:46

Thanks for your hint - based on this I found the following post: https://ctrlq.org/code/20102-convert-office-files-to-google-docs (plus http://scraping.pro/automaticly-converting-files-google-app-script/ which tells how to enable Drive API).

The following function works pretty well for me:

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;
}
查看更多
别忘想泡老子
3楼-- · 2019-03-03 14:05

I was able to create a new Doc with styled html content from the change mentioned in my comment in this post:

var blob = Utilities.newBlob(content, {mimeType:"text/html"});
var newfile = Drive.Files.insert(resource, blob, {"convert":"true"});
查看更多
登录 后发表回答