Go To Random Row Custom Function
Learn how to create a custom function in Google Sheets that activates a random row, enhancing your spreadsheet functionality with a simple script.
So let's create a little function , a little functionality that activates a random row. So I have everyday edge here. This is a free template that I created to show you how to create templates. So I actually have a course called Selling Spreadsheets, and this is one of the spreadsheets that you can take. And you can make it your own. It's a really cool thing that I, I literally made this for myself, is how to make a bunch of exercises that you can do at any time during the day. It shares with you what activity you're doing, what exercise you can do, the description, how many minutes it takes, how easy it is, what the target areas. You can build your gym routine around your everyday routine. But that's beyond this video. This is a free template . Go to better sheets.co/everyday edge if you wanna download this.
But I want to create a little function that says go to a random row. Because ah, sometimes we're like, ah, what, which one should we start at? So I'm gonna go to Extensions app script , and over here, let's call this just our automations. I want to create a function here called Go to Random Row. I also want to be able to access this random row from the sheet, not have to come here. So I'm gonna go over to Better sheets.co/snippets. And I'm gonna grab my custom menu function . I'm gonna insert that above here. And what it's doing is creating a ui, or getting the ui, then creating a menu called custom menu . I'll actually call this workout menu in this particular case, and I'm gonna have this first function , go to the function , go to random row, and I will call this.
First part, the human readable part, go to random row. I will have other automations here or other menu items here, but I'm gonna delete that for right now. And now I can save this. The functionality doesn't exist yet, but we can put in something. You can do spreadsheet app dot get active spreadsheet . That's the entire spreadsheet file. Get active sheet . Get range , and let's just say I want to go to row 50 in column two and I'm gonna do dot activate. So this is the whole thing to just go to row 50, but I want to show you how to get this menu here. So I just need to save all of that. I can either hit save or I can go to this button here that says Save Project to Drive Next to Run.
And I'm gonna refresh my sheet. I'm gonna close it and open it again. That means I'm gonna have to close that app script and right at the top of extensions help accessibility. Then here's workout menu. Go to random row. This right now will go to row 50. I do have to authorize it the very first time I use it, but there we go. We've activated row 50. So let's do that again to show you that it's working without authorization there. Go row 50. We're activating it. Great. But let's make that a random row. Go up to extensions app script and we need to take this number 50, delete it, and go random row. This is gonna be a variable that we're gonna create. Variable, random row. Equals, well, there's a few variables here. We need to know what row do we wanna start at.
So we're gonna create variable start row equals three variable max rows. That's how many rows there are. This is equal to, we're gonna grab this active sheet . We can create that as a variable. Active sheet equals this, and we can actually replace this part. With that variable active sheet , and we're gonna go max rows active sheet .gi maximum rows. So this gives us how many rows are in this particular sheet that we're on right now. So exercises, and we're gonna have to do a little bit of math here. Math, meaning MATH with a capital M. This is a set of F formulas and functions within app script all around math. And we're gonna need to use math floor and say math random times. And now we're gonna have a little bit of a function here, a little bit of algebra.
So we need to use, we need to use Max Rose. Minus start row plus one, and then at the end, so this is gonna get us a random number that's multiplied by this many, and then outside of this math dot floor, we need to actually add the start row again. Meaning we're gonna get a random number between basically the difference between the max rows and the number three. Then we need to add start row again just to make sure we get within the index , the actual number of rows. So hopefully this makes sense and I wanna make this a little easier to grok, so I'm gonna. Formatted a little bit. So we see math, the floor of all of this math thought random times this number of Max's minus start row plus one. Once we get that, we add the start row again, so command
S, and let's test this out. Workout menu. Go to random row. We went to 58 this time. Let's do that again. We're at 17. It is moving our cursor and activating a random row 74 that time. Pretty cool, right? That we can just do this with not that much function , right? Not much typing, really. That's 1, 2, 3, 4, 5 lines of code to go to random row, and then we're adding it to this handy dandy. custom menu again, go to better sheets.co/snippets for this custom menu and a bunch of other app scripts you can copy and paste into your sheet. Hope you enjoyed that. You are watching better sheets here on YouTube. Make sure you check out this video or this video and subscribe right
now to get more tips, tricks, how tos, get more out of your Google sheets than you ever have before. I'm excited to be making a ton more videos here. Ask me questions down in the comments and I will answer them in future videos. But for right now, right here, one of these videos is gonna be your next Google sheet.