gpt4 book ai didi

regex - 将句子大写的谷歌文档脚本

转载 作者:行者123 更新时间:2023-12-01 09:46:28 25 4
gpt4 key购买 nike

我正在编写下面的 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.

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

问题是我无法使用以下代码将 google 文档中的原始段落替换为转换后的段落:

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

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

如能帮助指出我的错误,我们将不胜感激。谢谢。

最佳答案

灵光一闪,我几乎用下面的代码解决了这个问题:

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

我的灵感实际上来自阅读有关 method "replaceText" 的文章.

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

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

当我有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);

我怀疑在方法“replaceText”的第一个参数中使用变量“str1”有问题。希望有高手能解释一下我原来代码中的错误。

关于regex - 将句子大写的谷歌文档脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33294140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com