How to revert back document to a previous revision

I want to restore a Google doc (or any other doucment) in GAS to an earlier revision. I managed to access revisions, even to download an older revision, but how can I restore a document to a specified revision?

2 answers

  • answered 2019-09-11 10:26 AMolina

    You can do it manually following these instructions. Additionally, as you can see in the revision resource documentation, you can change the published property to mark a particular revision to be the "active one".

    To do this from Apps Script you will need to enable Google Advanced Services, which will allow you to use the API fully. Then you can use the update method (Documentation here), using the desired revision's ID and changing its published property to true.

    Bear in mind that according to the documentation:

    This is only applicable to Google Docs.

  • answered 2019-09-12 01:34 Tanaike

    • You want to revert to the previous version of Google Docs.
    • You want to achieve this using Google Apps Script.

    If my understanding is correct, how about this answer?

    Issue and workaround:

    Unfortunately, in the current stage, the revision of Google Docs cannot be directly changed by APIs with a script. So as one of several workarounds, I would like to propose to overwrite the Google Docs file using the exported data. The flow of this workaround is as follows.

    1. Retrieve the exported endpoint from the Google Docs file with the revision ID.
      • In this case, the Google Docs file is exported as the Microsoft Docs file.
    2. Retrieve the blob from the exported endpoint.
    3. Overwrite the Google Docs with the retrieved blob.

    By this flow, the Google Docs file is reverted to the previous version.

    IMPORTANT:

    As an important point, when the Google Docs file is exported as the Microsoft Docs file, at the most cases, the overwritten Google Docs files are not changed from the original Google Docs for the version. But I'm not sure that this workaround works perfectly for all cases. So please be careful this.

    Sample script:

    The sample script of this workaround is as follows. Before you run the script, please enable Drive API at Advanced Google services.

    function myFunction() {
      var revisionId = "1";  // Please set the revision ID you want to revert.
      var googleDocsFileId = "###";  // Please set the Google Docs file ID.
    
      var endpoints = Drive.Revisions.get(googleDocsFileId, revisionId).exportLinks;
      var keys = Object.keys(endpoints);
      for (var i = 0; i < keys.length; i++) {
        if (keys[i].indexOf("application/vnd.openxmlformats-officedocument") > -1) {
          var endpoint = endpoints[keys[i]] + "&access_token=" + ScriptApp.getOAuthToken();
          var mediaData = UrlFetchApp.fetch(endpoint).getBlob();
          Logger.log(mediaData.getBytes().length)
          Drive.Files.update({}, googleDocsFileId, mediaData);
          break;
        }
      }
    }
    

    Note:

    • Even after the Google Docs file was reverted to the previous version, the version of the file can be changed. But before you test above script, I recommend to use a sample Google Docs file.
    • When this workaround is used, the files except for Google Docs can be also reverted to the previous version. But in this case, please modify the script.
    • If you want to revert perfectly to the previous version, in the current stage, I recommend to manually revert.
    • For example, when you want to revert only values of cells, this can be perfectly achieved. You can see it at this thread as the sample case.

    References:

    If this was not the direction you want, I apologize.