谷歌文档脚本,以大写句子

时间:2015-10-23 02:55:00

标签: regex google-docs

我正在编写下面的google doc脚本来大写文档中的句子。

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);

}

代码几乎起作用,因为正确的结果显示在日志文件中,如下所示:

[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.

上面的第一行是没有大写句子的原始段落;下面的第二行是带有大写句子的变换段落,无论句点后的空格数是多少。

问题是我无法使用代码替换google doc中的原始段落:

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

我怀疑我在方法“replaceText”的参数中出错了。

任何帮助指出我的错误将不胜感激。谢谢。

4 个答案:

答案 0 :(得分:1)

在灵感的闪光中,我几乎使用以下代码解决了问题:

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

我的灵感实际上来自阅读method "replaceText"

当我在google doc中只有一个段落时,上面的代码有效。

但是当我在谷歌文档中有两个段落时,代码就给出了文档的副本,即原始两段正下方的两段的第二个精确副本(正确的句子大写,包括第2段的开头,但不是第1段的开头。)

当我有3个段落时,我有3个段落的3个副本,如下所示:

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.

然后在运行脚本之后,我得到了这3段的3份副本(正确的句子大写,包括第2段和第3段的开头),如下所示:

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.

所以新代码中仍有问题......如果我可以删除文档的额外副本,这几乎无效。

返回原始代码

text.replaceText(str1, str2);

我怀疑使用变量" str1"在方法的第一个参数" replaceText"。希望有些专家可以解释我原始代码中的错误。

答案 1 :(得分:1)

我将Washington GuedesRobin Gertenbach的上述答案结合在一起,这导致了以下工作脚本:

function cap6() {

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

   // define variable str1       
   var str1 = text.getText();

   // 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;
   // or replace \s*\s by \s+
   var reg = /\.(\s+)[a-z]/g;

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

   // replace the entire text by string str2
   text.setText(str2);

}

另一方面,上面的脚本会清除所有现有的格式,例如链接,粗体斜体,在google doc中加下划线。

所以我的下一个问题是如何修改脚本以便它可以在选定的(突出显示的)段落上运行而不是整个谷歌文档,以避免脚本消除现有的格式。

答案 2 :(得分:0)

您遇到的重复问题来自换行符which are not matched by the dot operator in RE2 (Googles Regular expression engine) if you don't include the s flag 因此,您有一些等于段落数的匹配。

您不需要使用资源密集型替换方法,但只能使用text.setText(str2);代替text.replaceText(".*", str2);

答案 3 :(得分:0)

更改脚本以便它只在选定的文本(例如段落)上运行,以避免擦除google doc中其他段落中的现有格式,我的灵感来自{{3中的代码}}。

我还改进了变量“reg”中的正则表达式,以便行或段落的开头也可以大写:

var reg = /(^|\.)(\s*)[a-z]/g;

下面是一个脚本,可以将选定文本中的句子大写(只需运行脚本cap7,调用脚本cap8):

function cap7() {

   // script to capitalize the beginning of a paragraph and the sentences within.
   // highlight a number of paragraphs, then run cap7, which calls cap8.

   // get the selected text inside a google doc
   var selection = DocumentApp.getActiveDocument().getSelection();

   if (selection) {
   var elements = selection.getRangeElements();
     for (var i = 0; i < elements.length; i++) {
       var element = elements[i];

       // Only modify elements that can be edited as text; skip images and other non-text elements.
       if (element.getElement().editAsText) {
         var text = element.getElement().editAsText();

         // capitalize the sentences inside the selected text
         cap8(text);
       }
     }
   }

}

function cap8(text) {

   // define variable str1
   var str1 = text.getText();
   // Logger.log(str1);

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

   // beginning of a line or period, followed by zero or more blank spaces
   var reg = /(^|\.)(\s*)[a-z]/g;

   // capitalize sentence; replace regular expression "reg" by the output of function "replacement"
   var str2 = str1.replace(reg, replacement);
   // Logger.log(str2);

   // replace whole text by str2
   text.setText(str2); // WORKING

   return text;

}

另见帖子Class Range中的问题。