使用Google API还原电子表格的特定版本 [英] Restoring specific version of spreadsheet using the Google API

查看:128
本文介绍了使用Google API还原电子表格的特定版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题中所说的那样,我正在尝试找到一种使用Google Sheets API v4(或Google Drive API v3)将电子表格还原到特定版本的方法.

Just like the title says, I'm trying to find a way to restore a spreadsheet to a specific version using the Google Sheets API v4 (or Google Drive API v3).

这是由于单元测试而需要的-我需要一个测试以对电子表格执行一些操作(例如删除一行),声明结果,然后将电子表格重置为初始状态,以便下一次测试可以在电子表格上执行原始电子表格.

This is needed because of unit testing - I need one test to perform some actions (let's say delete a row) on a spreadsheet, assert the results and then reset the spreadsheet to its initial state so the next test can execute on the original spreadsheet.

到目前为止,我注意到Google Sheets API中没有选择执行类似操作的选项.就使用Google Drive API而言,我尝试使用修订 (获取所有修订的列表,并删除所需修订之后的所有修订),但无济于事.

So far I noticed that there isn't an option to do something similar in the Google Sheets API. As far as using the Google Drive API goes, I tried using revisions (getting a list of all revisions and deleting all revisions after the desired one), but to no avail.

在点击恢复该版本按钮后,我还尝试捕获请求,发现其中存在一个revisions/revert端点.

I also tried capturing the request after clicking on the Restore this version button and found out there is a revisions/revert endpoint.

> Example:
> https://docs.google.com/spreadsheets/d/**spreadsheetId**/revisions/revert?includes_info_params=false

我实际上设法以这种方式还原了版本,但是使用了Postman并使用了硬编码的值-这很糟糕.

I actually managed to revert the version this way, but using Postman and using hardcoded values - which is bad.

有关如何处理此问题的任何建议?

Any advice on how to approach this?

推荐答案

使用Drive API从文档还原旧版本的通常方法是删除所有使用您只能删除Google云端硬盘中具有二进制内容的文件(例如图像或视频)的修订.其他文件(例如Google文档或表格)的修订以及最后剩余的文件版本可以"不能删除".因此,没有直接的方法可以使用Drive API恢复到以前的版本(Sheets API对此无效).

The usual way to restore an older version from a document with Drive API is by deleting all later versions with Revisions: delete. But, as the referenced documentation indicates, "you can only delete revisions for files with binary content in Google Drive, like images or videos. Revisions for other files, like Google Docs or Sheets, and the last remaining file version can't be deleted". So there is no direct way to revert to a previous version with Drive API (Sheets API is not useful for this).

可能的解决方法是执行以下操作:

A possible workaround would be to do the following:

  • Get the revision ID you want to restore via Revisions.list.
  • Export the version you want to restore via Revisions.get and exportLinks, a property of the Revisions resource. You will have to choose the MIME type in which to export this version (see list of supported export MIME types). Update: You have to specify that you want to retrieve the field exportLinks by setting the parameter fields to exportLinks.
  • Get the blob of this exported version.
  • Using this blob, update the spreadsheet with Files.update.

您可以在此答案中的Google Apps脚本中查看执行此操作的代码.您必须直接使用API​​并将其适应C#,但是工作流程基本上是相同的.

You can see the code to do this in Google Apps Script in this answer. You would have to use the API directly and adapt this to C#, but the workflow is basically the same.

  • Revisions: delete
  • Revisions resource
  • G Suite documents and corresponding export MIME types
  • Files.update

这篇关于使用Google API还原电子表格的特定版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆