Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formula): today fill error #1798

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -309,5 +309,78 @@ describe('lexer nodeMaker test', () => {
')',
]);
});

it('No Parameter Function', () => {
expect(lexerTreeBuilder.sequenceNodesBuilder('=today()+today()+column()')).toStrictEqual([
{
endIndex: 4,
nodeType: 3,
startIndex: 0,
token: 'today',
},
'(',
')',
'+',
{
endIndex: 12,
nodeType: 3,
startIndex: 8,
token: 'today',
},
'(',
')',
'+',
{
endIndex: 21,
nodeType: 3,
startIndex: 16,
token: 'column',
},
'(',
')',
]);
});

it('No Parameter Today', () => {
expect(lexerTreeBuilder.sequenceNodesBuilder('=IF(TODAY()>1,"TRUE", "FALSE")')).toStrictEqual([
{
endIndex: 1,
nodeType: 3,
startIndex: 0,
token: 'IF',
},
'(',
{
endIndex: 7,
nodeType: 3,
startIndex: 3,
token: 'TODAY',
},
'(',
')',
'>',
{
endIndex: 11,
nodeType: 1,
startIndex: 11,
token: '1',
},
',',
{
endIndex: 18,
nodeType: 2,
startIndex: 13,
token: '"TRUE"',
},
',',
{
endIndex: 27,
nodeType: 2,
startIndex: 20,
token: ' "FALSE"',
},
')',
]);
});
});
});
72 changes: 48 additions & 24 deletions packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,16 @@ export class LexerTreeBuilder extends Disposable {
if (!this._setParentCurrentLexerNode() && cur !== formulaStringArrayCount - 1) {
return ErrorType.VALUE;
}
/**
* https://github.com/dream-num/univer/issues/1769
* Formula example: =IF(TODAY()>1,"TRUE", "FALSE")
* Copy or auto-fill at complex formula get error formula offset
*/
this._addSequenceArray(sequenceArray, currentString, cur, isZeroAdded);
cur++;
this._addSequenceArray(sequenceArray, nextCurrentString, cur, isZeroAdded);
cur++;
continue;
} else if (nextCurrentString) {
// const subLexerNode = new LexerNode();
// subLexerNode.token = DEFAULT_TOKEN_TYPE_PARAMETER;
Expand Down Expand Up @@ -1356,29 +1365,32 @@ export class LexerTreeBuilder extends Disposable {
if (this._negativeCondition(prevString)) {
this._pushSegment(operatorToken.MINUS);

if (!(isZeroAdded && cur === 0)) {
sequenceArray?.push({
segment: this._segment,
currentString,
cur,
currentLexerNode: this._currentLexerNode,
});
}
// if (!(isZeroAdded && cur === 0)) {
// sequenceArray?.push({
// segment: this._segment,
// currentString,
// cur,
// currentLexerNode: this._currentLexerNode,
// });
// }

this._addSequenceArray(sequenceArray, currentString, cur, isZeroAdded);

cur++;
continue;
}
} else if (this._segment.length > 0 && formulaStringArray[cur - 1] && formulaStringArray[cur - 1].toUpperCase() === 'E' && (currentString === operatorToken.MINUS || currentString === operatorToken.PLUS)) {
this._pushSegment(currentString);

if (!(isZeroAdded && cur === 0)) {
sequenceArray?.push({
segment: this._segment,
currentString,
cur,
currentLexerNode: this._currentLexerNode,
});
}
// if (!(isZeroAdded && cur === 0)) {
// sequenceArray?.push({
// segment: this._segment,
// currentString,
// cur,
// currentLexerNode: this._currentLexerNode,
// });
// }
this._addSequenceArray(sequenceArray, currentString, cur, isZeroAdded);

cur++;
continue;
Expand All @@ -1405,17 +1417,29 @@ export class LexerTreeBuilder extends Disposable {
this._pushSegment(currentString);
}

if (!(isZeroAdded && cur === 0)) {
sequenceArray?.push({
segment: this._segment,
currentString,
cur,
currentLexerNode: this._currentLexerNode,
});
}
// if (!(isZeroAdded && cur === 0)) {
// sequenceArray?.push({
// segment: this._segment,
// currentString,
// cur,
// currentLexerNode: this._currentLexerNode,
// });
// }
this._addSequenceArray(sequenceArray, currentString, cur, isZeroAdded);
cur++;
}

this._pushNodeToChildren(this._segment);
}

private _addSequenceArray(sequenceArray: ISequenceArray[] | undefined, currentString: string, cur: number, isZeroAdded: boolean) {
if (!(isZeroAdded && cur === 0)) {
sequenceArray?.push({
segment: this._segment,
currentString,
cur,
currentLexerNode: this._currentLexerNode,
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ export class BaseReferenceObject extends ObjectClassType {
}

getActiveSheetRowCount() {
return this.getCurrentActiveSheetData().rowCount;
return this.getCurrentActiveSheetData()?.rowCount || 0;
}

getActiveSheetColumnCount() {
return this.getCurrentActiveSheetData().columnCount;
return this.getCurrentActiveSheetData()?.columnCount || 0;
}

getRowCount() {
Expand All @@ -341,11 +341,11 @@ export class BaseReferenceObject extends ObjectClassType {
}

getRowData() {
return this.getCurrentActiveSheetData().rowData;
return this.getCurrentActiveSheetData()?.rowData || {};
}

getColumnData() {
return this.getCurrentActiveSheetData().columnData;
return this.getCurrentActiveSheetData()?.columnData || {};
}

isCell() {
Expand Down Expand Up @@ -411,7 +411,7 @@ export class BaseReferenceObject extends ObjectClassType {
}

getCurrentActiveSheetData() {
return this._unitData[this.getUnitId()][this.getSheetId()];
return this._unitData[this.getUnitId()]?.[this.getSheetId()];
}

getCurrentRuntimeSheetData() {
Expand Down