4

I'm trying to figure out how to programmatically revert to a pinned revision of a file using the Advanced Drive Service.

I've looked at the documentation here
https://developers.google.com/apps-script/advanced/drive#listing_revisions and here
https://developers.google.com/drive/v3/reference/#Revisions

I've tried

var revs = Drive.Revisions.list(fileId);
var revArr = revs.items;
var len = revArr.length;
var rev = revArr[len - 2];
var rev2 = revArr[len - 1];
var revId = rev.id;
var revId2 = rev2.id;
// These will throw an id mismatch error.
Drive.Revisions.update(rev, fileId, revId2);
Drive.Revisions.patch(rev, fileId, revId2); 
// These don't seem to revert the file.
Drive.Revisions.update(rev2, fileId, revId2);
Drive.Revisions.patch(rev2, fileId, revId2);

I just want to take the file and revert it back a revision. I don't want to download the old file, but take the current file and put it back to how it was before.
Eg. FileA rev0 -> {Edited: Change Text to gibberish} -> FileA rev1
(Using ADS) Drive.Revisions.revertTo(rev0.id); //Or something like that.

I've tried a bunch of different things, but can't seem to get it to work.

John
  • 71
  • 7
  • There isn't any direct way to do this, but every revision is being kept in the Google Drive like separate files. You can just get the revision directly or publish the revision you want to use. You can watch the video in the [documentation](https://developers.google.com/drive/v3/web/manage-revisions#tips_and_tricks) for more info. – gerardnimo Mar 02 '16 at 02:16

1 Answers1

0

You could try fetching the docx export link and see how much of the revision it restores:

var response = UrlFetchApp.fetch(
  "https://docs.google.com/feeds/download/documents/export/Export?id=<FILEID>&revision=<NUMBER>&exportFormat=docx",
  {
    headers: {
      "Authorization" : "Bearer " + ScriptApp.getOAuthToken()
    }
  }
);

var blob = response.getBlob();

var resource = {
  title: <DOCUMENT_TITLE>,
  parents: [
    {
      "id": <FOLDER_ID>, 
      "kind": "drive#fileLink"
    }
  ],
  mimeType: 'application/vnd.google-apps.document'
};

Drive.Files.update(resource, fileId, blob);

This post gave me hints on what to try.

Community
  • 1
  • 1
Bryan P
  • 5,031
  • 3
  • 30
  • 44