Word counting by sections in Google Docs extensions

I’m working on a Google Docs extension and need help with a specific feature. I want to create something that can count words within each section of a document, where sections are divided by headings.

Basically, I need the add-on to go through the document, find all the heading elements, and then calculate how many words appear between each heading. For example, if I have Heading 1 followed by some paragraphs, then Heading 2 with more content, I want to know the exact word count for each of these sections separately.

Can this be implemented in a Google Docs add-on? I’m thinking of showing this data in a custom sidebar panel where users can see a breakdown like “Introduction: 150 words, Methods: 300 words” etc. Is this technically feasible with the current Google Apps Script API?

Totally doable with Google Apps Script. I built something similar for academic papers about six months ago and hit a few snags you should know about. The Document service covers most of what you need, but watch out for performance issues on docs over 20-30 pages if you recalculate everything each time. I ended up caching section boundaries and only recalculating when the document structure actually changed. Footnotes and comments will mess up your word counts if you don’t exclude them during parsing - that one caught me off guard. The sidebar’s easy with HtmlService, but throw in a loading indicator since processing takes a few seconds on bigger docs. Pro tip that saved me hours: always check that headings exist before counting sections. Documents without proper heading structure make your script fail silently.

I built something like this last year for a client who needed chapter word counts in their technical docs. Google Apps Script works great for this through the Document service. Basically, you iterate through document elements and track heading styles (HEADING1, HEADING2, etc.) as section breaks. Text extraction needs some care since Google Docs stores content as structured elements, not plain text. Biggest headache was nested headings - figuring out which level defines your sections. Word counting gets messy with tables, images, and other non-text stuff between headings. Sidebar’s easy with HtmlService. I made a simple interface that updates counts when users hit a button. Real-time updates kill performance on long docs though. Watch out for inconsistent heading styles - that’ll break everything. Lots of people use manual formatting instead of proper heading styles. I’d add fallback logic for bold text or specific font sizes.

Yeah, Google Apps Script can definitely do this. The API has everything for parsing documents and counting words by section.

But you’ll be writing tons of code for document parsing, UI, and data processing. Plus maintaining it every time Google updates their APIs.

I’ve built similar tools and found automation platforms work way better. Instead of coding everything, you set up workflows that auto-process your Google Docs, extract sections, count words, and export to spreadsheets or databases.

You get a complete solution without hundreds of lines of Apps Script. Trigger analysis when documents change, email reports, or connect with other tools.

I’ve seen teams save weeks of dev time this way. You also get error handling, logging, and easy changes when requirements shift.

Check out Latenode for document automation - it connects directly with Google Docs and handles the heavy lifting: https://latenode.com

Apps Script works for this, but it’ll crawl if you don’t optimize it. I built mine to process chunks instead of everything at once - much smoother. Watch out though - Google Docs gets weird with text boxes and drawings. They can sit between headings and mess up your section breaks. Merged table cells are another pain point since they count words differently than you’d think.