Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The ID of the Google Sheet that you want to write the word count to. */
- const SHEET_ID = "SheetIDGoesHere";
- /**
- * It gets the URLs of Google Docs from column A of the first sheet in a Google Sheet, then it gets the
- * word count of each Google Doc and writes it to column B of the same row as the URL.
- */
- function countWords() {
- /* Get the Google Sheet. */
- const sheet = SpreadsheetApp.openById(SHEET_ID);
- /* It gets the first sheet in the Google Sheet and gets the values of column A, filtering out the
- empty rows. */
- const firstSheet = sheet.getSheets()[0];
- const urlValues = firstSheet
- .getRange("A2:A")
- .getValues()
- .filter((row) => row[0]);
- /* Converting the 2D array `urlValues` into a 1D array `urls`. */
- const urls = [].concat(...urlValues);
- let wordCounts = [];
- /* Getting the URLs of Google Docs from column A of the first sheet in a Google Sheet, then it gets
- the word count of each Google Doc and writes it to column B of the same row as the URL. */
- for (const url of urls) {
- /* Getting the document ID from the URL. */
- const documentId = url.match(/[-\w]{25,}/);
- /* Opening the Google Doc with the ID `documentId`. */
- const document = DocumentApp.openById(documentId);
- /* Initializing the variable `wordCount` to 0. */
- let wordCount = 0;
- /* Config for the headings and titles in the document to be excluded them from the word count.
- This is the same functionality that Google Docs uses for wordcount */
- const searchType = DocumentApp.ElementType.PARAGRAPH;
- const searchHeadings = [
- DocumentApp.ParagraphHeading.HEADING1,
- DocumentApp.ParagraphHeading.HEADING2,
- DocumentApp.ParagraphHeading.HEADING3,
- DocumentApp.ParagraphHeading.HEADING4,
- DocumentApp.ParagraphHeading.HEADING5,
- DocumentApp.ParagraphHeading.HEADING6,
- DocumentApp.ParagraphHeading.TITLE
- ];
- let searchResult = null;
- /* Getting the headings and titles in the document to be excluded them from the word count.
- This is the same functionality that Google Docs uses for wordcount. */
- while (
- (searchResult = document.getBody().findElement(searchType, searchResult))
- ) {
- const par = searchResult.getElement().asParagraph();
- if (searchHeadings.includes(par.getHeading())) {
- /* Subtracting the word count of the heading from the total word count. */
- wordCount -= par.getText().split(" ").length;
- }
- }
- /* It gets the paragraphs in the document. */
- const paragraphs = document.getBody().getParagraphs();
- /* Getting an individual paragraph from all the paragraphs and counting its words. */
- for (const paragraph of paragraphs) {
- /* It gets the text of the paragraph. */
- const paragraphText = paragraph.getText();
- /* Getting the word count of the paragraph and adding it to the total word count. */
- wordCount += paragraphText.split(" ").length;
- }
- /* Pushing the word count of the Google Doc to the array `wordCounts`. */
- wordCounts.push([wordCount]);
- }
- /* Writing the word count to Column B of the same row as the URL. */
- firstSheet
- .getRange(`B2:B${(wordCounts.length + 1).toString()}`)
- .setValues(wordCounts);
- }
Advertisement
Comments
-
- Just want to say thanks! I've been wanting to use a script like this for a while but didn't know how to do it. I found this via your reddit post. It works great!
Add Comment
Please, Sign In to add comment