Create Rolling Dice in Google Sheets
Learn how to create a rolling dice feature in Google Sheets using randomization techniques and App Script. This tutorial covers everything from basic random number generation to creating interactive buttons for rolling multiple dice.
Let's create some rolling dice inside of a Google sheet. We need to get some sort of randomization mechanism, and we can do that with equals rand. Between, we can say Rand, between one through six. What's interesting is let's put this in the center and make it bigger. This is essentially the side of a dice, either 1, 2, 3, 4, 5, 6, and you see. Every time I edit the sheet, if I check a box or even write hi here, it's randomly changing everything. That's a ran between now. This ran between plus ran plus today, plus now will change on every edit. So at the end of this video, I'm gonna show you how to sort of
do a different randomization. But for right now, perhaps this is all you need. We can create some dice. One through six, and we can even come in here and edit this. Maybe it's a 20 sided dice and we can copy paste this and we're just gonna need a brand between. But let's say we want to actually show an image of some dice. Well, let's go over here and say we want one sided dice, two sided dice, 3, 4, 5, and six sided dice. Let's create a little rubric here, or a little table that says one here and has a dice here. So let's insert. Image in cell and we can always just Google image for dice one. There we go. Insert. Gonna insert the same and search for dice two. So we have a table here of one through six dice, and we already
have this ran between one through, let's change this back to six. All we need to do is index , and the reference is gonna be a six colon, a 11. Our row is going to be this ran between and now if we copy paste this with some dollar signs through a 11, we get five dice that are randomized, but they're all random every time I edit a cell. So how do we fix this? Well, we just need to get a different way to randomize this brand between. And I think what we're going to do is use App Script , but a very simple app script . So don't worry. I'll do all of the coding for you. We're gonna go up to Extensions app script . We're gonna create a function roll five dice. And here we're going to enter into the spreadsheet , get active sheet , get range,
and we're going to try it in a two. Right now, a two set value. And this value I'm gonna set is exactly this formula with an equal sign index and the ran between, but I don't want ran between. I want a random number . So I'm gonna add in here a couple of quotes and a plus, and right between the plus, I'm gonna say random value. We're gonna do variable random value equals. And here we're gonna do math with a capital M floor. Math random times six. And then just beyond the parentheses, we're gonna do plus one. And let's try this. We're going to save and we're gonna have our function roll five dice. We're gonna hit run.
We'll have to review permissions the very first time if we check our A two, we see that we have a new formula index , and it's the exact same index , but it has the number six here. Very cool. Right? Without that ran between, let's run it again. And now it has one. So now we could roll all five dice at once if we had these random numbers and each one of these belong to each one of the ranges . So let's fix this a little bit. I'm gonna do SS here and do variable SS equals spreadsheet . active sheet and we want a new random value for each one. So I'm gonna say random value one. And put a random value one there. And now just make this a little bit , different order.
And now let's get each of these random values . So we'll just change right? Two. In B two, random value two, and then we're just gonna copy this again. Sure. We're gonna copy this total of five times to have value. Two is three in C3. D three . D two, E two. And this value is four. And this value is five. So we have value 1, 2, 3, 4, 5, A, B, C, D, E. And if we run that, we can come back here and see that each of these are random and they're not changing. If I check off any of these boxes or if I change any of the cells , that's really cool. But how do we get that button here? Well, we can do it a couple of ways.
Let's do this. Insert image oversells. Actually, I'm gonna insert a drawing . I'm gonna go to the shapes and get this nice button looking one and call this roll all dice. Save and close. Now we have an image that says Roll all Dice. It looks like a button, but how do we get it? To run this roll five dice, I'm gonna copy the name function roll five dice, and up in the top right corner in three dots, I'm gonna assign script and I'm just gonna paste roll five dice, exactly the same name as that function . Click okay. And now every time I click this, I'm rolling all five dice. Let's insert another image , or sorry, another drawing . Let's create a new shape. Square, it's colored. A little different, maybe orange.
And we will call this roll one. Save and close. And now I can click and drag this over here and say, okay, I want to roll the first dice. So we're just gonna take this whole function . We're gonna copy it and paste it. We're gonna rename it. Roll one. And we're gonna take everything out. That's not the first one, so it's just gonna roll. The first one, it's called roll one. So let's go back. Click those three dots. Assign script paste in the name of the roll one. And now this dice will change every time I click this, but all five dice can roll here. So now we can see. All I have to do for the next four is. Insert an image or insert a drawing over the cells . Assign a script. We have to go and create that script.
We can easily create it by just copying this four more times and calling this role two and just deleting everything 3, 4, 5, and deleting one. So now it's only going to need the spreadsheet , app dock, active sheet , and random value two. And set value for two. I think that's. All you need, and I hope that helps you in creating Rolling Dice inside of your Google sheet. 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.