I'm writing a Google Docs Add-on and would like to keep track of word count in the extension whenever user adds/deletes new word.
My initial approach is:
in Code.gs:
function getWordCount() {
var st = DocumentApp.getActiveDocument().getBody().getText().replace(/\n/g, ' ') + ' ';
st = st.split(/\s* \s*/).length;
return st - 1;
}
in Sidebar.html:
var docCount = 0;
function updateWordCount(data) {
if(data) {
docCount = data;
}
google.script.run.withSuccessHandler(updateWordCount).getWordCount();
}
But it seems very 90th... is there any way to push data from google apps script to client side? I know about Google Realtime API but it doesn't work in apps script for Google Docs.
Regards