Get Google Document as HTML

后端 未结 8 1985
无人及你
无人及你 2020-11-27 17:28

I had a wild idea that I could build a website blog for an unsophisticated user friend using Google Drive Documents to back it. I was able to create a contentService that c

8条回答
  •  执笔经年
    2020-11-27 18:03

    You may use the solution here

    /**
     * Converts a file to HTML. The Advanced Drive service must be enabled to use
     * this function.
     */
    function convertToHtml(fileId) {
      var file = Drive.Files.get(fileId);
      var htmlExportLink = file.exportLinks['text/html'];
      if (!htmlExportLink) {
        throw 'File cannot be converted to HTML.';
      }
      var oAuthToken = ScriptApp.getOAuthToken();
      var response = UrlFetchApp.fetch(htmlExportLink, {
        headers:{
          'Authorization': 'Bearer ' + oAuthToken
        },
        muteHttpExceptions: true
      });
      if (!response.getResponseCode() == 200) {
        throw 'Error converting to HTML: ' + response.getContentText();
      }
      return response.getContentText();
    }
    

    Pass as fileId, the id of the google doc and to enable advanced drive services follow the instructions here.

提交回复
热议问题