wthman
wthman

Reputation: 97

Find and change unknown strings to UPPERCASE in Google Apps Script Document using JS

I write in Fountain markdown http://fountain.io/ in Google Docs. Fountain is used for writing screenplays. I want to make writing in fountain a little friendlier by auto-capitalizing certain elements (on open or with a button, whatever).

Here is a correctly formatted screenplay (in fountain):

EXT. GAS STATION - DAY

Susie steps out of her car and walks toward the station attendant.

SUSIE
Hey, Tommy.

TOMMY
Where you been, Sue?  Come on in.

They walk toward the station entrance together.

INT. GAS STATION - NIGHT

etc...

As you can see there is a ton of CAPS-LOCK and SHIFT business in screenwriting, and it gets tedious.

Which is why I want to write in lower-case (ie. int. gas station - day) and have javascript/GAS find that text and uppercase it. Same with when a character speaks:

susie
Hey, Tommy.

would become

SUSIE
Hey, Tommy.

Characters speaking always have an empty line above their name and text on the next line. And scene headings ALWAYS start with either EXT. or INT.

I have had some kind help so far on Stackoverflow, but I'm still struggling to get this to work at all. I was given a great regex string that finds character names but GAS has a limited regex implementation. That regex is [\n][\n]([^\n]+)[\n][^\n|\s]/gi. I have had no luck replacing text with regex. My JS skill is newborn baby, but I have completed CodeAcademy's beginner's JS course for what that's worth.

I would be grateful for any help in the right direction.

Upvotes: 5

Views: 25688

Answers (2)

NEERAJ SHARMA
NEERAJ SHARMA

Reputation: 1

var par2 = table.getCell(i,j).getChild(0).asParagraph();
     
      if(i>=1&&j>=3){
      var paragraphText = par2.asText().getText();
      var newexp = paragraphText.toUpperCase();
      table.getRow(i).getChild(j).asText().setText(newexp);
      }
    

THIS IS WHAT IS USED TO CAPITALISE AN ELEMENT OF A TABLE IN DOCS !!

Upvotes: 0

Mogsdad
Mogsdad

Reputation: 45740

To change the text in a Google Doc, you need to get the individual elements and operate on them. There's a fair bit of work to do, digging into the document, before the Money Shot:

paragraphText.toUpperCase();

The following script is part of a document add-on, source available in this gist, in changeCase.js.

Code.gs

/**
 * Scan Google doc, applying fountain syntax rules.
 * Caveat: this is a partial implementation.
 *
 * Supported:
 *  Character names ahead of speech.
 *
 * Not supported:
 *  Everything else. See http://fountain.io/syntax
 */
function fountainLite() {
  // Private helper function; find text length of paragraph
  function paragraphLen( par ) {
    return par.asText().getText().length;
  }

  var doc = DocumentApp.getActiveDocument();
  var paragraphs = doc.getBody().getParagraphs();
  var numParagraphs = paragraphs.length;

  // Scan document
  for (var i=0; i<numParagraphs; i++) {

    /*
    ** Character names are in UPPERCASE.
    ** Dialogue comes right after Character.
    */
    if (paragraphLen(paragraphs[i]) > 0) {
      // This paragraph has text. If the preceeding one was blank and the following
      // one has text, then this paragraph might be a character name.
      if ((i==0 || paragraphLen(paragraphs[i-1]) == 0) && (i < numParagraphs && paragraphLen(paragraphs[i+1]) > 0)) {
        var paragraphText = paragraphs[i].asText().getText();
        // If no power-user overrides, convert Character to UPPERCASE
        if (paragraphText.charAt(0) != '!' && paragraphText.charAt(0) != '@') {
          var convertedText = paragraphText.toUpperCase(); 
          var regexEscaped = paragraphText.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); // http://stackoverflow.com/a/3561711/1677912
          paragraphs[i].replaceText(regexEscaped, convertedText);
        }
      }
    }
  }
}

Upvotes: 6

Related Questions