【问题标题】:How to use custom function ARRAYREPLACE() in a array formula? [Google Sheets]如何在数组公式中使用自定义函数 ARRAYREPLACE()? [谷歌表格]
【发布时间】:2021-03-26 13:22:44
【问题描述】:

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

Thiago的函数找到here

实际上,我正在使用他的函数将关键字替换为相应的超链接。

原始字符串存储在列Examples!A:A 中。 “keyword-replacement html”对存储在另外三个工作表中:“Verticals”、“Horizo​​ntals”和“Technologies”。

例如,在原始字符串中找到的每个“作者”实例都替换为

    <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 转换为有效的数组公式?

【问题讨论】:

  • 您能否澄清输入数据和所需的输出?请考虑提供一份您正在处理的电子表格的副本,不包含敏感信息,并清楚地说明这些信息。
  • 您提供的电子表格不是公开的。可以公开访问吗?此外,如果您这样做了,请留下评论说明您这样做了,以便人们注意到这一点。
  • 我公开了电子表格,现在可以在原始数据/公式之外对其进行编辑。
  • 嗨,我发布了an answer。您能否确认这是否解决了您的问题?

标签: google-apps-script google-sheets google-sheets-formula


【解决方案1】:

只需修改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,fromList,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 ) + ")$" , caseRule ), 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 ) + ")$" , caseRule ), 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)

【讨论】:

  • 谢谢@lamblichus!我尝试修改 ARRAYREPLACE(),但在尝试保存修改后的函数时收到错误消息:“Syntax error: SyntaxError: missing ) after argument list line: 92 file: Code.gs”。
  • @Tomas,您没有正确复制该函数(也没有在表格中正确调用它,至少在您提供的表格中)。我现在将使用完整的代码 sn-p 更新我的答案。
  • @Tomas return result 已经是您原始功能的一部分,不打算重复。请检查我更新答案的Full code 部分。此外,您在工作表中调用函数的方式不需要使用ARRAYFORMULA。我也将其添加到我的答案中(Formula 部分)。让我知道这是否适合您。
  • 非常感谢@Iamblichus!这行得通! :)
猜你喜欢
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2019-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
相关资源
最近更新 更多