Google Docs Apps脚本中的字数统计功能 [英] Function for word count in Google Docs Apps Script
问题描述
Google Apps Scrips中是否有一种方法可以从Google文档中返回字数?
Is there a method in Google Apps Scrips that returns the word count from a Google Document?
可以说我正在写一份报告,该报告对字数有特定的限制.它非常精确,并且准确地说出了1.8k-2k个单词(是的,不仅是一个案例,而且还有很多……)
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...)
在Microsoft Office Word中,页面底部有一个方便的状态栏,该栏自动为我更新了字数,因此我尝试使用Google Apps Scrips来制作一个.
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.
编写一个函数,该函数从当前文档中提取出整个文本,然后在一分钟内一次又一次地反复计算单词,这对我来说简直是胡说八道.它是完全低效的,它使CPU一无所有,但我在文档参考中找不到该功能来计算字数.
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打开一个包含它的弹出窗口,这意味着肯定存在返回Google文档总字数的函数...
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...
但是我找不到它! igh ...我花了几个小时浏览Google,但我找不到它,请帮忙!
But I can't find it! Sigh... I spent few hours digging through Google, but I simply cannot find it, please help!
推荐答案
写了一些可能有帮助的代码段.
Wrote a little snippet that might help.
function myFunction() {
var space = " ";
var text = DocumentApp.getActiveDocument().getBody().getText();
var words = text.replace(/\s+/g, space).split(space);
Logger.log(words.length);
}
这篇关于Google Docs Apps脚本中的字数统计功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!