Auto Increment Invoice Numbers So The Sheet Remembers!

Learn how to automatically increment invoice numbers in Google Sheets using Apps Script, ensuring your sheet remembers the last used number even after changes.

Do you ever have the situation where you have an invoice number and every time you add one, let's say Carl here, you're increasing this invoice number , and if you keep this all here, it's fine, but if you. Turn your sheet around. You move things around. You might not remember what the last invoice number was and you might not remember what to increment it to. So how can we create a sheet that we'll remember? Well let me show you 'cause it's really fun and really exciting part of app Script that's available to you, which is a property inside of your sheet. There's properties we can save inside of the sheet. So let's open app script and start coding. We'll create. A function called Increment

invoice number , we'll also create a function called Create Invoice Number , and what we're gonna do is use the property service Property service dot. Now we have three options, document, script or user properties . User properties will be with us through all of our Google workspace, but our document properties will have the sheet itself, remember. So let's do that. Get document properties and we'll set up property. Setting Property is fairly simple. We need a key and a value. The key is sort of the code we're gonna use to say, we're gonna go to properties, say give a, we're gonna give you a code, what's that value back?

And then we can set that property or reset that property later to something else. And these are all strings. So there is going to be a little bit of an issue where we're gonna have to. Get a string . Change that to a number . Change that number by one, then save it as a string . So let's set that property first as invoice number and the string is going to be 1000. Now this property we can save variable first. Invoice number might be something different, but we'll we're gonna save it as 1000. We can take this variable here and delete the quotes, but we're going to need to set

this as a string so we got a two string , so that's saving the invoice number or creating it in the very first place. So we're gonna save this code, and now we're gonna create our invoice number . We're gonna click run. And there it's saved. So let's create a function GI invoice number . We're gonna take the exact same thing, GI properties, document properties , GI property, and here we just need to give it the key, which is increment. Which is invoice number actually. And let's logger log that and let's just look at it and see if this is correct. Let's get invoice number run. There it is 1000, but let's make sure we get it.

We change it to a number . So I think we're gonna do parse in. And this is gonna be by 10. Let's save and run again. Get 1000. That now that 0.0 means we are a number . Instead of function increment, let's get new invoice number . And what we're gonna do is essentially get current invoice number , show current invoice number plus one. And then save current invoice number plus one. So this invoice number plus one, let's get it first. So we're gonna use this parse int, we're gonna say invoice number equals say variable.

Now we're gonna show the user this so that we know what's the current invoice number . We will do spreadsheet , app dot, get ui. We'll do it in in alert. An alert is just gonna be a string and we're gonna say invoice number or actually new invoice number . Let's variable new invoice number equals invoice number plus one, and let's actually show that new invoice number . And then we're gonna save it. So we need to take this exact same saving property service dock, get document properties, set, property invoice number, and we're going to say new invoice number two, string and save.

So how do we get this new invoice number ? I am gonna get a custom function that's gonna have a menu for us that's at Better sheets.co/snippets. And what it is, it's an unopen and it says Create a menu invoice number menu, and we're gonna have this create invoice number . We will call this set up invoice number . We can get invoice number as well if we want, but we just want new invoice number . So let's say get new invoice number or next could be, so we'll save this. And we will refresh the sheet, meaning close the sheet and open it again.

And when that happens, next to the help menu up here has invoice number . So let, let's get next invoice number . We need to authorize this. Click continue. New invoice number 1001. Okay, so that's not these, but the one that we actually saved. And let's get a new one. Get next. Invoice number 1002. So every time we get a new invoice number , it is gonna be added one. So pretty cool, right? So now this spreadsheet remembers the invoice number , and we can take this and say, okay, that's the next one for Fred. Okay? If this is here now. No matter what, even if this all changes, goes away, gets deleted, the spreadsheet remembers our invoice number and it can get it for us.

Get next. Invoice number 1 0 4 without any saving of data inside of the spreadsheet . This is actually saved in properties, I think is pretty cool. And if you want this exact spreadsheet and you want this exact code. Go down below. In the description below. I've given you a link directly to it. You can copy this exact sheet, this exact code. Enjoy. 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.