IAmMoonie

Google Doc Word Count + Update Sheet

Jan 9th, 2023
1,443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.50 KB | Source Code | 0 0
  1. /* A variable that stores the ID of the Google Document. */
  2. const DOCUMENT_ID = "YourDocIDGoesHere";
  3.  
  4. /* The ID of the Google Sheet that you want to write the word count to. */
  5. const SHEET_ID = "YourSheetIDGoesHere";
  6.  
  7. /**
  8.  * It counts the words in a Google Document and writes the word count to a Google Sheet.
  9.  */
  10. function countWords() {
  11.   /* Opening the Google Document with the ID of DOCUMENT_ID. */
  12.   const document = DocumentApp.openById(DOCUMENT_ID);
  13.   /* Setting the word count to 0. */
  14.   let wordCount = 0;
  15.  
  16.   /* Config for the headings and titles in the document to be excluded them from the word count.
  17.    * This is the same functionality that Google Docs uses for wordcount */
  18.   const searchType = DocumentApp.ElementType.PARAGRAPH;
  19.   const searchHeadings = [
  20.     DocumentApp.ParagraphHeading.HEADING1,
  21.     DocumentApp.ParagraphHeading.HEADING2,
  22.     DocumentApp.ParagraphHeading.HEADING3,
  23.     DocumentApp.ParagraphHeading.HEADING4,
  24.     DocumentApp.ParagraphHeading.HEADING5,
  25.     DocumentApp.ParagraphHeading.HEADING6,
  26.     DocumentApp.ParagraphHeading.TITLE
  27.   ];
  28.   let searchResult = null;
  29.  
  30.  
  31.   /* Searching for the headings and titles in the document and excluding them from the word count. */
  32.   while (
  33.     (searchResult = document.getBody().findElement(searchType, searchResult))
  34.   ) {
  35.     const par = searchResult.getElement().asParagraph();
  36.     if (searchHeadings.includes(par.getHeading())) {
  37.       /* Subtracting the number of words in the heading from the word count. */
  38.       wordCount -= par.getText().split(" ").length;
  39.     }
  40.   }
  41.  
  42.  
  43.   /* Get all the paragraphs in the document. */
  44.   const paragraphs = document.getBody().getParagraphs();
  45.  
  46.  
  47.   /* Loops through all the paragraphs in the document and adds the word count of each paragraph to
  48.   the total word count. */
  49.   for (const paragraph of paragraphs) {
  50.     /* Getting the text of the paragraph. */
  51.     const paragraphText = paragraph.getText();
  52.  
  53.     /* It splits the paragraph text into an array of words and then counts the number of words in the
  54.     array. */
  55.     wordCount += paragraphText.split(" ").length;
  56.   }
  57.  
  58.   /* It opens the Google Sheet with the ID of SHEET_ID. */
  59.   const sheet = SpreadsheetApp.openById(SHEET_ID);
  60.  
  61.   /* Writing the word count to the first cell of the first sheet in the Google Sheet.
  62.    * you can change this to place it whever you need to place the wordcount*/
  63.   const firstSheet = sheet.getSheets()[0];
  64.   const firstCell = firstSheet.getRange("A1");
  65.   firstCell.setValue(wordCount);
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment