如何在 Google Apps 脚本中的多个括号实例中加粗特定文本?
How to embolden specific text within multiple instances of parentheses in Google Apps Script?
我一直在尝试编写 Google Apps 脚本来搜索文档并更改括号的所有实例(包括括号本身):
Hello (C)world, nice to (D)meet you, You are (G) my sunshine, my (Em)only sunshine.
为此:
Hello **(C)**world, nice to **(D)**meet you, You are **(G)** my sunshine, my **(Em)** only sunshine.
我是这样成功的:
function onOpen() {
DocumentApp.getUi()
.createMenu('Utilities')
.addItem('Bold Parentheses', 'replaceParentheses')
.addToUi();
};
function replaceParentheses() {
var body = DocumentApp.getActiveDocument().getBody();
var found = body.findText("\(.+?\)");
while (found) {
var elem = found.getElement();
if (found.isPartial()) {
var start = found.getStartOffset();
var end = found.getEndOffsetInclusive();
elem.setBold(start, end, true);
}
else {
elem.setBold(true);
}
found = body.findText("\(.+?\)", found);
}
};
但我想知道是否有更有效的方法来做到这一点。
我用它来加强括号中的和弦名称...因此,如果歌词中有调用和响应,并且某些歌词周围有括号 - 我将如何排除这些括号?换句话说,我怎样才能使一组变量始终像 (A,B,C,A#,B#,C#,Ab,Bb,Cb,Am,Bm,Cm,Adim7,Bb7add9, etc)
您的代码可以很好地加粗括号内的所有内容。但是,如果您想设置一个应该加粗的变量列表而忘记其余部分,您可以尝试创建和排列并将其插入正则表达式。
您可以像这样更改您的代码:
function replaceParentheses() {
var body = DocumentApp.getActiveDocument().getBody();
var targetVariables = ['A', 'B', 'C', 'A#']
var searchPattern = "\([" + targetVariables.join("|") + "].?\)"
var found = body.findText(searchPattern);
while (found) {
var elem = found.getElement();
if (found.isPartial()) {
var start = found.getStartOffset();
var end = found.getEndOffsetInclusive();
elem.setBold(start, end, true);
}
else {
elem.setBold(true);
}
found = body.findText(searchPattern, found);
}
};
考虑到现在您只匹配 targetVariables
内的值,例如:
Hello (Caa)world (C) (A#), nice to (D)meet you, You are (G) my sunshine, my (Em)only sunshine.
会被转换为:
Hello (Caa)world (C) (A#), nice to (D)meet you, You are (G) my sunshine, my (Em)only sunshine.
我一直在尝试编写 Google Apps 脚本来搜索文档并更改括号的所有实例(包括括号本身):
Hello (C)world, nice to (D)meet you, You are (G) my sunshine, my (Em)only sunshine.
为此:
Hello **(C)**world, nice to **(D)**meet you, You are **(G)** my sunshine, my **(Em)** only sunshine.
我是这样成功的:
function onOpen() {
DocumentApp.getUi()
.createMenu('Utilities')
.addItem('Bold Parentheses', 'replaceParentheses')
.addToUi();
};
function replaceParentheses() {
var body = DocumentApp.getActiveDocument().getBody();
var found = body.findText("\(.+?\)");
while (found) {
var elem = found.getElement();
if (found.isPartial()) {
var start = found.getStartOffset();
var end = found.getEndOffsetInclusive();
elem.setBold(start, end, true);
}
else {
elem.setBold(true);
}
found = body.findText("\(.+?\)", found);
}
};
但我想知道是否有更有效的方法来做到这一点。
我用它来加强括号中的和弦名称...因此,如果歌词中有调用和响应,并且某些歌词周围有括号 - 我将如何排除这些括号?换句话说,我怎样才能使一组变量始终像 (A,B,C,A#,B#,C#,Ab,Bb,Cb,Am,Bm,Cm,Adim7,Bb7add9, etc)
您的代码可以很好地加粗括号内的所有内容。但是,如果您想设置一个应该加粗的变量列表而忘记其余部分,您可以尝试创建和排列并将其插入正则表达式。
您可以像这样更改您的代码:
function replaceParentheses() {
var body = DocumentApp.getActiveDocument().getBody();
var targetVariables = ['A', 'B', 'C', 'A#']
var searchPattern = "\([" + targetVariables.join("|") + "].?\)"
var found = body.findText(searchPattern);
while (found) {
var elem = found.getElement();
if (found.isPartial()) {
var start = found.getStartOffset();
var end = found.getEndOffsetInclusive();
elem.setBold(start, end, true);
}
else {
elem.setBold(true);
}
found = body.findText(searchPattern, found);
}
};
考虑到现在您只匹配 targetVariables
内的值,例如:
Hello (Caa)world (C) (A#), nice to (D)meet you, You are (G) my sunshine, my (Em)only sunshine.
会被转换为:
Hello (Caa)world (C) (A#), nice to (D)meet you, You are (G) my sunshine, my (Em)only sunshine.