How to Protect Spreadsheet From Yourself
Learn how to protect your Google Sheets from accidental edits, including how to set permissions and use App Script to maintain important formulas.
So, you want to protect a sheet from being edited and specifically you want to protect it from yourself. So, when we go to protect a sheet, we usually think about protecting it from other people. We'll go down to this bottom arrow on the sheet. We'll say protect sheet and we can either pick the whole sheet or a range . Let's pick a range because we may want to update the assigne in column B or status in column C, but we do not want to change the start date . So, we'll say E2 colon E here. And that means we can actually edit the header if we need to. Maybe we want to change this to start at some point, but we just don't want anything in here to be edited. So, we'll set permissions. And we have two options. We have a radio button that allows us to show a warning when editing this range or restrict who can edit this range.
range . And we say restrict who can edit this range . Even if we select custom, we're still the only ones allowed. So, what I do actually is I show a warning when editing this range . This is if I want to protect it from me. So, I'll click done. And now I will close this and I'll show you what happens if I try to change a status. Oh, we didn't actually start that yet. Great. Everything's fine. If I go to the start date and I'm like, "Hey, this start date 's wrong. I want to change it." It'll give me a heads up. You're trying to edit part of the sheet that shouldn't be changed accidentally. And so, this alert will alert you that, hey, you probably shouldn't be editing this. I can click okay if I want to edit it, but I can also click cancel and not edit it.
I also have the option to not show this again for five minutes. But I'm gonna hit cancel and see this is not edited and even if I try to edit it, I have to click okay. So that's a little bit of protection from yourself. But I want to show you one more thing which if we have a formula and we know that this formula needs to stay the same even if we delete it or or edit it or something accidentally we don't want a message we just want the formula to be written back in there. So let me show you how to do that extensions appscript. We're going to create a function here called onedit. What this does is it will automatically happen every single time your sheet is edited. Meaning cell has its value changed. So like this H column which is
actually column number eight. We want to say hey if you edit this cell in column 8 just write back this formula . So what we need to do is we need to know what row we're on. Event range .get get row and we want to know what column we're on equals event. range .get column. Once we do that, we can say if column is equal to 8, meaning we're editing that eighth column, column H, two amperands a row is greater than one, meaning we're not editing the head. We're going to just write spreadsheet app.getactive spreadsheet get active sheet get range . We want the row we're on. We want the column to be the eighth
column and we only need one row and one column. Set value. Now this value is going to be very specific. We'll call it formula and we'll come up here and say variable formula equals and we'll put in quotes this. But check out this. This F2 and two here are the row. So we actually want to put the row number here not two. So what I will do is I will change these quotes to back ticks and I will change the two to dollar sign curly brackets row. Same with this two. dollar sign curly brackets row. So we're essentially what this is called is interpolating this row into this number here. So
whatever the row is, it'll add that number and get us the exact formula that should be. So let's save this. And I want to go here to where it's a 17. Delete. Then we see it is written back in there. We can delete the 16 here in row five. And it writes back that formula . Here's the whole function written out. I'll put this in the description down below. You're 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 going to be your next Google Sheet.