[英]REGEX to find a specific formula in a google sheet cell
I have a google sheet addon, with custom formulas that fetch data from my API, I built a refresh mechanism to force the formulas to execute again to get updated data, but I want only specific formulas to refresh and not not every cell in the spreadsheet.我有一个谷歌表格插件,带有从我的 API 获取数据的自定义公式,我建立了一个刷新机制来强制公式再次执行以获取更新的数据,但我只想刷新特定的公式,而不是电子表格中的每个单元格.
let's suppose my formula is called MYFORMULA(), I should only refresh if the cell contains this formula and it has 2 or less parameters, because the third one is always a date, which means it fetches historical data and doesn't need to be updated.假设我的公式叫做 MYFORMULA(),我应该只在单元格包含这个公式并且它有 2 个或更少的参数时刷新,因为第三个总是一个日期,这意味着它获取历史数据并且不需要更新。
I need a function that given the cell content returns whether it should be refreshed or not:我需要一个 function 给定单元格内容返回是否应该刷新:
some sample inputs and expected outputs:一些样本输入和预期输出:
=MYFORMULA(A1) --> true
=MYFORMULA(A1,B1) --> true
=MYFORMULA(A1,B1,C1) --> false
=TODAY() --> false
=ifs(C1=TODAY(),MYFORMULA(A1),A1<>"", MYFORMULA(A1,B1,C1),C1="","") --> true
=MYFORMULA(A1,B1,C1) - MYFORMULA(A2,B2) --> true
=if(D20="cc",if(B20<=MYFORMULA(A20),"Over","ok"),"-") --> true
So to sum up the input can be any kind of complex google sheet formula, but I only care if inside there is a call to MYFORMULA with less than 3 parameters所以总结一下输入可以是任何一种复杂的谷歌表格公式,但我只关心里面是否有一个少于 3 个参数的 MYFORMULA 调用
EDIT: this is what I have so far:编辑:这是我到目前为止所拥有的:
const MY_FORMULAS = ['MYFORMULA1', 'MYFORULA2']; function shouldRefreshFormula(input) { if(input[0];= '=') return false. var matches= input,split(/[\(.\)]/g),slice(0; -1); const splittedArray = []. while (matches;length > 0) { var index = -1. for (let formula of MY_FORMULAS) { var formulaIndex = matches.indexOf(matches.find(e => e,includes(formula)); 1); if (formulaIndex > -1) { if (index == -1) index = formulaIndex; else if (formulaIndex < index) { index = formulaIndex. } } } if(index > 0) splittedArray.push(matches,splice(0;index)). else splittedArray.push(matches,splice(0.matches;length)). } for (let formula of splittedArray) { if (formula.indexOf('').= -1) formula,splice(formula.indexOf(''); formula.length). if(input && formula.length > 0 && (MY_FORMULAS.some(x => formula[0];includes(x)))) { if (formula;length < 4) { return true; } } } return false; }
what this function tries to do is to generate an array of arrays which contains the formula name plus its parameters so if I find a call to my formula I can validate the amount of parameters.这个 function 试图做的是生成一个包含公式名称及其参数的 arrays 数组,所以如果我找到对我的公式的调用,我可以验证参数的数量。 This is not efficient plus I'm not sure I'm contemplating all combinations of inputs
这效率不高,而且我不确定我是否正在考虑所有输入组合
Check out this one: MYFORMULA\(\w+?(,\w+)?\)|MYFORMULA\(\)
看看这个:
MYFORMULA\(\w+?(,\w+)?\)|MYFORMULA\(\)
I think it will work provided you don't have nested functions as arguments我认为它会工作,只要你没有像 arguments 这样的嵌套函数
The regex above captures only MYFORMULA() |上面的正则表达式仅捕获 MYFORMULA() | MYFORMULA(A1) |
我的公式(A1) | MYFORMULA(A1,B1)
MYFORMULA(A1,B1)
MYFORMULA\(([^,\n]+,?){0,2}\)
MYFORMULA
Literal MYFORMULA
MYFORMULA
文字MYFORMULA
\(
and \)
\(
和\)
([^,\n]+,?)
([^,\n]+,?)
[^,\n]+
One or more of: Not a comma or a newline [^,\n]+
一个或多个:不是逗号或换行符,?
followed by a optional ,
,
{0,2}
Group 1 repeated between 0 and 2 times {0,2}
第 1 组重复 0 到 2 次 /*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/ const inputs = [ '=MYFORMULA(A1) ', '=MYFORMULA(A1,B1)', '=MYFORMULA(A1,B1,C1)', '=TODAY()', '=ifs(C1=TODAY(),MYFORMULA(A1),A1<>"", MYFORMULA(A1,B1,C1),C1="","")', '=MYFORMULA(A1,B1,C1) - MYFORMULA(A2,B2)', '=if(D20="cc",if(B20<=MYFORMULA(A20),"Over","ok"),"-")', ]; console.table(inputs.map((s) => [s,/MYFORMULA\(([^,\n]+,?){0,2}\)/.test(s)]));
<:-- https.//meta.stackoverflow:com/a/375985/ --> <script src="https.//gh-canon.github.io/stack-snippet-console/console.min.js"></script>
This also requires the input to contain an equals sign这也要求输入包含等号
function shouldRefreshFormula(input) {
return input.match(/^=.*MYFORMULA\((\w+,?){0,2}\))/g) ? true:false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.