简体   繁体   English

正则表达式在谷歌表格单元格中查找特定公式

[英]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
  • Borders \( and \)边框\(\)
  • Group 1: ([^,\n]+,?)第 1 组: ([^,\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.

相关问题 使用匹配任何数值和特定字符串的正则表达式根据单元格值过滤谷歌表 - filter google sheet based on cell value using a regex expression that match any numeric values and a specific string Google表格脚本可为特定单元格添加时间 - Google Sheet Script To Add Time To Specific Cell Google Sheet - 在修改特定工作表上的任何单元格时添加时间戳 - Google Sheet - Add Timestamp When Any Cell on Specific Sheet is Modified 如何获取单元格值(来自 Google 表单的 Sheet1,来自公式的 Sheet2)到谷歌文档模板 - How to get the cell values (Sheet1 from Google Form, Sheet2 from Formula) to google doc template 在Google表格中找到字符串“用户输入”,并显示在其中找到的单元格 - Find string “user Input” in Google Sheet and display the cell it is found in 查找与工作表单元格中的值匹配的 Google 表格并执行一段代码 - Find Google Sheets that matches a value in a cell of a sheet and execute a piece of code Google Apps Script setFormula 如何将公式设置为包含变量的表格单元格 - How can Google Apps Script setFormula set a formula to sheet cell containing variables 谷歌表脚本中的正则表达式 - Regex in Google sheet script 单元格上的jquery.sheet触发公式计算 - jquery.sheet trigger formula calculation on cell 在工作表之间传输特定的单元格数据 - Transferring specific cell data from sheet to sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM