This post shows you how to connect large language models—such as GPT or Claude—directly to Google Docs using Apps Script. With just a few setup steps, you can send selected text to your preferred AI model, receive improved versions, summaries, stylistic rewrites, or even answers to complex questions—all without leaving the document. Whether you're refining tone, translating, generating citations, or restructuring paragraphs, this setup transforms Google Docs into a personalised, intelligent editing environment.
Open a Google Docs file and launch the Script Editor
To begin, open any Google Docs file and head to the Extensions menu at the top. From there, choose Apps Script to launch the script editor in a new tab. This is where you’ll write the code linking your document to a language model via its API.

Before writing any code, it is essential to store your API keys to enable script access securely. In the Apps Script editor, click the gear icon in the sidebar to open the Project Settings. Scroll down to the Script properties section and select Edit script properties. Here, you may define custom keys that your script will reference during execution. Once saved, these values will be accessible throughout your Apps Script code, thereby eliminating the need to hardcode your credentials. This represents a straightforward yet crucial step in maintaining a secure and well-structured setup.

In the script editor, delete the default function myFunction()
block and paste your custom code instead. This script will define the user interface and handle communication with the GPT or Claude API. Once your API keys are set, you can move on to writing the actual script. Start by replacing the default myFunction()
block with the complete code provided below. This script introduces a custom “AI Tools” menu into your Google Docs interface, offering two options: one for sending selected text to GPT, and another to Claude.

Test the Integration via the AI Tools Menu
Once the script has been saved and authorised, return to your Google Docs file. You will now see a new AI Tools menu in the toolbar.

Here, you can enter a short, task-specific instruction such as summarise, rewrite, or improve clarity. The selected text will then be sent to the GPT model, and the formatted response will appear directly below your selection, clearly marked as the AI's output.

Prompt
Rewrite the following paragraph in formal academic style suitable for meeting minutes.
Output
The prompt executed within a few seconds, and the response from the GPT model was automatically inserted below the selected section in the document. The integration was smooth and non-intrusive, allowing users to review the AI-generated suggestion in context without overwriting the original text. This setup offers a practical and efficient way to experiment with rewriting or summarising content directly within Google Docs.

Recommendations
This lightweight integration is ideal for writers, editors, researchers, and professionals who regularly work with Google Docs and want to enhance their productivity using language models. For best results, we recommend using concise, task-specific prompts tailored to the content—such as “make this more formal”, “summarise for a lay audience”, or “improve clarity”. Be mindful of any sensitive data, as API calls involve external processing. Lastly, explore both GPT and Claude if your script supports multiple models; they often yield different styles and strengths, and alternating between them can help refine tone and quality even further.
The authors used GPT-4o [OpenAI (2025), GPT-4o (accessed on 13 June 2025), Large language model (LLM), available at: https://openai.com] to generate the output.