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

不羁岁月 提交于 2019-12-02 10:23:12
Bryan P

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"});

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!