I'm playing with getting the revision history of a document through Google Apps Script and I'm looking for some advice on how to programmatically access the content of the revision.
Using the Drive API, I can access an array of revisions on the document and iterate based on user. The returned object does not include the content of the revision, just an ID. But, you can get a download URL for various content types (pdf, plaintext, etc).
I'd like to retrieve a download URL using UrlFetchApp
and get that content to append to a document. The problem is that the fetch app returns the entire document markup (HTML and CSS) and I'd only like the content of the file.
Script
function revisionHistoryLite() {
var doc = DocumentApp.getActiveDocument();
var eds = doc.getEditors();
var body = doc.getBody();
var revs = Drive.Revisions.list(doc.getId())
var editsList = [];
for(var i=0; i<revs.items.length; i++) {
var revision = revs.items[i];
editsList.push([revision.id, revision.kind, revision.modifiedDate, revision.lastModifyingUser.emailAddress]);
if(revision.lastModifyingUser.emailAddress == "bbennett@elkhart.k12.in.us") {
var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"];
// revUrl returns https://docs.google.com/feeds/download/documents/export/Export?id=docIdString&revision=1&exportFormat=txt
var revString = UrlFetchApp.fetch(revUrl, { contentType: "text/plain", }).getContentText();
Logger.log(revString); // Contains full HTTP markup
// Append the body contents to a temporary document for further processing
// var tempDoc = DocumentApp.create("Temp").getBody().appendParagraph(revString);
}
}
}