AI Apps Script Code here: https://bettersheets.co/snippets/updated-open-ai-model-to-omni
Members-only tutorial
Watch the video and get the practice sheet with membership.
About this Tutorial
AI Apps Script Code here: https://bettersheets.co/snippets/updated-open-ai-model-to-omni
Sheet Resources
Video Transcript
<div>0:00 I'm going to show you really quick how to create an AI-generated poem based on an input, like a name here.<br>0:06 We have a sheet called poems, we have two columns, name and poem. When we enter any name here, like Francine, and we hit enter, a poem is going to show up in the B column here that is generated by AI.<br>0:23 How do we do this? It's through AI, it's through some Apps Script Learn how to automate tasks in Google Sheets using Apps Script. Opens in new tab .
I'm going to show you every step of the way, and I've already coded some of this for you that you just have to copy-paste.<br>0:34 So sit back, relax, and enjoy this ride through creating an AI-generated poem based on a name. We're going to need a few things.<br>0:44 One is an API key, you're going to get that at OpenAI. The second thing is over at betasheets.co.uk slash snippets, I've already created this updated OpenAI to actually delete this, and we're going to paste our AI prompt.<br>1:18 We're going to do a couple of things to it.
One, instead of a prompt, we're going to just need a name.<br>1:25 Instead of API key here, I'm going to add it as a global API key equals this here. By the time you watch this, this API key will be deleted.<br>1:41 We have prompt here. We still need to add back prompt variable, prompt as a variable, and it's going to be pretty simple.<br>1:49 Create a short two-line rhyming poem with this name, period. then name. Quo, uh, colon plus name.<br>2:05 So this name here in this AI function Discover how to create reusable functions in your spreadsheets. Opens in new tab will be here. And then we're going to use this exact prompt.
We need to change API key as well.<br>2:13 Let's do that here to change it to that. And let's go get an API key. I've already gotten an API key.<br>2:21 Copy it. I'll have deleted this by the time you see. We're going to put it in quotes here as API key.<br>2:30 We can test if this is going to work actually by just saying variable name equals Nancy. And let's run just AI right now.<br>2:41 The first time you run it you'll have to generate, uhm, you'll have to authorize it. Thank you.
And instead of return, let's actually go and logger.log this last piece, this json.choices.message.content.<br>2:59 And let's run it again and you'll see it right here in this log. Nancy dances with endless glee, her spirit wild and fancy free.<br>3:07 There we go. So it works. We know it works now with this logger. Let's delete that. And go back up and delete this name and put it as a variable here.<br>3:17 So we need one more thing.
We need a function Discover how to create reusable functions in your spreadsheets. Opens in new tab that actually goes to that AI function Discover how to create reusable functions in your spreadsheets. Opens in new tab , returns the poem, and then enters it where Understand how to filter data using the query where clause. Opens in new tab ?<br>3:26 Well, we're going to have a name in the A column, and we need to enter it in the B column, wherever that row is.<br>3:34 So, we're gonna call this function Discover how to create reusable functions in your spreadsheets. Opens in new tab , createPoem, and we're gonna need an event, because what we're gonna do is, basically, we create this function Discover how to create reusable functions in your spreadsheets. Opens in new tab , then we create a trigger Set up triggers to automate actions in your Google Sheets. Opens in new tab that says, when something happens, go do this, this function Discover how to create reusable functions in your spreadsheets. Opens in new tab .<br>3:51 So the very first thing we need is the function Discover how to create reusable functions in your spreadsheets. Opens in new tab to trigger, though.
And then we'll build the trigger Set up triggers to automate actions in your Google Sheets. Opens in new tab in this video.<br>3:57 It's gonna be super easy. We need a few things, though. Variable ss equals spreadsheetApp Explore the SpreadsheetApp service for managing Google Sheets. Opens in new tab .getActive, getActiveSpreadsheet. That's just gonna be the entire file.<br>4:09 We're gonna need that, generally, most of the time when we're creating functions Get familiar with various functions available in Google Sheets. Opens in new tab here. We need to know what row we're on with this e event that's going to be triggered, So, e. range Learn about selecting and manipulating ranges in your spreadsheet. Opens in new tab .getRow. I wonder if you know it.<br>4:26 Got column. There it is.
And we need to know what sheet name Understand how to reference and use sheet names in formulas. Opens in new tab we're on, just to make sure we're on the correct sheet name Understand how to reference and use sheet names in formulas. Opens in new tab .<br>4:34 Equals ss.getActiveSheet.getName. Now, we can log this, but I'm just gonna keep going.<br>4:48 Thank Bye. Now, we're gonna do if sheetName is equal to poems. So let's put an ampersand here and col is equal to one.<br>5:18 We also want to make sure we're not in the header row Learn the importance of header rows in organizing your data. Opens in new tab . This is just a little bit of a optimization.<br>5:23 We don't want to be changing the header Discover how headers can enhance your data analysis. Opens in new tab and then it creates a poem there.
Uh, there is one more thing we can add here is essentially we don't want e.value to be blank.<br>5:35 Thank you. Exclamation point equals means not equal to, two quotes with nothing in it means blank. But now, once we have the, these all are yes, true, well, we want the poem.<br>5:54 But we want the poem in a very specific place. We want it in the same row we're on in that sheet.<br>6:00 So we're going to go ss.
ss.getSheetByName, poems, because that's where Understand how to filter data using the query where clause. Opens in new tab we are, getRange Find out how to retrieve specific ranges of data in Sheets. Opens in new tab , we are in row, whatever row we're on, and in two, the second column, we want to set value is the poem.<br>6:20 Well, how do we get the poem? We'll go variable POM equals AI and put in the name. But how do we get the name?<br>6:27 Oh, that is just variable name equals ss.getSheetByName, poems, getRange Find out how to retrieve specific ranges of data in Sheets. Opens in new tab , whichever range Learn about selecting and manipulating ranges in your spreadsheet. Opens in new tab we're in, in the row, first column, getValue Learn how to extract values from specific cells in your spreadsheet. Opens in new tab .<br>6:46 So, here we're getting the value of the first one.
First column, whichever row we're in, and here we're setting that value as the poem.<br>6:54 And here we're getting the poem. Pretty simple, right? Let's save it. Now if we save this right away and try to use it right now, this createPoem has no trigger Set up triggers to automate actions in your Google Sheets. Opens in new tab .<br>7:06 We're not triggering it anywhere. So now I'm going to show you how to create the trigger Set up triggers to automate actions in your Google Sheets. Opens in new tab . over on the left side, go to triggers.<br>7:13 We already had created one before. It's a test. So let's delete that. This is what it'll look like when you open your triggers.<br>7:19 We're going to add trigger Set up triggers to automate actions in your Google Sheets. Opens in new tab .
It's on the bottom right, big blue sign. We're going to choose which function Discover how to create reusable functions in your spreadsheets. Opens in new tab to run.<br>7:25 It'll be createPoem. Now this event source will be from the spreadsheet Explore the features and functionalities of Google Sheets. Opens in new tab . Keep it as that. But this event type, we're going to change this to onEdit.<br>7:36 Let's click save. Once that's saved, we have it here and we'll see if there's any errors. So let's do something like this. If we write a poem.<br>7:49 This is a poem. Nothing's happening. Let's write a name. George. Does anything happen? Yes.
It took a moment because it does have to do a little bit of a, a run over to AI, get the response and come back and then set that value.<br>8:09 In a land of dreams and old folklore stands the bold and brave Sir George. I don't know if George and folklore actually rhyme.<br>8:16 We can try that again. Let's just delete that. And go George again. See if it comes up with another one.<br>8:25 Let's delete, delete, George. Will it work this time? Let's try again with a Gary. Seems like it might be just a little bit slow.<br>8:45 Gary, with dreams that never tarry, finds adventure in worlds unwary.
Okay, it's a little weak, but it is generating these poems based on the name all in this script here with the trigger Set up triggers to automate actions in your Google Sheets. Opens in new tab .<br>8:59 So, to review, definitely need an API key. We need to create the function Discover how to create reusable functions in your spreadsheets. Opens in new tab AI, which I've already written for you over at bettersheets.co slash snippets.<br>9:08 This one is the updated OpenAI model to Omni.
we need to get that AI function Discover how to create reusable functions in your spreadsheets. Opens in new tab , but then we need to trigger Set up triggers to automate actions in your Google Sheets. Opens in new tab the poem creation, which is just grabbing whatever we're editing, making sure we're in the right place, getting that name, putting it through AI, getting the poem back, and setting that value.<br>9:27 So it's this three steps here. Thanks so much for watching and learning along with me. If you're watching this on bettersheets.co, down below is this sheet and the code all written for you.<br>9:41 If you're not yet a member, become one today over at bettersheets.co.</div>
Courses
Quickstart Tutorial OpenAI API in Google Sheets
Upgrade Google Sheets OpenAI Prompt to GPT 4
Perfect Use of AI in Google Sheets (Categorize Tasks)
How to Create AI Images From Google Sheets
Make AI-generated Poems Based on Name
Quickly Integrate AI and Google Sheets
Saving OpenAI API Calls inside of Google Sheets
Build Your Own AI Writer in Google Sheets
Fast FAQS
Created a GPT function in Sheets
Spreadsheet Automation for Beginners
Ultimate Prompt Writer and Prompt Rater
ChatGPT Clone in Google Sheets Part 1
Mr. Beast Renamed My YouTube Titles
I Built a SEO Meta Description Writer
Create a Waterfall of AI Prompts and Responses
ChatGPT Clone in Google Sheets Part 2
Run 100 ChatGPTs at One Time
Prompt Parade
Building an AI Prompts Playground
YouTube Title Rewriter with GPT-3
Ultimate Headline Writer with GPT in SHEETS
Create a ChatGPT Cheat Sheet Spreadsheet