google docs api word count
function countWords() {
var s = DocumentApp.getActiveDocument().getBody().getText();
//this function kept returning "1" when the doc was blank
//so this is how I stopped having it return 1.
if (s.length === 0)
return 0;
//A simple \n replacement didn't work, neither did \s not sure why
s = s.replace(/\r\n|\r|\n/g, " ");
//In cases where you have "...last word.First word..."
//it doesn't count the two words around the period.
//so I replace all punctuation with a space
var punctuationless = s.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()"?“”]/g," ");
//Finally, trim it down to single spaces (not sure this even matters)
var finalString = punctuationless.replace(/\s{2,}/g," ");
//Actually count it
var count = finalString.trim().split(/\s+/).length;
return count;
}
Also in JavaScript:
- how to use javascript to get full file path
- how to use await to console
- javascript fs write file with folder
- pdf javascript search text
- node js with express medium
- javascript remove last element from array
- too many open files react native
- how to access router from the store vue
- javascript time execution
- mongodb replace document
- setting property to method in child class javascript
- https request node.js output incomplete
- convert 24 hour to 12 hour moment js
- javascript onsubmit change input value
- objects in javascript
- return value from javascript function
- js output html
- angular [routerlink]
- .includes javascript
- gatsby-plugin-create-client-paths
- js remove item array
- jquery remove all alerts
- jest mock call
- Sets can be used to store __________. in js