Is possible to use Google Script to get word count and characters count of a Google Doc?

45 views
Skip to first unread message

Fernando Falcao

unread,
Dec 29, 2019, 10:50:07 PM12/29/19
to Google Apps Script Community
Hello Everyone!

Do you know some way to use Google Script to get the word count and characters count of a Google Doc file?

I have hundreds of file that I need to read and collect both information to save in a Spreadsheet.

I tried to use .DocumentApp.getActiveDocument().getBody().getText() methods of a Doc and then used .length in order to get the characters count, but the number doesn't match the number given by Google Doc when I open the doc in Google Docs and  go to Tools >> Word count menu.

Is is possible that we don't have this object available in Google Script when working with DocumentApp service?

Thanks for your feedback guys!

Fernando

Clay Smith

unread,
Dec 29, 2019, 11:27:22 PM12/29/19
to Google Apps Script Community
Hey Fernando,
Once you have the string of content you can split it to an Array and then get the length of the array. Try something like:
function wordCt(string){
return string.split(" ").length;
}

Fernando Falcao

unread,
Dec 30, 2019, 9:27:37 AM12/30/19
to google-apps-sc...@googlegroups.com
Hi Clay!
Yes, I tried that approach but the result is not always correct, compared to Google Doc Word Count.

I suspect that Google still didn't exposed this method to Google Script, unfortunately...as we have in MS Word VBA, for instance.

Thanks for your feedback!
Fernando

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/b5c11e75-f49a-4cb3-b9de-05d2d49457fe%40googlegroups.com.

Clay Smith

unread,
Dec 30, 2019, 4:16:35 PM12/30/19
to Google Apps Script Community
The accuracy is likely due to extra spaces. If you filter out any null array elements you should get an accurate count. You may also need to account for new lunes and/or paragraphs.

function wordCt(string){
string.split(" ");
var filtered= string.filter(function (element) {
return element != null;
});
return filtered.length
}

Fernando Falcao

unread,
Dec 30, 2019, 7:18:36 PM12/30/19
to google-apps-sc...@googlegroups.com
Hi Clay,

thanks again for your help. Yes, now I could increase my accuracy in the results!! 

By the way, did you already have experience on converting .docx file to Google Docs format by using Google Script?
I found some samples in the StackOverflow but they are not working...especially when creating the BLOB file.

I need to convert hundreds of .docx files hosted in Google Drive in order to run this word count script that I am writing.

Thanks a lot, Clay!
Fernando

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

Clay Smith

unread,
Dec 30, 2019, 8:20:45 PM12/30/19
to Google Apps Script Community
Do you mean the below? This is using the Advanced Drive Service. They need to be turned on to work. https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services

function convertDocx(id){
var docx = DriveApp.getFileById(id);
var newDoc = Drive.newFile();
var blob =docx.getBlob();
var file=Drive.Files.insert(newDoc,blob,{convert:true});
DocumentApp.openById(file.id).setName(docx.getName());
}

Fernando Falcao

unread,
Dec 31, 2019, 6:37:02 PM12/31/19
to google-apps-sc...@googlegroups.com
Hi Clay!

Yes. That's the code you mentioned that I am trying to run...I copied your code and it continues to give the message "We're sorry, a server error occurred. Please wait a bit and try again. (linha 238, arquivo "server" 

I already enabled the advanced service for Drive API and nothing changed. When you run this code in your side, does it work correctly?

Let me know your answer in 2020!! Have a great 2020!!

Thanks a lot for your help!!
Fernando





--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages