Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Google Document as HTML

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?

like image 500
Ray Wadkins Avatar asked Feb 02 '13 16:02

Ray Wadkins


5 Answers

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);
}
like image 88
St3ph Avatar answered Oct 17 '22 06:10

St3ph


Node.js Solution

Using the Google APIs Node.js Client

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.

like image 45
Derek Soike Avatar answered Oct 17 '22 06:10

Derek Soike


Google docs currently has a function to do this. Just download to zip(.html) and you can have a zip archive with html & image (if inserted)

I know this is not solution based on code, but its working :)

Menu for export

like image 22
sukalogika Avatar answered Oct 17 '22 04:10

sukalogika


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.

like image 4
Serge insas Avatar answered Oct 17 '22 04:10

Serge insas


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 protected]",
     subject: "document emailer",
     htmlBody:  docbody  });
}

Hope the new workaround helps

JD

like image 4
Juan Diego Antezana Avatar answered Oct 17 '22 05:10

Juan Diego Antezana