How to Count Views of Google Sheets
Learn how to count the number of views on your Google Sheets using Google Apps Script. This tutorial walks you through the steps to implement a simple script that tracks viewer activity.
So you want to see how many viewers open the sheet, so we can do that with a little bit of app script I wanna put here. Total views and a number in C five on this about page, this is a free sheet that I give out, but also in your case, maybe you're in a domain, you have other users that are using your sheet. You wanna see how many times they open it, how many times they see it. There is one little trick to know. Go up to tools, activity, dashboard , and you can see your viewer trend here and you can see how many. People have opened it in the last year all time, all that kind of cool stuff. So in this case, we want to have it in the sheet. We wanna show here's how many views and the C five, we want it to count the first person to open it other than us and including us.
Maybe later on it'll say open add one to this. So from zero to one, one to two, two to three, I'm gonna show you how to do that Extensions app script . We have some other functions already here. This is the custom menu for this workout menu, but if you don't have any, you're gonna have to write the word function on open, and we're adding a function inside of this on open. We cannot have more than one in a sheet, so we just have to have multiple functions inside here. So I'm actually gonna go function count views, and I'm gonna have this be a function . So all I have to do is say count views and these two parentheses, and then here underneath it, outside of the curly brackets, I can write function, count
views, and then do these curly brackets. And now I can run something every time the sheet opens. Well, I want to get variable current count, and that's going to be equals spreadsheet app dot get active spreadsheet , get sheet by name, and the name is gonna be wherever the cell is that I want to get. So this is C five on the about page. So it's about. Correct spelling and then dot get range and C five. So double check that it is C five. If it is like this merged cells , it's always gonna be the top left cell as the address of the whole thing. So C five. And then I'm gonna add get value. Why get value is because I want to know what is the value inside of that cell.
Right now it's zero. And then. I wanna take that current count. I can do plus. Plus. What this means is that it just adds one to whatever the value is here. So current count plus, plus, and then I'm gonna take most of this same thing, spreadsheet , app, dot, get, act, the spreadsheet , dot get sheet by name, dot get range , and I'm gonna paste it again, and then I'm gonna add at the end set value, and I'm gonna set the value. As the current count. So what's happening is we're getting the current count, we're adding one to it. This plus plus just means add one, and then we're gonna put that current count back into the cell. So the cool thing is I can run this right now so I can run on open. Sorry, not on open. I can run count, view this function . I can run it and we can see the number one.
Let's run it again. And everything looks fine and it's two. See it's taking that number , adding one, and putting it back in. But let's see if it happens when we actually open the sheet. So let's close our app script and we're gonna refresh the sheet, which just means we're gonna close it and open it again. You can also physically close Chrome menu and open it again, and we can see if there's any errors. It says failed. Let's see why it says count views is not defined. Oh, because I, I wrote it. Count, view, not count views. So we can say count views and count views. These two have to be the exact same spelling and same capitalization. So let's try that again. Fun that we got a failure there. So let's refresh. And you saw the number went from two to three.
Let's just put that there. There we go. Fun times. That's how you count the views. Every time somebody opens this sheet, it's gonna run it. We can even be, let's see if it, let's see if it works on incognito tab. I don't think it will because we're not even getting the menu. So that is one downside of this, is that the script has to run in order for it to be counted. 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.