Skip to content

Commit

Permalink
calc: fix: named-range can't be used in function like SUM
Browse files Browse the repository at this point in the history
Signed-off-by: Rashesh <[email protected]>
Change-Id: Ia07c579b3bc81ce73f40e86e6e07a02098e7bf4c

wip

Signed-off-by: Rashesh <[email protected]>
Change-Id: I2c2ecc74ed836f3b6d11c20990c8c8b29950abfe
  • Loading branch information
Rash419 committed Sep 5, 2024
1 parent b36b34d commit d4581a2
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 8 deletions.
77 changes: 72 additions & 5 deletions browser/src/control/Control.FormulaAutoCompletePopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,82 @@ class FormulaAutoCompletePopup extends L.Control.AutoCompletePopup {
return entries;
}

getAutocompleteText(
currentCellFormula: string,
functionName: string,
endIndex: number,
): string {
const openBracketIndex: number[] = [];
const semicolonIndex: number[] = [];
const equalIndex: number = 0;

for (let i = 0; i < currentCellFormula.length; i++) {
if (currentCellFormula.charAt(i) === '(') openBracketIndex.push(i);
else if (currentCellFormula.charAt(i) === ';') semicolonIndex.push(i);
}

let minDiff: number = Number.MAX_VALUE;
let startIndex: number;

const tmp = endIndex - equalIndex;
if (tmp >= 0 && tmp < minDiff) {
minDiff = tmp;
startIndex = equalIndex + 1;
}

openBracketIndex.forEach((index) => {
const tmp = endIndex - index;
if (tmp >= 0 && tmp < minDiff) {
minDiff = tmp;
startIndex = index + 1;
}
});

semicolonIndex.forEach((index) => {
const tmp = endIndex - index;
if (tmp >= 0 && tmp < minDiff) {
minDiff = tmp;
startIndex = index + 1;
}
});

const partialText: string = currentCellFormula
.substring(startIndex, endIndex + 1)
.trim();

let autoCompleteFunctionName: string = '';
for (
let i = 0;
i < Math.max(partialText.length, functionName.length);
i++
) {
if (
partialText.charAt(i).toLowerCase() !=
functionName.charAt(i).toLowerCase()
) {
autoCompleteFunctionName = functionName.substring(i);
break;
}
}

return autoCompleteFunctionName;
}

callback(objectType: any, eventType: any, object: any, index: number) {
if (eventType === 'close') {
this.closePopup();
} else if (eventType === 'select' || eventType === 'activate') {
var currentText = this.map._docLayer._lastFormula;
var chIndex = currentText.length - 1;
var functionName = this.functionList[index].name;
var namedRange = this.functionList[index].namedRange;
functionName = functionName.substring(chIndex);
const namedRange: string = this.functionList[index].namedRange;
const currentText: string = this.map._docLayer._lastFormula;
const addedCharacterIndex: number =
this.map._docLayer._newFormulaDiffIndex;

const functionName: string = this.getAutocompleteText(
currentText,
this.functionList[index].name,
addedCharacterIndex,
);

if (namedRange) this.map._textInput._sendText(functionName);
else this.map._textInput._sendText(functionName + '(');
this.closePopup();
Expand Down
20 changes: 17 additions & 3 deletions browser/src/layer/tile/CanvasTileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1794,9 +1794,23 @@ L.CanvasTileLayer = L.Layer.extend({
// This is done because coolwsd will send several 'cellformula'
// messages during text composition, and resetting the contents
// of the clipboard container mid-composition will easily break it.
var formula = textMsg.substring(13);
this._lastFormula = formula;
this._map.fire('cellformula', {formula: formula});

let newFormula = textMsg.substring(13);
if (this._lastFormula) {
let minLength = Math.min(newFormula.length, this._lastFormula.length);
let index = -1;
for (let i = 0; i < minLength; i++) {
if (newFormula.charAt(i) !== this._lastFormula.charAt(i)) {
index = i;
break;
}
}
if (index === -1)
index = minLength;
this._newFormulaDiffIndex = index;
}
this._lastFormula = newFormula;
this._map.fire('cellformula', {formula: newFormula});
},

_onCalcFunctionUsageMsg: function (textMsg) {
Expand Down

0 comments on commit d4581a2

Please sign in to comment.