Function for word count in Google Docs Apps Script

不羁岁月 提交于 2019-12-12 17:05:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!