如何在数组公式中使用自定义函数 ARRAYREPLACE()? [谷歌表格] 完整代码:公式:

问题描述

我正在使用 Thiago Mata's 自定义 Google 表格功能搜索替换字符串中的多个词。

找到蒂亚戈的函数here

实际上,我正在使用他的功能将关键字替换为其相应的超链接

原始字符串存储在列示例中!A:A。 “keyword-replacement html”对存储在其他三个工作表中:“Verticals”、“Horizo​​ntals”和“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

enter image description here

完整代码:

 
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)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...