如何通过Apps Script获取Google Docs的完整版本列表?
获取Google Docs所有细粒度修订记录的方法
可行,但你用的DocumentApp内置方法只能返回聚合后的分组版本,要拿到网页端显示的所有细粒度版本,得换用Google Drive API的修订接口。
为什么原来的代码只能拿到分组版本
Google Apps Script里DocumentApp.getRevisions()返回的是Google Docs自动合并后的修订组(比如同一时段内的多次小修改会被合并成一条),和网页版文档历史里的逐条记录不是同一个数据源。
解决方案:调用Drive API获取所有修订
启用Drive API服务
打开你的Google Apps脚本编辑器,点击「服务」(或旧版的「资源」→「高级Google服务」),找到「Drive API」并开启它。示例代码
下面的脚本可以获取当前文档的所有细粒度修订记录:function getAllDetailedRevisions() { // 获取当前文档ID const docId = DocumentApp.getActiveDocument().getId(); // 调用Drive API获取修订列表,maxResults可按需调整(最大1000) const revisionList = Drive.Revisions.list(docId, { maxResults: 1000, fields: "items(id, modifiedDate, lastModifyingUser(emailAddress, displayName), originalFilename)" }); // 遍历输出所有修订信息 if (revisionList.items && revisionList.items.length > 0) { revisionList.items.forEach(rev => { console.log(`修订ID: ${rev.id}`); console.log(`修改时间: ${new Date(rev.modifiedDate).toLocaleString()}`); console.log(`修改人: ${rev.lastModifyingUser.displayName} (${rev.lastModifyingUser.emailAddress})`); console.log("---"); }); } else { console.log("未找到任何修订记录"); } }注意事项
- 首次运行脚本时,需要授权Drive API的访问权限,按照提示完成授权即可。
- 如果文档修订超过1000条,需要通过
nextPageToken参数实现分页查询,循环获取所有记录。 - 可以修改
fields参数来获取更多修订详情,比如md5Checksum、fileSize等。
内容的提问来源于stack exchange,提问作者Phil Anderson
相关产品推荐
相关产品推荐

