GoogleJsonResponseException: The call from API to drive.revisions.get failed with error : Revision not found: 1

GoogleJsonResponseException: The call from API to drive.revisions.get failed with error : Revision not found: 1

以下代码用于在执行分析脚本后重新初始化文件。目标是清除已为新请求手动填写的单元格。

在分析过程的最后得到错误,我无法追查问题的根源。

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

更新 2022-01-14

// 在您 运行 脚本之前,请在高级 Google 服务中启用驱动器 API。

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

根据您的错误消息,在您的情况下,如何检索现有的第一个修订版本?当这反映在您的脚本中时,它会变成如下。

发件人:

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

收件人:

var googleDocsFileId = SpreadsheetApp.getActiveSpreadsheet().getId();
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;

参考: