Importing a HTML table into a google doc using google app script

后端 未结 2 866
误落风尘
误落风尘 2021-01-28 16:33

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

相关标签:
2条回答
  • 2021-01-28 16:54

    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"});
    
    0 讨论(0)
  • 2021-01-28 17:09

    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;
    }
    
    0 讨论(0)
提交回复
热议问题