gpt4 book ai didi

javascript - 用于连续/嵌套正则表达式的 Google Sheets 脚本

转载 作者:行者123 更新时间:2023-11-28 04:14:56 33 4
gpt4 key购买 nike

我正在尝试在 Google 表格上重现嵌套的 Regex_MATCH 函数,但使用匹配表(以便我可以更轻松地调整/编辑)。它应该与数组一起使用,因为我将把它与 arrayformula 一起使用。

Test sheet link

例如:我想替换这样的公式:

=arrayformula(if(len(A2:A)>1,If(REGEXMATCH(A2:A, "[bB]lue[bB]alloon|Blue_Balloon"),"BlueBalloon_2017",
IF(REGEXMATCH(A2:A, "[hH]ydraphase"),"Hydraphase_2017",
IF(REGEXMATCH(A2:A, "[sS]kinchecker"),"Skinchecker_2017"))),""))

用一个简单的:

=arrayformula(if(len(A2:A)>1,regexTable(A2:A),"")

其中 regexTable 是自定义函数

// function loop RE_table named range and get matching value in result_table
function regexTable()
{
var s = SpreadsheetApp.getActiveSpreadsheet();
var refs = s.getSheetByName('refs') //get range in refs worksheet
var RE_table = refs.getRange("D2:D4").getvalues; // list of regular expressions to test
var result_table = refs.getRange("E2:E4").getvalues; // results to return if REGEX_MATCH = TRUE

var numRows = RE_table.getNumRows(); // loop through all regular expressions to test
for (var i = 1; i <= numRows; i++) { // I would like to exist loop as soon as REGEX_MATCH = TRUE
if(RE_table[i][1].test())
return result_table[i][1]
}
}

这是我第一次使用 Google 脚本编辑器,所以我的问题是:

  1. 什么不起作用?
  2. 如何才能尽可能快地运行?

非常感谢,来自瑞士的欢呼。

最佳答案

用法

作为工作表中的自定义函数:

=getRegexTable(matchArray, 正则表达式, 替换)

函数

粘贴到脚本编辑器中:

/**
* returnType - "row" or "column" (default)
*/
function getRegexTable(matchArray, regexs, replaces, returnType) {
// convert attays to lines
matchArray = convertArrayIntoLine(matchArray);
matchArray = convertArrayIntoLine(matchArray);
matchArray = convertArrayIntoLine(matchArray);

returnType = returnType || "column" // set dafault to rows
var machValue = '';
var result = [];
// for each machValue
for (var i = 0, ii = matchArray.length; i < ii; i++)
{
machValue = matchArray[i];
result.push(getRegexList(machValue, regexs, replaces));
}
if (returnType == "column") return convertLineIntoColumn(result);
return result;
}

function getRegexList(machValue, regexs, replaces, defaultResult)
{
var defaultResult = defaultResult || '';
var regex;
// for each regex
for (var i = 0, ii = regexs.length; i < ii; i++)
{
if(machValue.match(new RegExp(regexs[i]))) return replaces[i];
}
return defaultResult;
}

function convertArrayIntoLine(array)
{
if (!Array.isArray(array[0])) return array;
var row = [];
var result = [];
for (i = 0, ii = array.length; i < ii; i++)
{
row = array[i];
for (var j = 0, jj = row.length; j < jj; j++)
{
result.push(row[j]);
}
}
return result;
}

function convertLineIntoColumn(array)
{
var result = [];
for (var i = 0, ii = array.length; i < ii; i++)
{
result.push([array[i]]);
}
return result
}

测试

我用这段代码测试了它:

function test_getRegexTable()
{
var matchArray = [
'Hydraphase boo',
'bar Skinchecker',
'BlueBalloon foo',
'BlueBalloon foo',
'bar Skinchecker',
'boo'];

var regexs = [
'[bB]lue[bB]alloon|Blue_Balloon',
'[hH]ydraphase',
'[sS]kinchecker'];

var replaces = [
'BlueBalloon_2017',
'Hydraphase_2017',
'Skinchecker_2017']

Logger.log(getRegexTable(matchArray, regexs, replaces));
/* result:
[
[Hydraphase_2017],
[Skinchecker_2017],
[BlueBalloon_2017],
[BlueBalloon_2017],
[Skinchecker_2017],
[]]
*/
}


function test_convertArrayIntoLine()
{
var array = [1, 2, 3];
Logger.log(convertArrayIntoLine(array));
array = [[1,2], [3,4]];
Logger.log(convertArrayIntoLine(array));
}

function test_convertLineIntoColumn()
{
var array = [1, 2, 3];
Logger.log(convertLineIntoColumn(array));
}

关于javascript - 用于连续/嵌套正则表达式的 Google Sheets 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45941590/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com