谷歌文档脚本大写句子 [英] google doc script to capitalize sentences

查看:83
本文介绍了谷歌文档脚本大写句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在下面编写google doc脚本,以大写文档中的句子.

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.

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

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

我怀疑我在方法"replaceText"的参数中犯了一个错误.

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.

推荐答案

我结合了华盛顿瓜迪斯,然后从 Robin Gertenbach 转到以下工作脚本:

i combine the above answers from Washington Guedes and from Robin Gertenbach here that led to the following working script:

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文档中所有现有的格式,例如链接,粗体斜体,下划线.

on the other hand, the above script would wipe out all existing formatting such as links, boldface, italics, underline in a google doc.

所以我的下一个问题是如何修改脚本,使其在选定的(突出显示的)段落上运行,而不是在整个Google文档上运行,从而避免脚本清除现有格式.

so my next question would be how could i modify the script so it would run on a selected (highlighted) paragraph instead of the whole google doc to avoid the script to wipe out existing formatting.

这篇关于谷歌文档脚本大写句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆