How to programmatically change the name of a Google Docs version?

I’m trying to figure out how to change the name of a specific version in a Google Docs file using code. I’d like to give it a name like “Version 1.0 - 20240101”. It would be great if I could do this with Python, JavaScript, or even through a REST API.

I’ve already got some code that can fetch all the revisions of a document, but I’m stuck on how to actually rename one. Here’s a quick example of what I’ve got so far:

import os
from google.oauth2 import service_account
from googleapiclient.discovery import build


def fetch_doc_versions(doc_id):
    creds = service_account.Credentials.from_service_account_file(
        'my_secret_key.json',
        scopes=['https://www.googleapis.com/auth/documents', 'https://www.googleapis.com/auth/drive']
    )
    drive_api = build('drive', 'v2', credentials=creds)
    
    try:
        version_list = drive_api.revisions().list(fileId=doc_id).execute()
        print(f"Found {len(version_list['items'])} versions for document {doc_id}")
    except Exception as e:
        print(f"Oops! Something went wrong: {e}")
        return None


doc_id = "your_document_id_here"
fetch_doc_versions(doc_id)

Can anyone help me figure out how to actually rename a version? I’ve looked through the API docs but can’t seem to find the right method. Even just renaming the latest version would be helpful. Thanks!

I’ve encountered this limitation in my work with Google Docs API as well. Unfortunately, direct renaming of versions isn’t supported programmatically. However, there’s a potential workaround you might consider.

You could create a copy of the document, then use the Drive API to copy over the content and metadata from the original, including the version history. Once copied, you can then create a new named version on this copy with your desired name.

Here’s a rough outline of the steps:

  1. Use Drive API to create a copy of the original document
  2. Copy over the content and metadata
  3. Use the revisions.update method on the new copy to create a named version

This approach maintains the version history while allowing you to add a named version. It’s not perfect, but it might serve your needs. Let me know if you’d like more specifics on implementation.

hey emma, i’ve dealt with this before. sadly, google doesn’t allow renaming versions through their API :frowning: the only way i found was to make a new ‘named version’ manually in the UI. maybe you could use selenium to automate that process? not ideal, but it might work for ya.

As someone who’s worked extensively with Google Docs API, I can confirm that programmatically renaming versions isn’t supported. It’s frustrating, I know. However, there’s a workaround I’ve used that might help.

Instead of renaming existing versions, you can create new named versions programmatically. Here’s the basic approach:

  1. Make a small change to the document (like adding and then removing a space).
  2. Save the document, which creates a new revision.
  3. Immediately after, use the revisions.update method to name this new revision.

This method isn’t perfect, but it allows you to add named versions via code. You’ll need to use both the Drive API and Docs API for this. It’s a bit hacky, but it’s the closest I’ve gotten to achieving what you’re after. Let me know if you want more details on implementation.