问题描述
我正在使用 Thiago Mata's 自定义 Google 表格功能来搜索和替换字符串中的多个词。
原始字符串存储在列示例中!A:A。 “keyword-replacement html”对存储在其他三个工作表中:“Verticals”、“Horizontals”和“Technologies”。
例如,在原始字符串中找到的每个“Authors”实例都替换为
<a target="_blank" href="https://www.flauntmydesign.com/authors" title="Click for more business examples targeting authors">Authors</a>
Here is a Google Sheet showing what I'm trying to do (可在原始数据/公式之外编辑)
当我将函数应用于单个单元格时,此关键字替换效果很好。当我手动将公式向下拖动到列时,它也很有效。
问题是,如何将列示例!B:B 转换为有效的数组公式?
解决方法
只需修改 ARRAYREPLACE
使其接收一系列单元格而不是单个单元格。然后,遍历数组(例如,使用 map),对于每一行,使用与您之前的函数对应的代码:
function ARRAYREPLACE(dataInput,fromList,toList){
return dataInput.map(row => {
const input = row[0];
// ... YOUR PREVIOUS FUNCTION
return result;
})
}
然后,在B2
中,不是将输入范围设置为A2
,而是将其设置为A2:A10
:
完整代码:
function ARRAYREPLACE(dataInput,toList,caseSensitive){
/* solution from Iamblichus */
return dataInput.map(row => {
const input = row[0];
/* default behavior it is not case sensitive */
if( caseSensitive == undefined ){
caseSensitive = false;
}
/* if the from list it is not a list,become a list */
if( typeof fromList != "object" ) {
fromList = [ fromList ];
}
/* if the to list it is not a list,become a list */
if( typeof toList != "object" ) {
toList = [ toList ];
}
/* force the input be a string */
var result = input.toString();
/* iterates using the max size */
var bigger = Math.max( fromList.length,toList.length) ;
/* defines the words separators */
var arrWordSeparator = [ ".",",";",":"," " ];
/* interate into the lists */
for(var i = 0; i < bigger; i++ ) {
/* get the word that should be replaced */
var fromValue = fromList[ ( i % ( fromList.length ) ) ]
/* get the new word that should replace */
var toValue = toList[ ( i % ( toList.length ) ) ]
/* do not replace undefined */
if ( fromValue == undefined ) {
continue;
}
if ( toValue == undefined ) {
toValue = "";
}
/* apply case sensitive rule */
var caseRule = "g";
if( !caseSensitive ) {
/* make the regex case insensitive */
caseRule = "gi";
}
/* for each end word char,make the replacement and update the result */
for ( var j = 0; j < arrWordSeparator.length; j++ ) {
/* from value being the first word of the string */
result = result.replace( new RegExp( "^(" + preg_quote( fromValue + arrWordSeparator[ j ] ) + ")",caseRule ),toValue + arrWordSeparator[ j ] );
/* from value being the last word of the string */
result = result.replace( new RegExp( "(" + preg_quote( arrWordSeparator[ j ] + fromValue ) + ")$",arrWordSeparator[ j ] + toValue );
/* from value in the middle of the string between two word separators */
for ( var k = 0; k < arrWordSeparator.length; k++ ) {
result = result.replace(
new RegExp(
"(" + preg_quote( arrWordSeparator[ j ] + fromValue + arrWordSeparator[ k ] ) + ")",caseRule
),/* need to keep the same word separators */
arrWordSeparator[ j ] + toValue + arrWordSeparator[ k ]
);
}
}
/* from value it is the only thing in the string */
result = result.replace( new RegExp( "^(" + preg_quote( fromValue ) + ")$",toValue );
}
/* return the new result */
return result;
})
}
然后这样称呼它:
公式:
=arrayreplace(arrayreplace(arrayreplace($A2:A10,Verticals!$A:$A,Verticals!$B:$B),Horizontals!$A:$A,Horizontals!$B:$B),Technologies!$A:$A,Technologies!$B:$B)