DEV Community

Cover image for How to check word count in Google Docs?
Nibesh Khadka
Nibesh Khadka

Posted on • Edited on • Originally published at kcl.hashnode.dev

How to check word count in Google Docs?

Are you tired of manually counting the number of words in your Google Docs while working on your blog or article? Well, fret not, as I have got you covered with a simple yet effective solution. In this tutorial series on Apps Script, I will walk you through a step-by-step guide on how to create a script that can read your Google Docs and return the exact word count of your document, making your life easier and your writing process more efficient. So, let's dive in and learn how to see how many words you have on Google Docs!

Welcome to the apps script tutorial series, where I, Nibesh Khadka, provide short and sweet scripts and teach a few things along the way to make our life less miserable.

Google Docs Word Counter Script

The given script will do just exactly what was promised earlier.

/**
 * Word Counter is A simple script to count number of words written in a google docs with apps script.
 */
function wordCounter() {
  let doc = DocumentApp.getActiveDocument();
  let data = doc.getText();

  let processedData = data.split("\n").filter(String).map(item => item.split(" ")).flat().filter(String);

  let wordCount = processedData.length;
  // now alet the word count
  DocumentApp.getUi().alert(`You have ${wordCount} words in this document.`)
}


/**
 * Menu creates menu UI in the document it's bound to.
 */
function createCustomMenu() {
  let menu = DocumentApp.getUi().createMenu("Count Number of Words"); // Or DocumentApp or SlidesApp or FormApp.

  menu.addItem("Word Counter", "wordCounter");
  menu.addToUi();
}


/**
 * OnOpen trigger that creates menu
 * @param {Dictionary} e
 */
function onOpen(e) {
  createCustomMenu();
}

/**
 * Word Counter is a script to count number of words written in Google Docs with Google Apps Script. 
 * It is created in such a way that it only works with bound script.
 * 
 * Created by: Nibesh Khadka.
 * linkedin: https://www.linkedin.com/in/nibesh-khadka/
 * website: https://nibeshkhadka.com
 */
Enter fullscreen mode Exit fullscreen mode

How to use Google Apps Script For Non-Coders?

This is a container-bound script i.e the script has to be attached to a file, in this case, a Google Docs. So, first, you'll have to open the document in which you'd want to count the number of words.

There just click the Extensions tab and then Apps Script as shown in the image below.

Open Script Editor

There, on code.gs file, just replace those codes with the codes above.

Now, similar to the previous blogs, you can now just:

  1. Save the code.

  2. Reload the document. Where you'll see the custom menu as shown below

  3. And execute the function.

Executing The Function

Here are a few images to guide what the operation will look like in your docs.

Word Counter Menu

Number Of Words In Google Docs

Thank You for Your Time

My name is Nibesh Khadka, and as a freelance automation expert, I specialize in automating Google products with Apps Script. So let's get started! If you need my services let me know.

Don’t forget to like and share this blog.

Like, Share and Follow Khadka's Coding Lounge

Top comments (0)