Log Every Time a Google Sheet is Opened

Learn how to log every time a Google Sheet is opened using Google Apps Script. This tutorial covers creating a logging function that captures timestamps and user information.

If you have a spreadsheet you wanna open every single day and you actually wanna know that you open this every single day, maybe you want to log every time the sheet is open, we can do that. We can go to Extensions app script and write a bit of code once opened. Call this log, we'll go to function . We'll call this log today. We have to write a function called on open. This is a function that's going to run every single time the sheet is open. What we wanna do is get the sheet that we're gonna log on, so that's spreadsheet app dot get active, spread. Get sheet by name and we're gonna call it log. We're gonna sheet a pin row and we wanna write this in an array. We want a timestamp and maybe one to look at the day and the time. This is just to show you that we can log different types of data here.

either a day or a time, or a timestamp . So the timestamps, the simplest one, it's just new date, and you'll see these colors. It'll be blue and pink if it's a capital D. If it's not, it's wrong. There we go. Then we want to just get the day. So we'll say variable day equals. And now we are going to use the exact same thing, new date, but we're going to wrap it. We're gonna wrap it with utilities dot format date. We're gonna get the date, we're gonna get time zone . I'm just gonna get the time zone from the spreadsheet I, and we need a format formatting of the days. Let's just get a. A two digit day, but then also space and the whole name. We'll end that parentheses, and now we're gonna use the exact same code except the

format we're going to change to HH colon. Mm. We can test this out by closing our script and refreshing our sheet. I think we might have a mistake. Which is we need a capitalized LOG log. The name of the sheet, it has to be exactly the same as this log, or we'll get an error if you get an error . It'll be over here on executions here, and it'll say, status failed. And if you click on it sometimes it'll actually tell you the exact reason why it failed. It says time is not defined, so let's go back to our editor . Oh yeah, we need to change this variable to time. Save. And again, let's make sure our app script is saved. We'll close it and we'll refresh the sheet again, you can close the sheet and reopen it if you want, but also refreshing opens it and there's our timestamp our day. Okay, we need to fix this.

Let's go to Extensions app script . Anything we need, lowercase m's. Here. And we need e, e, E for the full day of the week. So we'll save all of that and refresh our sheet. And there it is, 27th Wednesday, 1237, and here's the full date and time stamped here. Pretty cool, right? I wanna show one more thing. So we can get the user possibly. You want to log who's opening this sheet. So we'll do variable user equals session dot get active user, get email, and we'll add user. Let's close our app script . Refresh. And this one has gotten me, and you might wanna check, sometimes permissions doesn't allow you to have the user email depending on your organization and how your permissions are set up. But this should be pretty cool to be able to log who is opening the sheet when and get their email address or whoever it is.

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.