批量查找并替换为条件大小写匹配和完全匹配 google app script,doc,sheet
bulk find and replace with a condition case match and exact match google app script,doc,sheet
提前致谢。
我有一个sheet,数据如下。
Find
Replace
condition
Page number ten
Page No. 10
match case
Page number 10
Page No. 10
match case
ms
Ms
exact match
我想用条件查找和替换文档中的文本。
下面的代码对我来说很好用,但无论何时。 Williams 替换为 WilliaMs.
function replMyText() {
var ss = SpreadsheetApp.openById('1-WblrS95VqsM5eRFkWGIHrOm_wGIPL3QnPyxN_j5cOo');
var sh = ss.getSheetByName('find and replace');
var doc = DocumentApp.getActiveDocument();
var rgtxt = doc.getBody();
var rgrep = sh.getRange('A2:B103');
var repA = rgrep.getValues().filter(r => r.every(c => c.toString()));
repA.forEach(e => rgtxt.replaceText(...e));
}
我们需要捕获一组正则表达式来正确捕获所有情况,而不仅仅是空格之间的单词。 (e.g. ms. “ms” ms, ms! ms? ms! ‘ms’)
但是由于 replaceText
不支持捕获组而 replace
支持,我们需要在 replaceText
.
中使用 replace
来派生它
编辑:
代码:
function replMyText() {
var ss = SpreadsheetApp.openById('1-WblrS95VqsM5eRFkWGIHrOm_wGIPL3QnPyxN_j5cOo');
var sh = ss.getSheetByName('find and replace');
var doc = DocumentApp.getActiveDocument();
var rgtxt = doc.getBody();
var rgrep = sh.getRange('A2:C103');
var repA = rgrep.getValues().filter(r => r.every(c => c.toString()));
// Search per paragraph so it won't produce exponential copies per paragraph
var paragraphs = rgtxt.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
repA.forEach(function(e){
if(e[2] == "match case") {
DocumentApp.getActiveDocument().getBody().replaceText(e[0], e[1]);
}
else if (e[2] == "exact match") {
// Only replace the string in between non [a-zA-Z0-9_]
// Capture string including 2 \W characters around it, then pad them around the replaced string
var replace = "(\W)" + e[0] + "(\W)";
var re = new RegExp(replace, "g");
var text = paragraphs[i].getText();
paragraphs[i].replaceText(".*", text.replace(re, "" + e[1] + ""));
// Replace the ones starting in a paragraph if present
replace = "^" + e[0] + "(\W)";
re = new RegExp(replace, "g");
text = paragraphs[i].getText();
paragraphs[i].replaceText(".*", text.replace(re, e[1] + ""));
}
});
}
}
最新更新测试:
最新更新输出:
参考文献:
- variable in regex
- capture group in regex
提前致谢。
我有一个sheet,数据如下。
Find | Replace | condition |
---|---|---|
Page number ten | Page No. 10 | match case |
Page number 10 | Page No. 10 | match case |
ms | Ms | exact match |
我想用条件查找和替换文档中的文本。 下面的代码对我来说很好用,但无论何时。 Williams 替换为 WilliaMs.
function replMyText() {
var ss = SpreadsheetApp.openById('1-WblrS95VqsM5eRFkWGIHrOm_wGIPL3QnPyxN_j5cOo');
var sh = ss.getSheetByName('find and replace');
var doc = DocumentApp.getActiveDocument();
var rgtxt = doc.getBody();
var rgrep = sh.getRange('A2:B103');
var repA = rgrep.getValues().filter(r => r.every(c => c.toString()));
repA.forEach(e => rgtxt.replaceText(...e));
}
我们需要捕获一组正则表达式来正确捕获所有情况,而不仅仅是空格之间的单词。 (e.g. ms. “ms” ms, ms! ms? ms! ‘ms’)
但是由于 replaceText
不支持捕获组而 replace
支持,我们需要在 replaceText
.
replace
来派生它
编辑:
代码:
function replMyText() {
var ss = SpreadsheetApp.openById('1-WblrS95VqsM5eRFkWGIHrOm_wGIPL3QnPyxN_j5cOo');
var sh = ss.getSheetByName('find and replace');
var doc = DocumentApp.getActiveDocument();
var rgtxt = doc.getBody();
var rgrep = sh.getRange('A2:C103');
var repA = rgrep.getValues().filter(r => r.every(c => c.toString()));
// Search per paragraph so it won't produce exponential copies per paragraph
var paragraphs = rgtxt.getParagraphs();
for (var i = 0; i < paragraphs.length; i++) {
repA.forEach(function(e){
if(e[2] == "match case") {
DocumentApp.getActiveDocument().getBody().replaceText(e[0], e[1]);
}
else if (e[2] == "exact match") {
// Only replace the string in between non [a-zA-Z0-9_]
// Capture string including 2 \W characters around it, then pad them around the replaced string
var replace = "(\W)" + e[0] + "(\W)";
var re = new RegExp(replace, "g");
var text = paragraphs[i].getText();
paragraphs[i].replaceText(".*", text.replace(re, "" + e[1] + ""));
// Replace the ones starting in a paragraph if present
replace = "^" + e[0] + "(\W)";
re = new RegExp(replace, "g");
text = paragraphs[i].getText();
paragraphs[i].replaceText(".*", text.replace(re, e[1] + ""));
}
});
}
}
最新更新测试:
最新更新输出:
参考文献:
- variable in regex
- capture group in regex