简体   繁体   English

如何获取旧版本的 Google 电子表格数据?

[英]How to get older versions of Google Spreadsheet data?

I'm trying to compare different versions of Spreadsheet data and highlight differences between the two versions.我正在尝试比较不同版本的电子表格数据并突出显示两个版本之间的差异。 I know there is Version History feature, but what I'm trying is to get all the revision history when choosing the version.(ie if the latest version is five and I choose two, I want to get all the revisions made between version two and five).我知道有版本历史功能,但我想要的是在选择版本时获取所有修订历史。(即,如果最新版本是五个而我选择两个,我想获得版本二之间的所有修订和五个)。

I know how to access to the current version, but I'm stuck with how to do this with older versions.我知道如何访问当前版本,但我对如何使用旧版本执行此操作感到困惑。 Is there any way to access to older versions as the same manner as the following code?有没有办法以与以下代码相同的方式访问旧版本?

var spreadsheet = SpreadsheetApp.openById(id);
var sheet = spreadsheet.getSheets()[0];
var sheetdata = sheet.getDataRange().getValues();
  • You want to retrieve the values from other version of Spreadsheet using the following script.您想使用以下脚本从电子表格的其他版本中检索值。

     var spreadsheet = SpreadsheetApp.openById(id); var sheet = spreadsheet.getSheets()[0]; var sheetdata = sheet.getDataRange().getValues();
  • You have already got the revision ID of the Spreadsheet.您已经获得了电子表格的修订 ID。

  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本来实现这一点。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? When only values are retrieved from the other version of Spreadsheet, how about this workaround?当仅从其他版本的电子表格检索值时,此解决方法如何? Unfortunately, the Spreadsheet of the some revision cannot be directly retrieved as the Spreadsheet.不幸的是,某些修订版的电子表格不能作为电子表格直接检索。 So in this workaround, it is converted to other format and retrieved the values.所以在这个解决方法中,它被转换为其他格式并检索值。 Please think of this as just one of several answers.请将此视为几个答案之一。

Flow:流动:

The flow of this workaround is as follows.此变通方法的流程如下。

  1. Retrieve Spreadsheet of other version as the excel format.以excel格式检索其他版本的电子表格。
  2. Convert the excel format to Google Spreadsheet.将 excel 格式转换为 Google 电子表格。
    • By this, the values are separated to each sheet.通过这种方式,值被分隔到每个工作表。
  3. Retrieve values using your script.使用您的脚本检索值。

Sample script:示例脚本:

Before you run this script, please enable Drive API at Advanced Google services .在运行此脚本之前,请在高级 Google 服务中启用 Drive API。 And set the variables of spreadsheetId and revisionId .并设置spreadsheetIdrevisionId的变量。

function myFunction() {
  var spreadsheetId = "###"; // Please set the Spreadsheet ID.
  var revisionId = "###"; // Please set the revision ID.

  var url = "https://docs.google.com/spreadsheets/export?id=" + spreadsheetId + "&revision=" + revisionId + "&exportFormat=xlsx";
  var blob = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}}).getBlob();
  var id = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: revisionId}, blob).id;


  var spreadsheet = SpreadsheetApp.openById(id);
  var sheet = spreadsheet.getSheets()[0];
  var sheetdata = sheet.getDataRange().getValues();
}

Note:笔记:

  • In this sample script, a Spreadsheet of the version is created to the root folder.在此示例脚本中,将在根文件夹中创建版本的电子表格。
    • The filename is the revision ID.文件名是修订 ID。
  • When you want to retrieve the revision IDs of the Spreadsheet, please use Revisions: list.当您要检索电子表格的修订 ID 时,请使用 Revisions: list。

References:参考:

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM