如何将文档还原到以前的版本 [英] How to revert back document to a previous revision
问题描述
我想将GAS中的Google文档(或任何其他文档)还原到较早的版本.我设法访问了修订版,甚至下载了旧的修订版,但是如何将文档还原到指定的修订版?
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?
推荐答案
- 您想还原到以前的Google文档版本.
- 您想使用Google Apps脚本实现这一目标.
- 从Google Docs文件中检索带有修订ID的导出的端点.
- 在这种情况下,Google Docs文件将导出为Microsoft Docs文件.
- 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.
如果我的理解正确,那么这个答案如何?
If my understanding is correct, how about this answer?
不幸的是,在当前阶段,API不能通过脚本直接更改Google Docs的修订版.因此,作为几种解决方法之一,我建议使用导出的数据覆盖Google Docs文件.此解决方法的流程如下.
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.
通过此流程,Google Docs文件将还原为以前的版本.
By this flow, the Google Docs file is reverted to the previous version.
重要的一点是,当将Google Docs文件导出为Microsoft Docs文件时,在大多数情况下,覆盖的Google Docs文件不会从该版本的原始Google Docs更改.但是我不确定该解决方法是否在所有情况下都能正常工作.所以请注意这一点.
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.
此替代方法的示例脚本如下.在运行脚本之前,请在高级Google服务中启用Drive API .
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;
}
}
}
注意:
- 即使将Google Docs文件恢复为以前的版本,也可以更改文件的版本.但是,在测试上述脚本之前,建议您使用示例Google Docs文件.
- 使用此替代方法时,除Google Docs之外的文件也可以还原为以前的版本.但是在这种情况下,请修改脚本.
- 如果您想完全还原到以前的版本,在当前阶段,我建议手动还原.
- 例如,当您只想还原单元格的值时,可以完美实现.您可以在此线程中看到它作为示例案例.
- 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.
- Advanced Google services
- Revisions: get
- Files: update
Note:
如果这不是您想要的方向,我深表歉意.
If this was not the direction you want, I apologize.
从2020年1月开始,访问令牌不能与access_token=###
之类的查询参数一起使用.
From January, 2020, the access token cannot be used with the query parameter like access_token=###
. Ref So please use the access token to the request header instead of the query parameter. It's as follows.
var res = UrlFetchApp.fetch(url, {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}});
这篇关于如何将文档还原到以前的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!