Build a Book Recommender with AI in Google Sheets
Learn how to create a book recommender using AI in Google Sheets, leveraging Apps Script and the OpenAI API to generate personalized book suggestions based on user input.
Hey, so let's build a book recommender. We're going to get an email form from online. We're going to create that email form. We're going to get an email. And then we're going to send that email to someone and say, Hey, here's some books I recommend for you. And they're might. Input some books. So we have some input and we saw we have a book we want to recommend or books. So let's start in Apps Script . For members of BetterSheets, I have this script all ready for you. Get it at Sheets Resources in the video called Upgrade Google Sheets OpenAI Prompt to GPT 4 . I already have this written for you. So let's get that AI prompt here.
go grab an API key to test this. So we got an API key from OpenAI here and we're going to run a prompt. What's that prompt that we're going to run? Uh, let's just add it here to the prompt, uh, to the function . We're going to do variable prompt equals based on this list of books. Please, uh, suggest one more book. to read and tell me quickly how, uh, why I should read that book based on the books I input I list here. Uh, and then we're going to do plus books and we're going to get, uh, books here. So we'll insert a list of books in text and we're going to get OpenAI API Prompt with Array of Cells
a book recommendation out of it. So let's get that book recommendation, books input. So I'm going to create a card. C A R D. It's truly just a really easy and fast way to create a little HTML page. We're going to create a form here. Let's see where is form. We're going to say custom, uh, send to URL. Uh, we'll create an API for this. Uh, we're going to fill in the API later. We need a post, uh, and we want actually, Yeah. Two things. We want email, text, text. Actually we can say email here if we want. ID, email. We're going to create another form here and we're going to call this books.
Books, and we're going to add the ID books. And so when we send this, uh, this form, we're going to get an email, we're going to get a list of books, and doPost here. So function doPost. And we're going to get, uh, some, uh, URL here, uh, actually an input from a URL. And variable email is equal to e. parameter , parameter . email. That is literally what we put here as the ID. So whatever ID you put in it, you can get it. Books equals e. parameter . Books. I think we did books. Yes, books. Not just book. And now we want to get, uh, we want to return just 200.
Uh, but actually instead of 200, it's going to be content service. Create text output 200. That is just the reply we wanna send back, but we wanna email someone. And so. So let's get actually a variable, uh, recommendation equals AI books. And so we're going to insert, basically take whatever books we get, put that in the recommendation and get an output down here, return JSON to choices, whatever the return is of that. But we want to save this to, uh, the sheet. So we're going to do spreadsheet app dot get active spreadsheet . Yep. Dot get sheet by name, and we're going to call this database
sheet by name database. First we're going to insert a row after one, and then we're going to take that same database and we're going to dot get range , uh, column one, no, sorry, row two, column one, uh, and one cell wide and one cell big, set value, and we're going to say, I think our first thing we're going to do, yeah, email, we're going to get the input, and then we want the book that they recommended. So we're going to actually copy this all the way again, paste it twice, and instead of the first column, it's the second column, and this is books, books.
That's what we get up here, books, and then we're going to get the recommendation here. And we're going to print that out into the cell. So how do we actually email the person? Well, we're going to create another function called email, uh, we're just going to call it email, and we have the email, and we have recommendation. Do we spell this correctly? There we go, recommendation. And what's that function ? We'll create it here, function email. And we have email and recommendation. We are gonna do mail app, do send email. We're gonna send an email to, is gonna be email.
The subject is going to be book recommendation and our body is gonna be the recommendation for right now. We're just gonna send that super simple. And to use this do post, we need to deploy new deployment. Let's see if this is all going to work. We're going to select our type web app. We are going to run as me, uh, as me and anyone can use it. Deploy, authorize access. We need to authorize the use of all of these functions . It'll say which function we're using. Spreadsheet app, mail app, all that kind of stuff. Authorize it. It's going to send email as me, and I'll probably put my own email address so it's going to send it to me. Get this URL here. We're going to copy that URL. You can always get it again. Uh, over here in our form, actually, we'll call this book recommender.
And actually I'm going to rename this Books I've Read and Liked. Uh, Over here, send to URL. Put that URL in here. Do not do anything else to it. We don't have to. It's going to form its own, um, stuff from the post. It's going to create a little parameter there called email, called books, and then it's going to add those parameters . Click done. We're going to save this as a book, book recommendations. Git. Where you can get book recommendations. We're going to save this as a book. recommender. car. co Let's see if that's available. It's checking. It's available. Yes! Let's publish it. This is a speed run to see how if we can do this.
Alright, I'm going to use my own email address, andrew. bettersheets. co. I've read Project Hail Mary and Hitch Hiker's Guide to the Galaxy. So, based on those give me some recommendation. We're going to hit submit I think we're going to hit, hit submit. See if it's going to work. Exceptions Ah, okay, uh, so line five we got to do a little bit of fix here. Insert rows , I think I need to do insert row . That's, that's a little, uh, That's a little error here. New deployment, let's try it again. Okay. Deploy. We have to get a new URL. Every time we, uh, deploy a new web app, we're going to get a new URL.
Go back over here. Okay. We're going to insert it into this URL. The new one. Click done. Click publish . Publish changes. And the next thing I'm actually going to do is I'm going to open this site is, uh, incognito because I think there's sometimes there's a weird thing that happens. Thanks. Okay. Email. Again. Andrew at BetterSheets. co. Let's use the same submission. Let's see if there's any error . We got an error . Type error email is not a function . Okay. Let's go and look at that. Email, function . Hmm. What we could try to do is put this line here instead of using this function .
We'll just use mail app. Let's see if that works. Deploy new deployment. Again, we go through the same process. Again, deploy as anyone we might, might have a problem with this email, but we can, there's other ways to fix it if we don't, uh, if we do have a problem with it, okay, let's go back and fix our URL done, publish , publish changes. We are doing a fast, quick run here of through these errors. All right. Email, same email, same recommendation. Now we got a 200 code. That is plus, A plus, awesome. We have no recommendation. Uh, oh, actually, I would suggest reading the Martian by Andy Weir.
Okay, so this is the recommendation in the wrong place. Let's go fix that. Um, what did I do here? Oh, I didn't fix this last one, so this would be three. So it should put the email, the books, and then the recommendation, and it should email us. Let's check our email. We got a book recommendation in our email. I would suggest you read The Martian. This book would be a great choice for you based on your enjoyment. Of course, I would recommend The Martian, too. Um, it combines elements of science fiction and humor. Yeah. Uh, perfect. We got it in our email. So now So let's go fix that little error we have, uh, we want to save these emails, save the inputs, and save the book recommendation to our database. Let's do it all again, deploy, new deployment, deploy to anyone, get that URL, fix it up in our site.
All we have to do is put it in the URL, done, hit publish changes, and we're going to have a book recommender saving to our database, emailing. Let's see what we get. So actually, I'm going to try the Bible and Project Hail Mary. Let's see if that works. We got a 200. Let's see if we have book recommendation. I'd recommend Dune. Great recommendation based on those two, the Bible and Project Hail Mary. Very religious and religion, power, all this stuff. Yeah. Great recommendation. Uh, we have it. Do we have it in our database. Yes, we do. And so now we have created a book recommender in what, 11 minutes here? Really cool.
Here's the code for you. Uh, if you are a Google or if you're a Google Sheets user and you're trying to do this, we did it. If you are a BetterSheets member, you can grab the URL of the sheet down below, copy the sheet to your Google Sheet drive.