Get Word Count of Google Sheet

Learn how to create a custom word count function in Google Sheets using Apps Script. This tutorial guides you through setting up a menu to easily count words in the current sheet or the entire document.

you're writing in sheets and you wanna get a word count. Let's do that. Let's go up to Extensions app script . First thing we're gonna do is create an on open function . What this does is it creates a little custom menu that we can use to click. And use a function instead of having to come in here all the time. So variable UI equals spreadsheet . App. Do get UI and ui. Create menu. We're gonna call this word count. That's gonna be the little text. Up next to the help menu , I'm gonna add item. Get word count. That's what the user sees. And then we're going to call it word count. We're gonna call this act of active sheet or current sheet , actually. And then we're gonna add one more item. Um, let's get the whole file. Word count, and we're gonna name this get word, count file. So we're gonna create two functions here. Function, word count. It's just gonna get the active sheet. So we need variable sheet equals spreadsheet, app, docket, active sheet.

And we're gonna get all of the values in that sheet. So sheet dot, get data range , get values , that's everything in there. And all of the text is gonna be variable. Text equals values , flat in that, flat join and join it with a space. And then our count is gonna be equals text trim, trim, any spaces out of there, split length. So that's slash slash s plus slash. Let's give it a little ui spreadsheet , app dot get ui, and for that we need, why we need that UI is UI alert, word count, colon plus count. Let's command S this and save it. We can also add our function here, get word count file, but if we're gonna come and fix that later, or write that later, right now, let's go back to our sheet.

Let's refresh it. We did forget one thing, which is at the end of our on open add to ui. Because if we don't do this, it doesn't ever come up. So now close it, refresh it again. And now next to our help menu . At the top we have word count. So let's get a word count of the current sheet . We will have to authorize the first time we run it. Here's word count 3 39. Let's see the word count of this page, and we can just go to each page and find the word count. 2 96 habits, and we see it's a different number each time. So let's see what the word count of the entire thing is. Let's go back to extensions app script and we're gonna go down to the bottom and write our function . Get word count for the entire file. This one, we need variable sheets equals spreadsheet , app dot get active spreadsheet , get sheets. That's getting every single sheet in the entire file.

We're gonna create a variable called all text and keep it blank for now and do sheets for each. We're gonna create a function sheet. Sheet is that variable in there that's gonna iterate through all of them. And so we're gonna get all the values by going to sheet dot, get data range , get values . And we're gonna take that all text and we're gonna add to it. Plus equals means we're gonna take whatever's there and add to it. Just string . Keep on adding a string values , do flat that join and join with a space in quotes. When you plus and have a quote at the end. Make sure that spells function there. And so for every single sheet, it's gonna iterate through each sheet. Get the data range. Get the values, add to that all text. And so our count is gonna be equals all.

Text the end trim for any errand spaces split . Add this s plus length. And again, we wanna put this ui UI alert here and just take this count and show us the word count and just say of file. Now we don't need to refresh it because this get word count. File is already here. Word count. Get. Whole file word counts. Let's run it. We have a UI not defined because we haven't put it here. Variable UI equals spreadsheet , app.gi ui. Let's save it again. Dismiss, and let's try to get that word count again. 8 96. There we go. That should be pretty darn close to getting the word count of the entire document there. If you want this app script , I'll put a link to it down below.

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.