Get Google Document as HTML

2019-01-09 01:35发布

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 compiles a list of documents. However, I can't see a way to convert the document to HTML. I know that Google can render documents in a web page, so I wondered if it was possible to get a rendered version for use in my content service.

Is this possible?

7条回答
Explosion°爆炸
2楼-- · 2019-01-09 02:14

You can try this code :

  function getGoogleDocumentAsHTML(){
  var id = DocumentApp.getActiveDocument().getId() ;
  var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
  var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html";
  var param = {
    method      : "get",
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions:true,
  };
  var html = UrlFetchApp.fetch(url,param).getContentText();
  Logger.log(html);
}
查看更多
别忘想泡老子
3楼-- · 2019-01-09 02:19

There is no direct method in GAS to get an HTML version of a doc and this is quite an old enhancement request but the workaround described originally by Henrique Abreu works pretty well, I use it all the time...

The only annoying thing in the authorization process that needs to be called from the script editor which makes it uneasy to use in a shared application (with "script unable" users) but this only happens once ;).

There is also a Library created by Romain Vialard that makes things (a bit) easier... and adds a few other interesting functions.

查看更多
时光不老,我们不散
4楼-- · 2019-01-09 02:27

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.

查看更多
来,给爷笑一个
5楼-- · 2019-01-09 02:31

Node.js Solution

Here's how you can get a google doc as html using google drive's node.js client library.

// import googleapis npm package
var google = require('googleapis');

// variables
var fileId = '<google drive doc file id>',
    accessToken = '<oauth access token>';

// oauth setup
var OAuth2 = google.auth.OAuth2,
    OAuth2Client = new OAuth2();

// set oauth credentials
OAuth2Client.setCredentials({access_token: accessToken});

// google drive setup
var drive = google.drive({version: 'v3', auth: OAuth2Client});

// download file as text/html
var buffers = [];
drive.files.export(
    {
        fileId: fileId,
        mimeType: 'text/html'
    }
)
    .on('error', function(err) {
        // handle error
    })
    .on('data', function(data) {
        buffers.push(data); // data is a buffer
    })
    .on('end', function() {
        var buffer = Buffer.concat(buffers),
            googleDocAsHtml = buffer.toString();
        console.log(googleDocAsHtml);
    });

Take a look at the Google Drive V3 download docs for more languages and options.

Note that the Google APIs Node.js Client is in alpha (January 2017).

查看更多
爷的心禁止访问
6楼-- · 2019-01-09 02:31

Perhaps this would work for you...

function doGet() {
  var blob = DriveApp.getFileById('myFileId').getAsHTML();
  return HtmlService.createHtmlOutput(blob);
}
查看更多
仙女界的扛把子
7楼-- · 2019-01-09 02:33

Here is a little snipped for the new version of goole AOuth following the idea posted by Enrique:

function exportAsHTML(){
  var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
  var docID = DocumentApp.getActiveDocument().getId();
  var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+docID+"&exportFormat=html";
  var param = {
    method      : "get",
    headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions:true,
  };
  var html = UrlFetchApp.fetch(url,param).getContentText();
  return html; 

}

and then use the usual mailApp:

function mailer(){
   var docbody = exportAsHTML();
   MailApp.sendEmail({
     to: "email@mail.com",
     subject: "document emailer",
     htmlBody:  docbody  });
}

Hope the new workaround helps

JD

查看更多
登录 后发表回答