使用javascript / jquery在类似excel的Formula-String中更改Cell-Reference

时间:2016-06-29 18:08:53

标签: jquery excel-formula

我正在寻找这个问题的解决方案: 我创建了一个基于jquery的电子表格,其中包含计算功能,效果很好。现在我想将给定的formlua复制到下一行并更新该公式中的单元格引用。 示例:row1中的公式= if(和(A1 = 0; B1 = 0); 0; A1 + B1) 该公式到row2的副本应为:if(和(A2 = 0; B2 = 0); 0; A2 + B2) 简单地替换数字将导致" if(和(A2 = 2; B2 = 2); 2; A2 + B2)"并且复制的公式不对。有人有想法吗? 非常感谢,来自Germay的Bernd

这是我目前的解决方案:

        function createNewFormula()
        {
            var rowNew = $('#copyto option:selected').attr('value');    //the targetRow
            var exp = /(?:\w[0-9]+)/g;                                  //searchPattern
            var formula = $('#formula2').val();                         //current formula
            var arrMatches = formula.match(exp);                        //array of Matches
            var startIndexOf=0;                                         //startposition in current formula
            $.each(arrMatches,function(key,val){
                var idx = formula.indexOf(val,startIndexOf);
                //set new startPositon in current formula
                startIndexOf = idx;
                //replace the number in new Cell-Reference
                var valNew = val.replace(/(?![A-Za-z])\d+/g,rowNew);
                //replace part of current formula with new Cell-Reference
                formula = formula.replace(formula.substring(idx,idx+val.length),valNew);
            });

            //output to an input-element for test
            $('#formulaNew').val(formula);

        }

第二个想法:

        //create a new string-method replaceAll
        String.prototype.replaceAll = function(find, replacewith) {
            var currentformula = this;
            return currentformula.split(find).join(replacewith);
        };            
        //create a function to check if a substring in formula is a letter
        //like A,B,C....
        function isLetter(formulapos) {
          return formulapos.length === 1 && formulapos.match(/[A-Za-z]/i);
        }
        //and use this in the following to replace the cell-references
        function createNewFormula()
        {
            var rowNew = $('#copyto option:selected').attr('value');    //the targetRow                
            var exp = /\d+/g;                                           //match all munbers
            var formula = $('#formula2').val();                         //current formula to copy
            var arrMatches = formula.match(exp);                        //array of matches
            var startIndexOf=0;                                         //startposition for indexOf-method
            $.each(arrMatches,function(key,val)
            {
                var idx = formula.indexOf(val,startIndexOf);            //set position
                var fsubstr = formula.charAt(idx-1);                    //prev. Pos.
                startIndexOf=idx;
                var isletter = isLetter(fsubstr);                       //is letter at prev. Pos.
                if(isLetter(fsubstr)!==null)
                {
                    //insert a placeholder on the numbers in current formula
                    formula = formula.replace(formula.substring(idx,idx+val.length),'#');
                }
            });
           //replace all placeholders with the new row-Number
           formula = formula.replaceAll('#',rowNew);
           $('#formulaNew').val(formula); 

        }

1 个答案:

答案 0 :(得分:0)

你可以改变一点正则表达式只改变单元格名称,例如:

var valNew = val.replace(/([A-Z])\d+/g,"$1"+rowNew);

当然,在某些情况下,如果你需要一个永不改变的细胞,这不会有帮助,但在这种情况下它应该有效。