Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google doc script to capitalize sentences

i am writing the google doc script below to capitalize the sentences in a document.

function cap6() {

   var body = DocumentApp.getActiveDocument().getBody();
   var text = body.editAsText();

   var str1 = text.getText();
   Logger.log(str1);

   // define function "replacement" to change the matched pattern to uppercase
   function replacement(match) { return match.toUpperCase(); }

   // period followed by any number of blank spaces (1,2,3, etc.)
   var reg = /\.(\s*\s)[a-z]/g;

   // capitalize sentence
   var str2 = str1.replace(reg, replacement);
   Logger.log(str2);

   // replace string str1 by string str2
   text.replaceText(str1, str2);

}

the code almost worked in the sense that the correct result is shown in the log file as follows:

[15-10-22 22:37:03:562 EDT] capitalize sentences.  this is one example with ONE blank space after the period.  here is another example with TWO blank spaces after the period.          this is yet another example with MORE THAN THREE blank spaces.

[15-10-22 22:37:03:562 EDT] capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

the 1st line above is the original paragraph without capitalized sentences; the 2nd line below is the transformed paragraph with capitalized sentences, regardless of the number of blank spaces after the period.

the problem was that i could not replace the original paragraph in the google doc with the transformed paragraph using the code:

   // replace string str1 by string str2
   text.replaceText(str1, str2);

i suspect that i made an error in the arguments of the method "replaceText".

any help to point out my errors would be appreciated. thank you.

like image 409
Luke V Avatar asked Aug 17 '26 13:08

Luke V


1 Answers

in a flash of inspiration, i ALMOST solved the problem using the following code:

text.replaceText(".*", str2);

my inspiration actually came from reading about the method "replaceText".

the above code worked when i had only ONE paragraph in the google doc.

but when i had two paragraphs in the google doc, then the code gave a duplicate of the document, i.e., a 2nd exact copy of the two paragraphs just below the original two paragraphs (with correct capitalization of the sentences, including the beginning of the 2nd paragraph, but not the beginning of the 1st paragraph).

when i had 3 paragraphs, then i had 3 copies of these 3 paragraphs, such as shown below:

capitalize sentences.  this is one example with ONE blank space after the period.  here is another example with TWO blank spaces after the period.          this is yet another example with MORE THAN THREE blank spaces.

capitalize sentences.  this is one example with ONE blank space after the period.  here is another example with TWO blank spaces after the period.          this is yet another example with MORE THAN THREE blank spaces.

capitalize sentences.  this is one example with ONE blank space after the period.  here is another example with TWO blank spaces after the period.          this is yet another example with MORE THAN THREE blank spaces.

then after running the script, i got 3 copies of these 3 paragraphs (with correct capitalization of the sentences, including the beginning of the 2nd and 3rd paragraphs), as shown below:

capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.







capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.







capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

Capitalize sentences.  This is one example with ONE blank space after the period.  Here is another example with TWO blank spaces after the period.          This is yet another example with MORE THAN THREE blank spaces.

so there is still something wrong in the new code... which almost worked if i could get rid of the extra copies of the document.

returning to the original code

text.replaceText(str1, str2);

i suspect that there was something wrong with using the variable "str1" in the 1st argument of method "replaceText". it is hoped some experts could explain the error in my original code.

like image 173
Luke V Avatar answered Aug 20 '26 04:08

Luke V



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!