javascript 使用 JS 在 Google Apps 脚本文档中查找未知字符串并将其更改为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24847408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:27:05  来源:igfitidea点击:

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

javascriptregexgoogle-apps-script

提问by wthman

I write in Fountain markdownhttp://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).

我在 Google Docs中的Fountain markdownhttp://fountain.io/中写道。喷泉是用来写剧本的。我想通过自动大写某些元素(打开或使用按钮,无论如何)使喷泉中的写作更友好一些。

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:

这就是为什么我想用小写(即int. gas station - day)编写并让 javascript/GAS 找到该文本并将其大写。与角色说话时相同:

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.

说话的角色在他们的名字上方总是有一个空行,在下一行有文字。场景标题总是以 EXT 开头。或 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.

到目前为止,我在 Stackoverflow 上得到了一些帮助,但我仍在努力让它发挥作用。我得到了一个很好的正则表达式字符串,它可以找到字符名称,但 GAS 的正则表达式实现有限。该正则表达式是[\n][\n]([^\n]+)[\n][^\n|\s]/gi. 我没有运气用正则表达式替换文本。我的 JS 技能是新生婴儿,但我已经完成了 CodeAcademy 的初学者 JS 课程,这是值得的。

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

我将不胜感激任何在正确方向上的帮助。

回答by Mogsdad

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:

要更改 Google Doc 中的文本,您需要获取各个元素并对其进行操作。在 Money Shot 之前,还有很多工作要做,深入研究文档:

paragraphText.toUpperCase();

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

以下脚本是文档附加组件的一部分,此 gist 中的源可用,在changeCase.js.

Code.gs

代码.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);
        }
      }
    }
  }
}