Published: 2024-06-04 Last Update: 2024-06-04

Google Sheets - Formula of Formulas

You forgot a formula? Don’t worry, we have a formula for that.

Here, we will create a custom function, using Google Apps Script, that we can use in our Google Sheets.

In Google Sheets, click Extensions > Apps Script. extensions apps script

You will see a screen like this. This is a Code.gs file where we can put Apps Script codes. apps script code.gs file

Copy this Code.gs file and paste it to replace everything in your Code.gs.

Your Code.gs file will now look something like this. apps script new code.gs file

The formula name is KLFORMULA. You can change it however you want.

Click save. apps script save

Groq API

Open a new tab and go to console.groq.com

Login however you want. groq login

Once logged in, go to API Keys. groq cloud api keys

Click Create API Key. groq cloud create api keys

Enter any name you want then click Submit. api key enter display name

Click Copy and paste it somewhere because as indicated, it will not be displayed again. Thus, it cannot be copied later. api key copy

Go back to Apps Script. On the left side, go to Project Settings. apps script project settings

At the bottom, click Add script property. apps script project settings. add script property

In the Property field, enter apiKey. In the Value field, enter the copied API key. Then click Save script properties. apps script project settings. script property

Back to Google Sheets, you can now use the function KLFORMULA (or whatever function name you changed it to).

This function accepts a cell or range of cells as its first argument. The second argument is the query you want to ask or to get from your data. Here are some examples: klformula formula. date format klformula formula. count numbers klformula formula. split text

Take note that in the last example, the result we want is separated by two pipes ||. Thus, we can use the SPLIT formula to extract them. split formula

If you are not yet aware, we are using an LLM (AI) in this formula. Thus, the result is not deterministic. Meaning, you can get different answers at different times. But we are not here for a perfect solution. We are here to use the power of AI to our advantage. I hope that you can see its usefulness and use it in your own use case.

You can check the prompt in the Apps Script code and modify it however you want to suit your needs.

In addition, because we are using an API, and it’s free, there’s, of course, a limit. Check the API usage limit here. In this example, we used the llama3-70b-8192 model. And because there’s an API limit, the problem we have here is the “recalculation” of values in Google Sheets. If the formula recalculates, that means a request to the API will be made. And thus, will consume our API usage limit. And so, I recommend that once you’ve used the formula, paste the result as values so that the cell won’t contain the formula anymore and thus, won’t recalculate. Or if you have any better solution for a cell not to recalculate (specifically for this formula that we created), do it, and also, let me know. :)