Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
fix(formula update fix): formula update only one level, when cell val…
Browse files Browse the repository at this point in the history
…ue change. Fix it
  • Loading branch information
DR-Univer committed Dec 1, 2020
1 parent e96d210 commit cf6c5be
Showing 1 changed file with 67 additions and 27 deletions.
94 changes: 67 additions & 27 deletions src/global/formula.js
Original file line number Diff line number Diff line change
Expand Up @@ -5005,21 +5005,21 @@ const luckysheetformula = {
}

//把修改涉及的单元格存储为对象
let updateValueOject = {}, updateValueArray = [];
let updateValueOjects = {}, updateValueArray = [];
if (_this.execFunctionExist == null) {
let key = "r" + origin_r + "c" + origin_c + "i" + index;
updateValueOject[key] = 1;
updateValueOjects[key] = 1;
}
else{
for (let x = 0; x < _this.execFunctionExist.length; x++) {
let cell = _this.execFunctionExist[x];
let key = "r" + cell.r + "c" + cell.c + "i" + cell.i;
updateValueOject[key] = 1;
updateValueOjects[key] = 1;
}
}

let arrayMatchCache = {};
let arrayMatch = function(formulaArray, formulaObjects, func){
let arrayMatch = function(formulaArray, formulaObjects, updateValueOjects, func){
for(let a=0;a<formulaArray.length;a++){
let range = formulaArray[a];
let cacheKey = "r"+range.row[0]+""+range.row[1]+"c"+range.column[0]+""+range.column[1]+"index"+range.sheetIndex;
Expand All @@ -5036,8 +5036,7 @@ const luckysheetformula = {
for(let c=range.column[0];c<=range.column[1];c++){
let key = "r" + r + "c" + c + "i" + range.sheetIndex;
func(key, r, c, range.sheetIndex);

if(formulaObjects && key in formulaObjects){
if((formulaObjects && key in formulaObjects) || (updateValueOjects && key in updateValueOjects) ){
functionArr.push({
key:key,
r:r,
Expand All @@ -5048,7 +5047,7 @@ const luckysheetformula = {
}
}

if(formulaObjects){
if(formulaObjects || updateValueOjects){
arrayMatchCache[cacheKey] = functionArr;
}
}
Expand Down Expand Up @@ -5108,29 +5107,60 @@ const luckysheetformula = {
r:formulaCell.r,
c:formulaCell.c,
index:formulaCell.index,
parents:{},
chidren:{},
color:"w"
}

formulaObjects[key] = item;

if(isForce){
updateValueArray.push(item);
}
else{
arrayMatch(formulaArray, null, function(key){
if(key in updateValueOject){
updateValueArray.push(item);
}
});
}


// if(isForce){
// updateValueArray.push(item);
// }
// else{
// arrayMatch(formulaArray, null, function(key){
// if(key in updateValueOjects){
// updateValueArray.push(item);
// }
// });
// }

}
// console.log(formulaObjects)
// console.timeEnd("1");

// console.timeEnd("1");

// console.time("2");
//形成一个公式之间引用的图结构
Object.keys(formulaObjects).forEach((key)=>{
let formulaObject = formulaObjects[key];
arrayMatch(formulaObject.formulaArray, formulaObjects, updateValueOjects, function(childKey){
if(childKey in formulaObjects){
let childFormulaObject = formulaObjects[childKey];
formulaObject.chidren[childKey] = 1;
childFormulaObject.parents[key] = 1;
}
// console.log(childKey,formulaObject.formulaArray);
if(childKey in updateValueOjects){
updateValueArray.push(formulaObject);
}
});
});

// console.log(formulaObjects)
// console.timeEnd("2");


// console.time("3");
let formulaRunList = [];
//计算
//计算,采用深度优先遍历公式形成的图结构

// updateValueArray.forEach((key)=>{
// let formulaObject = formulaObjects[key];


// });

let stack = updateValueArray, existsFormulaRunList={};
while(stack.length>0){
Expand All @@ -5140,30 +5170,40 @@ const luckysheetformula = {
continue;
}

if(formulaObject.color == "b"){
formulaRunList.push(formulaObject);
existsFormulaRunList[formulaObject.key] = 1;
continue;
}

let cacheStack = [];
arrayMatch(formulaObject.formulaArray, formulaObjects,function(childKey){
if(childKey in formulaObjects && !(childKey in existsFormulaRunList)){
let childFormulaObject = formulaObjects[childKey];
cacheStack.push(childFormulaObject);
Object.keys(formulaObject.parents).forEach((parentKey)=>{
let parentFormulaObject = formulaObjects[parentKey];
if(parentFormulaObject!=null){
cacheStack.push(parentFormulaObject);
}
});


ii++;

if(cacheStack.length==0){
formulaRunList.push(formulaObject);
existsFormulaRunList[formulaObject.key] = 1;
}
else{
formulaObject.color = "b";
stack.push(formulaObject);
stack = stack.concat(cacheStack);
}
}

formulaRunList.reverse();

// console.log(formulaObjects, ii)
// console.timeEnd("2");
// console.timeEnd("3");

// console.time("3");
// console.time("4");
for(let i=0;i<formulaRunList.length;i++){
let formulaCell = formulaRunList[i];
if(formulaCell.level==Math.max){
Expand Down Expand Up @@ -5191,7 +5231,7 @@ const luckysheetformula = {
};
}
// console.log(formulaRunList);
// console.timeEnd("3");
// console.timeEnd("4");

_this.execFunctionExist = null;
},
Expand Down

0 comments on commit cf6c5be

Please sign in to comment.