You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过编程方式重命名Google Docs文档的版本?

问题描述

我有一份Google Docs文档(文档ID:1U_Be02ViQp0UKPYNQo184ks4Vv-ey8mNoawJtTyC1R8),希望通过Python/JS等编程语言或REST API,将文档的特定版本重命名为类似Version 1.0 - 20240101的格式,仅重命名最新版本也可以接受。我已经写出了获取文档所有修订记录的代码,但找不到用于重命名版本的API,求技术解决方案。

现有获取修订记录的代码
import os
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
import urllib.request


def get_revision_history(fileId):
    # 不要将token文件提交到代码仓库
    KEY_FILE = 'token.json'
    DOWNLOAD_DIR = 'download'
    curDir = os.path.join(DOWNLOAD_DIR, fileId)
    if not os.path.isdir(curDir):
        os.mkdir(curDir)

    # 使用服务账号认证Google API
    credentials = service_account.Credentials.from_service_account_file(
        KEY_FILE,
        scopes=['https://www.googleapis.com/auth/documents',
                'https://www.googleapis.com/auth/drive']
    )

    # 初始化Google Drive API客户端,仅v2可用(v3不行)
    drive_service = build('drive', 'v2', credentials=credentials)

    try:
        # 获取Google Docs的修订历史
        revisions = drive_service.revisions().list(fileId=fileId).execute()
        print(f"{fileId}: 找到 {len(revisions['items'])} 条修订记录")
        return revisions

    except HttpError as error:
        print(f"发生错误: {error}")
        raise error


if __name__ == '__main__':
    # fileId是Google Docs链接中的ID,格式为https://docs.google.com/document/d/fileId/
    fileId = "1U_Be02ViQp0UKPYNQo184ks4Vv-ey8mNoawJtTyC1R8"
    get_revision_history(fileId)
解决方案说明

Google Drive API(无论v2还是v3)并没有提供直接“重命名”修订版本的功能,因为修订记录本身没有独立的“版本名称”字段。不过可以通过修订记录的description字段来实现类似效果——这个字段支持自定义文本,会在Google Docs的版本历史界面中显示,完全可以用来存储Version 1.0 - 20240101这类版本标识。

以下是基于你现有代码扩展的实现:

新增更新修订描述的函数

在原有代码中添加以下函数,用于给指定修订版本设置自定义描述:

def update_revision_description(fileId, revisionId, description):
    KEY_FILE = 'token.json'
    credentials = service_account.Credentials.from_service_account_file(
        KEY_FILE,
        scopes=['https://www.googleapis.com/auth/drive']
    )
    drive_service = build('drive', 'v2', credentials=credentials)
    
    try:
        # 获取目标修订记录
        revision = drive_service.revisions().get(fileId=fileId, revisionId=revisionId).execute()
        # 更新描述字段
        revision['description'] = description
        # 提交更新
        updated_revision = drive_service.revisions().update(
            fileId=fileId,
            revisionId=revisionId,
            body=revision
        ).execute()
        print(f"修订 {revisionId} 的描述已更新为: {updated_revision['description']}")
        return updated_revision
    except HttpError as error:
        print(f"更新时出错: {error}")
        raise error

调用示例(更新最新版本)

修改主函数,调用上述函数给最新版本设置自定义描述:

if __name__ == '__main__':
    fileId = "1U_Be02ViQp0UKPYNQo184ks4Vv-ey8mNoawJtTyC1R8"
    revisions = get_revision_history(fileId)
    if revisions and revisions['items']:
        # 修订记录按时间升序排列,最后一条为最新版本
        latest_revision = revisions['items'][-1]
        # 设置自定义版本标识
        update_revision_description(
            fileId,
            latest_revision['id'],
            "Version 1.0 - 20240101"
        )

注意事项

  1. 确保你的服务账号拥有https://www.googleapis.com/auth/drive权限(原代码已包含该scope),否则无法修改修订记录。
  2. 如果需要给特定历史版本设置描述,只需找到对应版本的revisionId(可通过get_revision_history返回的items列表获取),传入update_revision_description函数即可。
  3. 在Google Docs的版本历史界面中,设置的description会显示在对应版本的下方,能起到版本名称的标识作用。

内容的提问来源于stack exchange,提问作者Ben Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 02:57:07