Here are some key points to notice in the code above:
Paste the API key that you created earlier inside the quotes.
We will be using the Chat Completions API. You can find more details about it here.
ChatGPT models have different roles, such as system, user, and assistant.
The systemContent parameter is where you provide roleplay for the GPT system. For example, you could say “You’re an expert algebra teacher” or “You’re an expert CV writer”.
The userContent parameter is where you provide tasks to perform for the model. In our case, we will provide long passages from the spreadsheet to summarize and simplify.
Again, for the custom formula, we’ll create a new file named formula and then we will create a function named GPT_SIMPLIFY:
The GPT_SIMPLIFY formula simplifies whatever text is provided as input. The input to this function is data that is coming from the spreadsheet. When you select a range, a cell, or multiple cells, the data in the range will be automatically provided by the spreadsheet to this formula.
The systemContent is defined to be passed as the first parameter to the fetchData(systemContent,userContent) function.
We are checking if the input is an Array because the data passed to this function can either be a nested array or just a string if we select multiple cells or single cell, respectively, in the spreadsheet.
You can read more on custom functions on this page.
Now go ahead and apply this formula in your spreadsheet. I copied some text from a book I’m reading in the first column and applied the formula in the second column named “Simplify Passage”, like this =GPT_SIMPLIFY(A2) for the second cell.
Note: Make sure to refresh the spreadsheet before you apply the formula to sync with the latest changes in the script.
GPT_SUMMARIZE
To summarize the formula we’ll just copy the simplify function and some other things, as you can see in the code below.
The main thing to notice here is the different system content.
Note: Since this is not a tutorial on how to use ChatGPT optimally, I provided instructions as the system content instead of role-play, and then just provided data in the user content. You can improvise this by providing roles in system content, and tasks as well as data as two different user roles in our FetchData() function.
GPT Rate Limit Error
For free users, the rate limit to use the API is 3/minute. As such, when you apply these formulas in more than three cells you’ll encounter the error. Luckily the execution won’t stop because we’re returning an error string from fetch data which will be saved into those cells.
Auto Refresh and Error
Moreover, the auto-refresh feature of the formula can force the re-application of the formula on cells that already have satisfying values whenever source cells are updated, in our case cells in column “A”.
When we add a rate limit on top of auto-refresh it can cause a conundrum. You can technically make changes in custom functions to accommodate such circumstances but, I like to keep formulas light and efficient. So, I recommend we instead create custom menus and apply these functions manually.
Integrate GPT Chat API in Sheets Menu Functions
GPT_SIMPLIFY
First, let’s create another file named menu. Then we’ll create the gptSimplifyMenu function which will be an alternative to the GPT_SIMPLIFY formula:
Key points that are different to understand in this code are:
We’re hardcoding the data sources, as such data[i], which refers to the second column (that is “Simplified Passage”) as shown in the spreadsheet image above. This means that if you’re using some other columns to save data from ChatGPT to, then you’ll have to make changes according to it.
We only fetch data when the target cell is empty or contains an error message. This helps to avoid unnecessary API calls.
Add a Custom Function as a Sheets Menu
The function is ready to be tested, but it still will not appear in the spreadsheet. To do so, we’ll need to provide the following instructions:
We add a function to the menu with the menu.addItem("GPT SIMPLIFY", "gptSimplifyMenu"), where the first parameter is the title for display and the second is the function to call when pressed.
Add the menu to the UI with menu.addToUi().
The onOpen trigger runs automatically whenever the document the script is attached to reloads and as such will add a menu to the spreadsheet as shown in the image below.
GPT Summarize Menu
We’ll make some minor changes after copying the simplify function as shown below:
The system role has been changed to address summary instruction.
The target column to save data is now the third column.
The doc string has been adjusted as well.
As for adding this function to the menu I’ll leave it to you.
Tips
All you need to make your own formula like =GPT_COVER_LETTER_CREATOR() are the following modifications:
To FetchData
You can change System Content Description should to address your needs, like “You write an expert cover letter for software developers”.