Build Your Own AI Writer in Google Sheets
Learn how to create your own AI writer in Google Sheets using Apps Script. This tutorial covers everything from writing prompts to logging responses, enabling you to build a functional AI tool for your needs.
This video is going to show you how you can build your own AI writer. We're going to start very simply, and we're going to end up doing these five things. One, we're going to write a prompt, get a response. That's the most simple aspect of AI in Google Sheets. I'm going to show you where you can get that app script , which is actually totally free. If you're watching this somewhere else other than BetterSheets, you can go to bettersheets.co/snippets and find the OpenAI blog. API prompt and response that I'll be using in this sheet. Everything else, though, for members, if you're watching this on BetterSheets, down below you can get this sheet and the completed sheet with all of the Apps Script . If you are not watching this on BetterSheets, become a BetterSheets member today and you can access the Apps Script , you can get the sheet exactly to, you can copy it directly to your Google Drive When you're watching this on Better sheets.co. So first off, we're going to write a response, get a re, we're gonna write a prompt and get a response. Build Your Own AI Writer in Google Sheets Easily Write 1,000 Headlines in Google Sheets Build Your Own AI Writer in Google Sheets Bigger Brain
Then I'm gonna show you how to write a res, write a prompt, but then get the response as values only. This is actually really a huge step because if we just write a function inside of App Script that uses this URL fetch app, it's gonna run every 10 minutes. It's gonna cost us a lot of money after about a month if it runs for a month or more every 10 minutes. So we want to write as values only, and then. Use the AI prompts when we want them. We're also going to. Build essentially a UI for our AI writer, and I'll show you how to do that, and we're going to write the response to the same cell every time. It's a little bit of a change, a little tweak that helps a lot to create a interface here. And then we're going to create a log. This is very similar to ChatGPT. You may have seen other videos I've done. done like chat GPT clone . This is going to be similar, but we're not going to go so far as to include the past responses in the, in the prompt. We're just going to log each one. This is very useful. If you're creating your own AI writer, you're going to use it a lot and you want to see the past prompts you've used to see the past responses you've had. If you want to save these things, archive them. This is where we're going to show in this section here. Then the last thing we're going to do is we're going to write a prompt Template inside of our app script . That way you can generate your own Templates that you can use as custom functions . I'm going to show you how to do a really cool thing there. And essentially at the end of this, you're going to have all of the skills that you need to be able to build your own AI writer and possibly if you're interested in it, Create a sheet that you yourself can sell. So I have an entire other course called selling spreadsheets.
You can check, check out selling spreadsheets. com or better sheets dot co slash courses slash selling hyphens spreadsheets. And it's also available on Udemy. It goes into all the things you need to do to sell a sheet, but what it doesn't do is show you how to build a sheet like this video does. So if you're really into AI, you love using chat GPT, but you want to like write these prompts for others to use. This is the video for you and we're gonna build our own AI writer. So first off, again BetterJs. co slash snippets. Get this OpenAI API prompt. You can copy to clipboard. It's already in my Apps Script . You just go to Extensions, Apps Script , and it will open up the clipboard. The app script here you can copy and paste that function here. I have done nothing except I think I changed the max tokens to 1000 for this one.
So we can double check. Yeah, so I just copy pasted it changed the max tokens. We're going to use 1000 for this one. If you have any other changes you want to do, there's the changes temperature, top p, frequency penalty, all this kind of stuff that you may learn about AI, you can write here, but we're just going to use max tokens 1000 right now. We're using the temperature of one, we may want to change that later on, but you'll see why. You might want to lower this down to zero if you want to do something very. specific. But if you want to do some creative writing, which we'll do in this video, we're going to use one for our temperature. Okay. Write a response or sorry, write a prompt, get a response. So let's create a sheet called prompt plus response. And all we're going to do is use this function AI. We're going to write a prompt here in A1. Write one great idea. Build Your Own AI Writer in Google Sheets GPT in SHEETS Building an AI Prompts Playground AI Emails Me Every Day
This'll be probably quicker as well. We're using GPT 3. 5 turbo. So it should be pretty quick. We should get a response pretty quick here. And so we get the response to error that we don't have an API key. So of course, when you go back to API key, put in our API key. And now let's try writing this again, equals AI, A1. We'll see if we get an error , get truncated error . You didn't provide an API key. We will probably have to see this again, API key, get value. We are here and there we go. We have our answer. So again, we're just recalling the AI and getting a response with this function AI. Bye bye. Okay. So how do we actually get this to just produce values ? So we can, of course, get the response, do command C, shift command V, and we're going to get the paste values . But how do we do that straight from here? Well, we need to add another function . So we're going to do function , right? Values only. And we need to be able to run this without having to run this in a function inside of our cell. We're going to have to add a function menu on open. So we can go actually to better G seco slash snippets. And we have a custom function with two, two features or functions here. We'll take that piece that up here. And we'll call a function right values only go right values. We'll have a custom menu called custom menu. And what are we going to put in here? Well, first off, we need to actually return actually, no, not return, but right into a cell. Spreadsheet app dot get active spreadsheet, Saving OpenAI API Calls inside of Google Sheets Getting Started Coding in Apps Script Custom Menu - 2 functions Get Values from Sheet for Custom Function in Apps Script
dot get range , that range . We'll figure out later. Set value. And this value will be the, we'll call it output and the variable output equals ai. And we'll put in a prompt here, but where are we gonna get our prompt and where are we gonna write out to our set values ? What we could do is have an in intuitive, user interface, where we just have the cell here. Tick tock, let's change that to tick tock, and let's change this to LinkedIn. So we have some different ideas here. We can say A2, if we have selected A2, we'll write in B2 the answer to this. So let's get our active sheet . So that'll be our, our active sheet , active range. So our prompt, variable prompt, equals spreadsheet app dot get. Build Your Own AI Writer in Google Sheets Writer Better Prompts Build Your Own AI Writer in Google Sheets Get Values from Sheet for Custom Function in Apps Script
Get active range dot get value. So we're just getting the value in the active range . And we also want to get the row number . So we just put it in the column next to it. So we'll get row equals spreadsheet app get active range dot get row. So we know what row we're on. And the output is in row. Same row, but in column one over. So we can also do this column two if we knew, okay, we're only going to use column one, or we can get, grab that column programmatically, same way we do the row here. Okay, so in this function , whenever we click this write values in this function , on open function menu, we're going to get the value of the prompt wherever we are. We are going to get the row, then we're going to go to AI, grab that Build Your Own AI Writer in Google Sheets Getting Started Coding in Apps Script Highlight Row as You Move Your Cell Selection Get Values from Sheet for Custom Function in Apps Script
getRange . Dismiss it. Let's go back to extensions, Apps Script , and troubleshoot, or debug, our error . We can also look at our extensions, executions here. See why it failed. Gives us sometimes more information on code line 15. 15 is, Set value, output, get range . Ah, we probably need get active sheet . There we go. Not spreadsheet . The spreadsheet is the entire file, and it doesn't know which, which tab we're in, unless we do active sheet . So active spreadsheet is the entire file. Active sheet is the exact tab we are on. So let's run that again. Again, we're on A2, and ideally, if this works the first time, we'll have our values in B2. Not first time. Second time we're running this. No. Run our script, and there we go, we have some response, may not be perfect as you want, but you can always redo it, add some examples, be like, hey, we want only the title, right, only the title, and let's run that again, and it'll write over B2 because we're in the same line, there we go, we have the power, the power, AI, and again, we can change that row we're on, just by changing which one we have selected, And we're now writing to our values instead of the function . Perfect. Again, if we have to make any changes to this inside of Google Sheets or, that's right, or Excel , if we want. Write those values again, see if we get something else. Okay, we've got something else. So, so far we've created the prompt and the gotten response. We write the prompt and we get a response as values . Only we do it in an intuitive way where we have the same row. But let's say we want to write to the same cell every time.
We want to create a UI where we have some writing, some maybe examples we write, and we take all that information and then we get it in a response the same way. Just like online web apps usually do. Work as well. So now we're going to write a response at same cell every time. Let's re duplicate this and write to same cell every time. So we want to make sure we're on this page. So let's create a second value, right? To same cell, we're gonna go function , right? The same cell and the output. This is the last thing we want to do is almost the same thing here, but spreadsheet app dot get. Active spreadsheet dot get sheet by name. We'll put that sheet right to the same cell every time. Get range . Build Your Own AI Writer in Google Sheets Writer Better Prompts Build Your Own AI Writer in Google Sheets How do I reference a different spreadsheet in Apps Script?
Oh, I think we haven't renamed it. I think we have it right into the same place. But we didn't rename it. Yep. Write in D2. We'll call it write in D2 so we know that it's different than write values . Again, refresh. Now we have a custom menu , write in D2. So we'll take A2, run that as a prompt in D2. And there we go. But let's say we want to do some more stuff here, right? Of a type. So this is our template and this is our input. Actually, we will do inputs. Let's merge these. Yep. It's fine. And we'll say type is tick tock. Okay. Here, we'll use substitute and we're going to substitute. this, search for, we're going to create our own little substitution here. Build Your Own AI Writer in Google Sheets Writer Better Prompts AI Formula in Google Sheets
We'll look for type and we'll substitute it with whatever is in B4. So let's center these up. And so now we have a template , right? One title idea of here. So let's also add title or style and title instead of title idea. All right. style. And all we have to do is wrap this substitute with another substitute. And we'll wrap the substitute. The output of that substitute will be this one. And we'll write style. And instead of style, we'll write B5. Okay. So we have a template here. We can also write, get, get our prompt here. So we're writing all of these inputs. We're writing a template , getting some styles, adding these extra things, and then getting this prompt here. And we're always writing it into D2. So instead of title, let's say weird. Build Your Own AI Writer in Google Sheets Harada Method Template
We'll always put it on the second row. Two row. In the first column, we'll put the prompt. Set value will be the prompt. So we're using the same, same exact inputs just putting them in the log. Now we can take this, copy it two more times, and instead of column one, two, we will write the output. Instead of column one, we'll put three, and we'll log a timestamp . Timestamp , we'll add another variable, timestamp . equals utilities dot format date. And then we're going to just copy this from inside here. This is a good GMT. We'll do minus five for New York time. And so now, again, because we've added a new custom menu , we'll close this. Build Your Own AI Writer in Google Sheets How To Create a Stop Watch in Google Sheets Build Your Own AI Writer in Google Sheets Advanced Attendance in Google Sheets
Refresh, refresh our sheet. We now have a custom menu with D2 plus log. So anything we put here will write a awesome custom menu D2 plus log. And we'll have two things happen. It will write to D2, but it will also log it on log. And we'll see here, let's see. Why didn't that happen? Let's look at our Apps Script again and make sure we edited the correct one. Write and log. Oh, I see what we did. We wrote. it in write only. All right, take these out, put it in, write and log. Let's save that and we'll try again. Write and log. Wait for it. Wait for it. Finish script. Do we have a log? Yes, we do. We have the prompt, the output and timestamp here.
We can always make this a little bit bigger. So far, we've written a simple Prompt and response using AI in our app script . We've gotten it as values only. We write to the same cell every time so that we can create a nice UI something much better than this, but a simple UI that we can add some interesting stuff to. And now what if we wanted to create. Our own AI writer that others are going to use, but they don't necessarily want to see all of this stuff. What if they want to create, you want to create a function that is a brand new function for them to use, not in a custom menu , but is just like AI, this one AI down here, but it includes all of the stuff they want to do. Okay. So we need to create function . And in this case, our name is going to be very simple. Build Your Own AI Writer in Google Sheets Writer Better Prompts Build Your Own AI Writer in Google Sheets Bigger Brain
and now it gives us our response. That's pretty cool, but our third party, they're not gonna really like that they have to write out the word and not know if it works or not. I'm gonna show you one little thing that I absolutely love, is writing a little comment above it, and write at custom function . That's all we need to do. Hit Command S, save it. Now, we can do equals write, and this shows up. Write title idea. And it says, what are the inputs, style and platform. So now, our function that we've written, our AI writer, our prompt, that we know we only need to add two things, style and prompt in here, shows up for the user as a completely custom function native to Google Sheets. They don't know anything else. They don't know what that prompt is. They don't know what's in it. They just know that they can use it.
So hopefully this has allowed you some really cool ideas to build your own AI writer in Google Sheets. Again, check out selling spreadsheets. That's the course that I have that allows you to build a spreadsheet like this, build your own AI writer, and then sell it to a marketplace willing to use it. By pay willing to buy it. Even if you're a teacher, there's teachers pay teachers platform. There's absolute marketplace. If you're trying to sell to entrepreneurs who are too busy to write their own prompts or create, or if you have a really interesting idea on how to create your own UI in here really fun stuff, hope you are successful.