Home > database >  Macro or script that will put a wordcount into a Google Docs table cell?
Macro or script that will put a wordcount into a Google Docs table cell?

Time:03-11

I hope this won't get voted down as I'm unsure of how to properly phrase this but what I'm looking to do is NOT in Google Sheets, but regular Google Docs wordprocessor. I'm using a text doc that has a bunch of tables and each table has a paragraph, two, three of text below it, not as part of the table itself. The table has columns with various info, but one of them is "words" and currently I have to manually highlight that section below, go up to the "Tools" find "Word Count" and then see what it is. Then if I edit the section below, I repeat that process. Some of these documents have dozens or even hundreds of these individual sections (table, with text below until the next table) so having some way to automatically update that field would be incredible.

Is there any way to have a cell of the table look at the text below and calculate the wordcount?

Each table has a H3 title above it, so my thought would be to somehow identify and count words that are between the end of the table and the next H3.

The structure is:

H3 level title
Table
Text
Text
Text

H3 level title
Table
Text
Text

etc.

Here's a link to a document showing what I'm hoping to do: link

There are a few small additional things to consider but just knowing if that's possible would be great.

I am running Lubuntu 20.04LTS, using Chrome as the browser, if that makes a difference.

Fingers crossed, and again, apologies for a question that may be vague or have insufficient information. Thank you for any help!

CodePudding user response:

Description

I've developed a script for your specific situation. The word count may not be accurate as this script only uses a space as a seperator between words. If there is a tab or some other character the count may be off. The subject of word count has come up before on stackoverflow without a fool proof counter.

Basically it skips the header, keeps track of the following table to fill in the word count then counts the words between the table and next header.

I've edited the script to omit from count if words are highlighted in yellow. But, something I observed if a word ends in a punctuation mark and it is not highlighted it counts as a word. So again you can see there are a lot of things to consider in word counting.

Fixed for case if paragraph starts with highlight.

Script

function countWords() {
  try {
    var doc = DocumentApp.getActiveDocument();
    var body = doc.getBody();
    var child = null;
    var i = 0;
    var j = 0;
    var count = 0;
    var text = null;
    var table = null;
    var length = 0;
    var startOffset = -1;
    var endOffset = -1;
    for( i=0; i<body.getNumChildren(); i   ) {
      child = body.getChild(i);
      if( child.getType() === DocumentApp.ElementType.PARAGRAPH ) {
        var atts = child.getAttributes();
        var heading = atts[DocumentApp.Attribute.HEADING].toString();
        if( heading.indexOf("HEADING") < 0 ) {
          length = child.asParagraph().getText().length;
          text = child.asParagraph().copy().editAsText();
          for( j=length-1; j>=0; j-- ) {  // have to delete from end first
            if( text.getBackgroundColor(j) == "#ffff00" ) {  // yellow
              if( endOffset < 0 ) {
                endOffset = j;
              }
              else {
                startOffset = j;
              }
            }
            else if( endOffset >= 0 ) {
              console.log("offset = " startOffset " " endOffset);
              text.deleteText(startOffset,endOffset);
              if( startOffset === 0 ) break; // if the paragraph starts with highlight
              startOffset = -1;
              endOffset = -1;
            }
          }
          text = text.getText().split(" ");
          count = count   text.length;
          if( text.length > 0 ) text.forEach( word => { if( word == '' ) count--; } ); // incase of double spaces
        }
        else {
          if( table ) table.asTable().getCell(1,5).setText(count);
          // start a new count
          count = 0;
        }
      }
      else if( child.getType() === DocumentApp.ElementType.TABLE ) {
        table = child;
      }
    }
    // fill in the last table
    table.asTable().getCell(1,5).setText(count);
  }
  catch(err) {
    console.log(err);
  }
}

Reference

  • Related