问题
Is there a method in Google Apps Scrips that returns the word count from a Google Document?
Lets say I'm writing a report that have a particular limit on word count. It's quite precise and it states exactly 1.8k - 2k words (yes and it's not just a single case, but many...)
In Microsoft Office Word there was a handy status bar at the bottom of the page which automatically updated the word count for me, so I tried to make one using Google Apps Scrips.
Writing a function that rips out whole text out from a current document and then calculates words again and again several times in a minute feels like a nonsense to me. It's completely inefficient and it makes CPU run for nothing but I couldn't find that function for the word count in Docs Reference.
Ctr+Shift+C opens a pop-up that contains it, which means that a function that returns total word count of a Google Document definitely exists...
But I can't find it! Sigh... I spent few hours digging through Google, but I simply cannot find it, please help!
回答1:
Wrote a little snippet that might help.
function myFunction() {
var text = DocumentApp.getActiveDocument().getBody().getText();
var words = text.match(/\S+/g);
Logger.log(words.length);
}
来源:https://stackoverflow.com/questions/33338667/function-for-word-count-in-google-docs-apps-script