Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

javascript - Retrieve revision text through Google Apps Script

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


    }
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When it downloads files from exportLinks using UrlFetchApp.fetch(), the authorization is required. So please modify your script as follows.

From :

var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"];
var revString = UrlFetchApp.fetch(revUrl, { contentType: "text/plain",  }).getContentText();

To :

var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"]  + "&access_token=" + ScriptApp.getOAuthToken();
var revString = UrlFetchApp.fetch(revUrl).getContentText();

By this, you can download text data from the revision data.

Edit :

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 == "### mail address ###") {
      var revUrl = Drive.Revisions.get(doc.getId(), revision.id).exportLinks["text/plain"]  + "&access_token=" + ScriptApp.getOAuthToken();
      var revString = UrlFetchApp.fetch(revUrl).getContentText();
      Logger.log(revString); // Contains full HTTP markup
    }
  }
}

Updated: February 7, 2020

From January, 2020, the access token cannot be used with the query parameter like access_token=###. Ref So please use the access token to the request header instead of the query parameter. It's as follows.

var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...