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(sheet): move formula ref #2078

Merged
merged 1 commit into from
Apr 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/core/src/shared/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
VerticalAlign,
WrapStrategy,
} from '../types/enum';
import type { IRange } from '../types/interfaces';
import { type IRange, RANGE_TYPE } from '../types/interfaces';
import type { ICellData } from '../types/interfaces/i-cell-data';
import type { IDocumentData } from '../types/interfaces/i-document-data';
import type { IRangeWithCoord, ISelectionCell, ISelectionCellWithCoord } from '../types/interfaces/i-selection-data';
Expand Down Expand Up @@ -531,20 +531,24 @@ export function getDocsUpdateBody(model: IDocumentData, segmentId?: string) {
}

export function isValidRange(range: IRange): boolean {
const { startRow, endRow, startColumn, endColumn } = range;
const { startRow, endRow, startColumn, endColumn, rangeType } = range;
if (
startRow < 0
|| startColumn < 0
|| endRow < 0
|| endColumn < 0
|| Number.isNaN(startRow)
|| Number.isNaN(endRow)
|| Number.isNaN(startColumn)
|| Number.isNaN(endColumn)
) {
return false;
}

if (!(Number.isNaN(startRow) && Number.isNaN(endRow)) && rangeType === RANGE_TYPE.COLUMN) {
return false;
}

if (!(Number.isNaN(startColumn) && Number.isNaN(endColumn)) && rangeType === RANGE_TYPE.ROW) {
return false;
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shared/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function moveRangeByOffset(range: IRange, refOffsetX: number, refOffsetY:
const startAbsoluteRefType = newRange.startAbsoluteRefType || AbsoluteRefType.NONE;
const endAbsoluteRefType = newRange.endAbsoluteRefType || AbsoluteRefType.NONE;

if (startAbsoluteRefType === AbsoluteRefType.ALL && endAbsoluteRefType === AbsoluteRefType.ALL) {
if (!ignoreAbsolute && startAbsoluteRefType === AbsoluteRefType.ALL && endAbsoluteRefType === AbsoluteRefType.ALL) {
return newRange;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,56 @@ describe('lexer nodeMaker test', () => {
expect(result).toStrictEqual('=SUM($A$1:$B$10) + LAMBDA(x, y, x*y*x)($A$1:$B$10,$A$10) + MAX($A$1:$B$10,SUM($A$2))');
});
});

describe('moveFormulaRefOffset', () => {
it('move all', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum(A1:B1)', 1, 1, false);
expect(result).toStrictEqual('=sum(B2:C2)');
});

it('move not first column', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum($A1:B1)', 1, 1, false);
expect(result).toStrictEqual('=sum($A2:C2)');
});

it('move not first row', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum(A$1:$B1)', 1, 1, false);
expect(result).toStrictEqual('=sum(B$1:$B2)');
});

it('move only column', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum(A:B)', 1, 1, false);
expect(result).toStrictEqual('=sum(B:C)');
});

it('move only column absolute end', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum(A:$B)', 1, 1, false);
expect(result).toStrictEqual('=sum(B:$B)');
});

it('move only column absolute all', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum($A:$B)', 1, 1, false);
expect(result).toStrictEqual('=sum($A:$B)');
});

it('move only row absolute end', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum(1:$3)', 1, 1, false);
expect(result).toStrictEqual('=sum(2:$3)');
});

it('move only row absolute all', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum($1:$3)', 1, 1, false);
expect(result).toStrictEqual('=sum($1:$3)');
});

it('move omit absolute all', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum($A$1:$B$3)', 1, 1, true);
expect(result).toStrictEqual('=sum($B$2:$C$4)');
});

it('move omit absolute column', () => {
const result = lexerTreeBuilder.moveFormulaRefOffset('=sum(A$1:B$3)', 1, 1, true);
expect(result).toStrictEqual('=sum(B$2:C$4)');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export class LexerTreeBuilder extends Disposable {

let newRange: IRange = sequenceGrid.range;

if (newRange.startAbsoluteRefType === AbsoluteRefType.ALL && newRange.endAbsoluteRefType === AbsoluteRefType.ALL) {
if (!ignoreAbsolute && newRange.startAbsoluteRefType === AbsoluteRefType.ALL && newRange.endAbsoluteRefType === AbsoluteRefType.ALL) {
newSequenceNodes.push(node);
continue;
} else {
Expand Down
11 changes: 9 additions & 2 deletions packages/engine-formula/src/engine/utils/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ export interface IAbsoluteRefTypeForRange {
* @param singleRefString for example A1 or B10, not A1:B10
*/
export function getAbsoluteRefTypeWithSingleString(singleRefString: string) {
const isColumnAbsolute = singleRefString[0] === '$';
let isColumnAbsolute = singleRefString[0] === '$';
const remainChar = singleRefString.substring(1);

const isRowAbsolute = remainChar.indexOf('$') > -1;
let isRowAbsolute = remainChar.indexOf('$') > -1;


if (Tools.isStringNumber(remainChar) && isColumnAbsolute && !isRowAbsolute) {
isColumnAbsolute = false;
isRowAbsolute = true;
}


if (isColumnAbsolute && isRowAbsolute) {
return AbsoluteRefType.ALL;
Expand Down
Loading