Home > Software engineering >  GoogleJsonResponseException: The call from API to drive.revisions.get failed with error : Revision n
GoogleJsonResponseException: The call from API to drive.revisions.get failed with error : Revision n

Time:01-15

The following code is used to reinitialize the file following the execution of the analysis script. The goal is to erase the cells already filled in manually for a new request.

The error is obtained at the end of the analysis process and I cannot track down the source of the problem.

    // Before you run the script, please enable Drive API at Advanced Google services.
function resetFile() {//restore previous version of the file
  var revisionId = "1";  // Please set the revision ID you want to revert.
  var googleDocsFileId = SpreadsheetApp.getActiveSpreadsheet().getId();  // 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;
    }
  }
}

function resetSource(){//pas nécessaire
   var file= SpreadsheetApp.getActiveSpreadsheet().getRangeByName("FileID").getValue(); 
   var fileID = DriveApp.getFileById(file);  
  fileID.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.EDIT);
}

Update 2022-01-14

enter image description here

CodePudding user response:

// Before you run the script, please enable Drive API at Advanced Google services.

function resetFile() {
    //restore previous version of the file var revisionId = "1"; // Please set the revision ID you want to revert. 
    var googleDocsFileId = SpreadsheetApp.getActiveSpreadsheet().getId();

    // 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; 
        } 
    }
} 

function resetSource() {
    //pas nécessaire 
    var file = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("FileID").getValue(); 
    var fileID = DriveApp.getFileById(file); 
    fileID.setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.EDIT);
}

CodePudding user response:

From your error message, in your situation, how about retrieving the 1st existing revision version? When this is reflected in your script, it becomes as follows.

From:

var revisionId = "1";
var googleDocsFileId = SpreadsheetApp.getActiveSpreadsheet().getId();

To:

var list = Drive.Revisions.list(googleDocsFileId, { fields: "items(id)" }).items;
if (list.length == 0) throw new Error("No revisions were found.");
var revisionId = list[0].id;

Reference:

  •  Tags:  
  • Related