我想创建一个脚本来大写google doc中的句子,但不会改变某些单词中的现有属性。例如,在谷歌文档中,会有几个段落,每个段落都有几个句子。在这样的谷歌文档中,会有超链接,粗体字,斜体字,带下划线的字等等。我希望所有这些属性保持不变;脚本应该只对句子进行大写,而不删除这些单词的现有属性。
下面是我试图打印属性的代码:
function cap12b() {
// define function "replacement" to change the matched pattern to uppercase
function replacement(match) { return match.toUpperCase(); }
// define regex "period, followed by zero or any number
// of blank spaces, followed by any lowercase character"
var regex1 = "(^|\.)(\s*)[a-z]";
var regex2 = /(^|\.)(\s*)[a-z]/;
Logger.log('regex1 -> %s, regex2 -> %s', regex1, regex2);
// var body = DocumentApp.getActiveDocument().getBody().editAsText();
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
Logger.log('doc -> %s', doc);
Logger.log('body -> %s', body);
var atBody = body.getAttributes();
Logger.log('atBody -> %s', atBody);
// get body to edit as text
// var body = body.editAsText();
var idBody = body.editAsText().getTextAttributeIndices();
Logger.log("idBody -> %s", idBody);
// get element text matching pattern "regex"
var foundElement = body.findText(regex1);
Logger.log('foundElement -> %s', foundElement);
// get attributes of foundElement
var atFE = foundElement.getElement().getAttributes();
Logger.log("atFE -> %s", atFE);
while (foundElement != null) {
var foundText = foundElement.getElement();
// get attributes of foundText
// var atFT = foundText.getAttributes();
// Logger.log("atFT -> %s", atFT);
// capitalize the character after the period
var str1 = foundText.getText();
var str2 = str1.replace(regex2, replacement);
foundText.setText(str2); // running, but removed attribute
// Find the next match
foundElement = body.findText(regex1, foundElement);
}
// try to set attributes in variable "atBody" to body
var body = body.setAttributes(atBody);
Logger.log("body -> %s", body);
return body;
}
该文件包含以下段落,粗体字,斜体字,下划线字:
大写句子。这是一个带有一个空格的例子 时期。这是另一个带有两个空格后的例子 期。这是超过三个的另一个例子 空白。但粗体 斜体强调[这个词 在我的谷歌文档中有下划线,但当前的文本区域没有 在文本被删除后删除了下划线字体]格式 更换。我如何保留这些属性?
运行脚本cap13a后,句子的大小写都是正确的,如上所示,但所有粗体,斜体,下划线属性都被删除了;下面是转换后的谷歌文档:
大写句子。这是一个后面有一个空格的例子 时期。这是另一个带有两个空格后的例子 期。这是超过三个的另一个例子 空白。但粗体斜体下划线格式是 文本被替换后删除。我如何保留这些 属性?
看到代码确实正确地将句子大写,无论空格的数量是多少,请参阅上面的文字"代码"下面:
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. but the **boldface** _italics_ underline [this word was underlined in my google doc, but the current text area does not have the underline font] formats were removed after the text was replaced. how do i keep these attributes ?
我获得了以下日志:
[15-10-24 13:51:35:055 EDT] regex1 - > (^ |。)(s *)[a-z],regex2 - > / (^| .)(\s*)[a-z]/ [15-10-24 13:51:35:128 EDT] doc - >文献 [15-10-24 13:51:35:129 EDT]身体 - > DocumentBodySection [15-10-24 13:51:35:130 EDT] atBody - > {FONT_SIZE = null,ITALIC = null, PAGE_WIDTH = 612.0,LINK_URL = null,UNDERLINE = null, BACKGROUND_COLOR = null,MARGIN_BOTTOM = 72.0,PAGE_HEIGHT = 792.0, MARGIN_RIGHT = 72.0,STRIKETHROUGH = null,MARGIN_LEFT = 72.0, FOREGROUND_COLOR = null,BOLD = null,FONT_FAMILY = null,MARGIN_TOP = 72.0} [15-10-24 13:51:35:131 EDT] idBody - > [0,232,240,241,248,249, 258] [15-10-24 13:51:35:132 EDT] foundElement - > RangeElement [15-10-24 13:51:35:133 EDT] atFE - > {FONT_SIZE = null,ITALIC = null, STRIKETHROUGH = null,FOREGROUND_COLOR = null,BOLD = null,LINK_URL = null, UNDERLINE = null,FONT_FAMILY = null,BACKGROUND_COLOR = null} [15-10-24 13:51:35:635 EDT]身体 - > DocumentBodySection
注意原始文档的属性不包含任何粗体,斜体,下划线字体,但只包含边距!??我不知道文档中保留了这些属性的位置,以及如何访问这些属性。
我在代码中做错了什么?如果有人可以指出我的错误,我会很感激。感谢。
请参阅我的帖子google doc script, capitalize sentences without removing other attributes,了解上述问题的背景。