123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- function isFormulaItem(ele){
- return ele.type == 'Element' || ele.type == 'ParamData'
- }
- //转换公式的参数
- // function transformArguments(children,curEle,eleMap){
- // let fcReg = /(FC\.\S+\()(.+)\)/;
- // let fcText = '';
- // for (let i = (children.length-1); i >= 0; i--) {
- // let ele = children[i];
- // if(ele.isOn === false){
- // //点击了开关关闭的,跳过
- // continue;
- // }
- // //console.log(ele.name)
- // let tmpArr = fcReg.exec(ele.template.ft);
- // fcText = tmpArr[1] + fcText;//fc.XX( 左括号部分
- // let argList = tmpArr[2].split(",");//括号里面参数部分#1,#2..
- // let isNestEle = false;//是否有过嵌套的元素了
- // let isNestEle2 = false;
- // //console.log(argList)
- // //console.log(text)
- // argList.forEach((argText,index)=>{
- // if(argText.indexOf('#')>-1){
- // //#动态参数
- // let argIndex = index;
- // for (let j = 0; j < ele.template.args.length; j++) {
- // if(ele.template.args[j].key == argText){
- // //找到相同的#编码
- // argIndex = j;
- // break;
- // }
- // }
- // let arg = ele.arguments[argIndex];
-
- // if(arg && isFormulaItem(arg)){
- // if(!isNestEle && !isNestEle2 && i != (children.length-1) && curEle.id == arg.id){
- // //不是第一个嵌套的公式,且和当前挂载的是同一个元素
- // //认为这个参数是之前公式计算的结果,不再写入元素
- // isNestEle = true;
- // }else{
- // if(arg.type == 'Element'){
- // eleMap[arg.tableElementKey] = {
- // id: arg.id,
- // name: arg.name,
- // tableElementKey: arg.tableElementKey,
- // type: "Element",
- // };
- // fcText += `E[${arg.tableElementKey}]`;
- // }else if(arg.type == 'ParamData'){
- // eleMap[arg.k] = {
- // id: arg.id,
- // name: arg.name,
- // v:arg.v,
- // k:arg.k,
- // type: "ParamData",
- // };
- // fcText += `WP[${arg.k}]`;
- // }
-
- // }
- // }else if(Array.isArray(arg)){
- // //ifsles方法会进来
- // arg.forEach((a)=>{
- // if(a && isFormulaItem(a)){
- // if(!isNestEle2 && i != (children.length-1) && curEle.id == a.id){
- // //不是第一个嵌套的公式,且和当前挂载的是同一个元素
- // //认为这个参数是之前公式计算的结果,不再写入元素
- // isNestEle2 = true
- // }else{
- // if(a.type == 'Element'){
- // eleMap[a.tableElementKey] = {
- // id: a.id,
- // name: a.name,
- // tableElementKey: a.tableElementKey,
- // type: "Element",
- // };
- // fcText += `E[${a.tableElementKey}]`;
- // }else if(a.type == 'ParamData'){
- // eleMap[a.k] = {
- // id: a.id,
- // name: a.name,
- // v:a.v,
- // k:a.k,
- // type: "ParamData",
- // };
- // fcText += `WP[${a.k}]`;
- // }
- // }
- // }else if(a && a.type == 'Operator'){
- // //在运算符前后加上空格
- // fcText += ' '+a.name+' ';
- // //console.log(fcText)
- // }else if(a && a.type){
- // fcText += a.name;
- // }else{
- // fcText += a?a:"''";
- // }
- // })
- // }else{
- // fcText += arg?arg:"''";
- // }
- // }else{
- // //已有参数
- // fcText += argText?argText:'';
- // }
- // if(index != argList.length-1){
- // fcText += ',';
- // }
- // })
- // fcText += ')';
- // }
- // return fcText;
- // }
- function transformArguments(children,curEle,eleMap){
- // debugger
- let fcReg = /(FC\.\S+\()(.+)\)/;
- let fcText = '';
- for (let i = (children.length-1); i >= 0; i--) {
- let ele = children[i];
- if(ele.isOn === false){
- //点击了开关关闭的,跳过
- continue;
- }
- if (ele.template != null && ele.template != undefined) {
- let tmpArr = fcReg.exec(ele.template.ft);
- fcText = tmpArr[1] + fcText;//fc.XX( 左括号部分
- let argList = tmpArr[2].split(",");//括号里面参数部分#1,#2..
- let isNestEle = false;//是否有过嵌套的元素了
- let isNestEle2 = false;
- //console.log(argList)
- //console.log(text)
- argList.forEach((argText,index)=>{
- if(argText.indexOf('#')>-1){
- //#动态参数
- let argIndex = index;
- for (let j = 0; j < ele.template.args.length; j++) {
- if(ele.template.args[j].key == argText){
- //找到相同的#编码
- argIndex = j;
- break;
- }
- }
- let arg = ele.arguments[argIndex];
-
- if(arg && isFormulaItem(arg)){
- if(!isNestEle && !isNestEle2 && i != (children.length-1) && curEle.id == arg.id){
- //不是第一个嵌套的公式,且和当前挂载的是同一个元素
- //认为这个参数是之前公式计算的结果,不再写入元素
- isNestEle = true;
- }else{
- // let atbkey=arg.tableElementKey.replace('_key',':key')
- if(arg.type == 'Element'){
- eleMap[arg.tableElementKey] = {
- id: arg.id,
- name: arg.name,
- tableElementKey: arg.tableElementKey,
- type: "Element",
- };
- fcText += `E[${arg.tableElementKey}]`;
- }else if(arg.type == 'ParamData'){
- eleMap[arg.k] = {
- id: arg.id,
- name: arg.name,
- v:arg.v,
- k:arg.k,
- type: "ParamData",
- };
- fcText += `WP[${arg.k}]`;
- }
-
- }
- }else if(Array.isArray(arg)){
- //ifsles方法会进来
- arg.forEach((a)=>{
- if(a && isFormulaItem(a)){
- if(!isNestEle2 && i != (children.length-1) && curEle.id == a.id){
- //不是第一个嵌套的公式,且和当前挂载的是同一个元素
- //认为这个参数是之前公式计算的结果,不再写入元素
- isNestEle2 = true
- }else{
- if(a.type == 'Element'){
- eleMap[a.tableElementKey] = {
- id: a.id,
- name: a.name,
- tableElementKey: a.tableElementKey,
- type: "Element",
- };
- fcText += `E[${a.tableElementKey}]`;
- }else if(a.type == 'ParamData'){
- eleMap[a.k] = {
- id: a.id,
- name: a.name,
- v:a.v,
- k:a.k,
- type: "ParamData",
- };
- fcText += `WP[${a.k}]`;
- }
- }
- }else if(a && a.type == 'Operator'){
- //在运算符前后加上空格
- fcText += ' '+a.name+' ';
- //console.log(fcText)
- }else if(a && a.type){
- fcText += a.name;
- }else{
- fcText += a?a:"''";
- }
- })
-
- }else{
- fcText += arg?arg:"''";
- }
-
- }else{
- //已有参数
- fcText += argText?argText:'';
- }
- if(index != argList.length-1){
- fcText += ',';
- }
- })
-
- fcText += ')';
-
- }
- console.log(fcText,'ele.name')
-
- }
- return fcText;
- }
- export const formulaArrayToString = (processFormula,resultFormula) => {
- let text = '';
- let eleMap = {};//元素字典,为了回显的时候查询信息
- let preIsOp=false;//前一个参数是否是四则运算(+-*/)
- processFormula.forEach((item,index) => {
- if(isFormulaItem(item)){
- //console.log(item)
- if(item.children.length <1){
- if(item.type == 'Element'){
- eleMap[item.tableElementKey] = {
- id: item.id,
- name: item.name,
- tableElementKey: item.tableElementKey,
- type: "Element",
- };
- text += `E[${item.tableElementKey}]`;
- }else if(item.type == 'ParamData'){
- eleMap[item.k] = {
- id: item.id,
- name: item.name,
- v:item.v,
- k:item.k,
- type: "ParamData",
- };
- text += `WP[${item.k}]`;
- }
-
- }else{
- text += (index>0&&!preIsOp?',':'')+transformArguments(item.children,item,eleMap);
- preIsOp=false;
- }
- }else if(item.type == 'Element'){
- text += item.template.ft;
- }else if(item.type == 'Operator'){
- //在运算符前后加上空格
- preIsOp=true;
- text += ' '+item.name+' ';
- //console.log(fcText)
- }else{
- text += item.name;
- }
- });
- if(resultFormula[0].children.length){
- //等号左侧部分
- let resText = transformArguments(resultFormula[0].children,resultFormula[0],eleMap);
- //等号左侧元素不需要,左侧的公式嵌套右侧所有结果
- let fcReg = /(FC\.\S+\()(.+)\)/;
- let leftArr=fcReg.exec(resText);
- //text = resText.replace(`E[${resultFormula[0].tableElementKey}]`,text);
- if(!!text&&!resText.includes(text)&&!!leftArr){
- let reg = leftArr[2].replace('OPTION,','');
- text = resText.replace(reg,text);
- }else{
- text = resText;
- }
-
-
- }
- console.log(text,'最终');
- //console.log(eleMap)
- return {text,eleMap};
- }
|