How to check word count in Google Docs?

Written by kcl | Published 2023/04/25
Tech Story Tags: google-apps-script | google-apps-script-tutorial | google-docs | count-words-in-google-docs | productivity-tools | automation | coding | tutorial | web-monetization

TLDRCopy the script given to your script in editor. Save the script, and then reload the google docs that the script is attached to. You can now run the word counter function from the tabs.via the TL;DR App

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 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.j

/**
 * 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
 */

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.

On code.gs file, 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.

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.

Also published here.


Written by kcl | As a freelancer, I create addons and scripts to automate google products such as sheets, Gmail, docs with apps script.
Published by HackerNoon on 2023/04/25