Create Instant Summaries in Google Docs: A Step-by-Step Guide with OpenAI API and Google Apps Script

Aryan Irani
8 min readJun 8, 2023


In today’s fast-paced digital world, effectively condensing lengthy content has become a necessity. Whether you’re a student grappling with research papers or a professional striving to stay ahead, generating concise summaries can save time and boost productivity. Luckily, the powerful combination of OpenAI APIs and Google Apps Script provides a seamless solution for effortlessly summarising paragraphs within Google Docs.

In this blog, we’ll explore how to integrate OpenAI API and Google Apps Script to generate paragraph summaries in Google Docs. I will guide you through the setup process, show you how to extract meaningful summaries using the OpenAI API, and seamlessly implement the solution in your Google Docs workflow.

Sample Google Doc

For this blog we will be using a very simple Google Doc that contains a paragraph for which we want to generate a summary for.

If you want to work with the Google Doc, click here. Once you make a copy of the Google Doc you have to go ahead and change the API key in the Google Apps Script code.

If you want a video version of this blog, you can check out the YouTube tutorial below.

Step1: Get API keys

To obtain an OpenAI API key, start by logging into your OpenAI account on their website. Once you’re logged in, navigate to the API Keys tab on your Dashboard page.

Click on the Create new Secret Key button. Finally, click on the “Generate API Key” button to generate your new key, and then copy it to your clipboard. Be sure to store it in a secure place as it will not be visible again.

With your API key in hand, you can now use it to authenticate your requests to the OpenAI API and integrate ChatGPT with Google Docs. Simply add the key to your Google Apps Script code and use the appropriate functions to make requests to the API. You’re now ready to start using the power of AI to enhance your Google Docs!

Step2: Write the Automation Script

While you are in the Google Docs, let’s open up the Script Editor to write some Google Apps Script. To open the Script Editor, follow these steps:

  1. Click on Extensions and open the Script Editor.

2. This brings up the Script Editor as shown below.

We have reached the script editor let’s code.

Now that we have the Google Docs setup and the API key ready, let’s go ahead and write our Google Apps Script to get the summary for the paragraph in the Google Doc.

function onOpen(){

var ui = DocumentApp.getUi();
ui.createMenu('Summarise paragraph')
.addItem("Summarise Selected paragraph",'accessdoc')

We are going to start out by creating our own custom menu using which we can select the paragraph we want to summarise and run the code.

To do that we are going to start out by opening a new function called onOpen(). On opening the function we are going create a menu using the createMenu() function, inside which we will be passing the name of the menu. After that we assign some text to the name followed by the function you want to run when the menu is clicked.

//Replace with your API key
var API_key = "your_api_key";

After creating the custom menu, we are going to go ahead and declare the API key that we just generated. You have to replace this with the your API key.

function summarydoc(paragraph){

var model_ID = "text-davinci-002";
var maxtokens = 200;
var temperature = 0.7;

Now that we have the API key ready, it’s time to go ahead and start writing the function that will generate the summary. We are going to open up a new function called summarydoc(paragraph) inside which we will be passing the paragraph as a parameter.

On opening the function, we will be declaring the model that we want to use, followed by the max tokens and the temperature of the response.

To know more about the models the Open AI API has check out the link below.

  var payload = {

'prompt': "Please generate a short summary for :\n"+paragraph,
'temperature': temperature,
'max_tokens': maxtokens,
"presence_penalty": 0.5,
"frequency_penalty": 0.5


This part of the code sets the parameters for the Open AI API request. We start out by declaring the desired model that we declared previously. After declaring the model, we go ahead and declare the prompt that we are going to use. In the prompt we add the paragraph that we want a summary for.

Once we declare the prompt, we declare the temperature to control the randomness of the response based on the prompt. After this we declare the number of tokens to generate the response.

  var options = {

"method": "post",
"Content-Type": "application/json",
"Authorization" : "Bearer " + API_key
"payload": JSON.stringify(payload)

The options variable is used to design the parameters for the HTTP request that will be sent to the Open AI API. We start out by declaring the method parameter to post which indicates that request will be sending data to the API.

The headers specifies the type of data that will be sent in the request, which in this case in “application/json” which is a commonly used format for sending structured data over the internet. Additionally, the “Authorization” header is set to include the API key we generated earlier.

Finally, the “payload” parameter is used to specify data will be sent in the request. In this case it is a JSON object that contains the Model ID, prompt, temperature and the max tokens for the API request.

  var response = UrlFetchApp.fetch("",options);

These options are now passed as an argument to the “UrlFetchApp.fetch” function which sends the request to the Open AI endpoint, and returns the response that contains the API generated text.

In this case the API endpoint is" which is the endpoint for the ChatGPT API provided by OpenAI.

The options variable that we defined is passed as an argument to the fetch function to provide additionally parameters for the requests. The fetch functions then sends the request to the API endpoint and waits for a response from the server. The response from the server will contain the AI generated text that will be stored in the response variable.

var summary = JSON.parse(response.getContentText());
return summary.choices[0].text.trim();


Now that we have sent a request to the Open AI endpoint we get a response back. We are now going to parse this response.

The getContentText() function is used to extract the text content from the response object. Since the response is in JSON format, we use the JSON.parse function to convert the JSON string into an object.

The parsed data is then stored in the summary variable, which contains an array of choices that t he AI model has generated for us based on the prompt.

Since we are only interested in the first choice of the array, we use summary.choices[0] to access that choice. We then use the .text to extract the text from the choice and finally use .trim function to remove any leading or trailing whitespace from the generated text. On getting the first choice from the array we return the summary.

function accessdoc(){

var selection = DocumentApp.getActiveDocument().getSelection();
var text = selection.getRangeElements()[0].getElement().getText();
var summary = summarydoc(text);

Now that we have the summary function ready and good to go, we go ahead and open up the function that will be interacting with the Google Doc. We want the summary to be generated for the paragraph that the user selects.

To do that we are going get the selected text from the Google Doc using the getSelection() function. Once we get the selected text we go ahead and get the text using the .getText() function. To generate the summary using ChatGPT we pass the text in the summarydoc() function.



Now that we have the summary for the selected text, its time to append the paragraph back into the Google Doc. To do that we are going to be using the appendParagraph() function inside which we will pass the summary variable. Just to divide the summary from the original paragraph we append an additional line that says “Summary”.

Our code is complete and good to go.

Step3: Check the output

Its time check the output and see if the code is working according to what we expected. To do that go ahead and save your code and run the OnOpen() function. This will create the menu that we can select and generate the summary for the paragraph.

On running the code you should get an output like this in the Execution Log.

On running the onOpen() function the custom menu has been created in the Google Doc successfully.

To generate the summary in the Google Doc, follow the steps.

  1. Select the paragraph you want to generate the summary for.

2. Once you select the paragraph go ahead and click on the custom menu and click on Summarise Selected paragraph.

3. On clicking the option, you will see that code will generate a summary for the paragraph we selected.

Here you can see the summary for the paragraph has been generated in the Google Doc successfully.


In conclusion, the integration of OpenAI API and Google Apps Script empowers users to generate concise summaries of paragraphs in Google Docs effortlessly. This powerful combination revolutionises information processing, enhances productivity, and saves valuable time. Experience the transformative benefits of intelligent text summarisation today and streamline your workflow with impactful summaries in Google Docs. Maximise efficiency and productivity with this remarkable integration. You can get the code from the GitHub link given below.

Feel free to reach out if you have any issues/feedback at