我正在生成财务分析,并为会计科目表生成动态公式。
示例
@1~Cº + @2~Cº
生成公式@4~Cº + @5~Cº+@6~Cº
生成公式依旧......
我正在计算文本框focusout calculate_year
。
function calculate_year(ThisObj) {
var obj;
//intiate vaiables
var year_formulaTxt = "",resultExp ="",lastPos ="",sequenceTypePosC="",sequenceTypePosP="",sequenceTypePosF="",COACode="",COAValue="",ratio_formulaTxt = "";
var ratio_formulaTxt = "",COACode_P="",COAValue_Pyear_formula = "",ratio_formula = "",year_formulaObj = "",ratio_formulaObj = "",year_formulaArr = "",ratio_formulaArr = "",FormulaResult="";
//match paterm for
var matchPattern = /[^\s()*/%+-/]+/g;
var tableId = $(ThisObj).closest('table').attr('id');
var CuurentDiv = $(ThisObj).closest('.program-column').index();
if (CuurentDiv < 2){ // to check if previous month added
CuurentDiv = CuurentDiv + 1 ;
}
var tableIdNext = $('.mCSB_container .program-column:eq('+CuurentDiv+') table').attr('id');
//get all textbox inside div
$('#'+tableId+' .financial_txt').each(function () {
//º
obj = $(this);
year_formulaObj = obj.find('input[type=text]');
ratio_formulaObj= obj.find('input[type=text]:eq(1)');
//calcualtion for only formula fields
if ($(year_formulaObj).attr('data-fieldtype') == "F") {
//get formula from custom field
year_formula = $(year_formulaObj).attr('year_formula');
if($.trim(year_formula) !=""){
//match formula with math's operator(Binary operator)
year_formulaArr = year_formula.match(matchPattern);
//break string @ º : working for single experssion using loop
// ----------------------For Year ---------------------------
for( var i=0; i< year_formulaArr.length; i++ ){
//sub string from '@' to 'º'
lastPos = year_formulaArr[i].substring(1, year_formulaArr[i].length - 1);
//all sequence type
sequenceTypePosC = lastPos.indexOf("C");
sequenceTypePosP = lastPos.indexOf("P");
sequenceTypePosF = lastPos.indexOf("F");//
if(sequenceTypePosC >= 0){
//console.log(lastPos);
//getting value of COACode From Formula
COACode = lastPos.substring(0, sequenceTypePosC - 1);
//getting value of COACode From Text box id
COAValue = $.trim($('#'+tableId+' #txt_year_formula'+COACode).val()) == "" ? 0 : $.trim($('#'+tableId+' #txt_year_formula'+COACode).val());
$('#'+tableId+' #txt_year_formula'+COACode).val(COAValue);
//work for field value
var tempRes = year_formula.substring(year_formula.indexOf("@"), year_formula.indexOf("º")+1);
year_formula = year_formula.replace(tempRes,COAValue);
tempRes = year_formula;
//replace rest of @ ,º with 0
tempRes = tempRes.replace(/\s*@[^º]+º\s*/g,parseFloat(0));
var result = parseFloat(mathEval(tempRes)).toFixed(3)|| 0;
$('#'+tableId+' #txt_year_formula'+$(this).attr('id')).val(mathEval(result) == "NaN" ?"0":mathEval(result));
}
}
}
}
});
}
function mathEval (exp) {
var reg = /(?:[a-z$_][a-z0-9$_]*)|(?:[;={}\[\]"'!&<>^\\?:])/ig,
valid = true;
// Detect valid JS identifier names and replace them
exp = exp.replace(reg, function ($0) {
// If the name is a direct member of Math, allow
if (Math.hasOwnProperty($0))
return "Math."+$0;
// Otherwise the expression is invalid
else
valid = false;
});
// Don't eval if our replace function flagged as invalid
if (!valid){
//console.log("Invalid arithmetic expression");
}
else{
try { return (eval(exp) == "Infinity" ? "0":eval(exp)); } catch (e) { };
}
}
String break然后生成计算是函数calculate_year
的最佳方法?
如您所见,我正在使用动态公式计算金额,问题是由于tempRes.replace(/\s*@[^º]+º\s*/g,parseFloat(0));
@8~Cº / @27~Cº
我怎么处理这个?抱歉英语
如果你检查你会看到公式。