Skip to content

Commit

Permalink
fix(formula): get rich text data stream as content (#1305)
Browse files Browse the repository at this point in the history
* fix(formula): get rich text data stream as content

* fix(formula): get datastream number when insert function
  • Loading branch information
Dushusir committed Feb 7, 2024
1 parent 0204b43 commit 51ae0e1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
NumberValueObject,
StringValueObject,
} from '../value-object/primitive-object';
import { getCellValue } from '../utils/cell';

export type NodeValueType = BaseValueObject | BaseReferenceObject | AsyncObject | AsyncArrayObject;

Expand Down Expand Up @@ -332,7 +333,7 @@ export class BaseReferenceObject extends ObjectClassType {
}

getCellValueObject(cell: ICellData) {
const value = cell.v || 0;
const value = getCellValue(cell);
if (ERROR_TYPE_SET.has(value as ErrorType)) {
return new ErrorValueObject(value as ErrorType);
}
Expand Down
46 changes: 46 additions & 0 deletions packages/engine-formula/src/engine/utils/__tests__/cell.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { describe, expect, it } from 'vitest';

import type { ICellData } from '@univerjs/core';
import { getCellValue } from '../cell';

describe('Test cell', () => {
it('Function getCellValue', () => {
const cell1: ICellData = {
p: {
id: 'p',
body: {
dataStream: 'test\r\n',
},
documentStyle: {},
},
};

const cell2 = {
v: 2,
};

const cell3 = {
f: '',
};

expect(getCellValue(cell1)).toBe('test');
expect(getCellValue(cell2)).toBe(2);
expect(getCellValue(cell3)).toBe(0);
});
});
40 changes: 40 additions & 0 deletions packages/engine-formula/src/engine/utils/cell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { DEFAULT_EMPTY_DOCUMENT_VALUE } from '@univerjs/core';
import type { ICellData, Nullable } from '@univerjs/core';

export function getCellValue(cell: Nullable<ICellData>) {
if (cell === null) {
return 0;
}

if (cell?.p) {
const body = cell?.p.body;

if (body == null) {
return 0;
}

const data = body.dataStream;
const lastString = data.substring(data.length - 2, data.length);
const newDataStream = lastString === DEFAULT_EMPTY_DOCUMENT_VALUE ? data.substring(0, data.length - 2) : data;

return newDataStream;
}

return cell?.v || 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ describe('Test insert function operation', () => {
const cell = {};
expect(isNumberCell(cell)).toBeFalsy();
});
it('should return true when cell is rich text number', () => {
const cell = {
p: {
id: 'rich1',
documentStyle: {},
body: {
dataStream: '111/r/n',
},
},
};
expect(isNumberCell(cell)).toBeTruthy();
});
it('should return true when cell is rich text', () => {
const cell = {
p: {
id: 'rich1',
documentStyle: {},
body: {
dataStream: 'rich/r/n',
},
},
};
expect(isNumberCell(cell)).toBeFalsy();
});
});

describe('function isSingleCell', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import type { ICellData, ICommand, IRange, Nullable, ObjectMatrix } from '@unive
import {
CellValueType,
CommandType,
DEFAULT_EMPTY_DOCUMENT_VALUE,
getCellValueType,
ICommandService,
isRealNum,
IUniverInstanceService,
Rectangle,
} from '@univerjs/core';
Expand Down Expand Up @@ -235,6 +237,19 @@ function findStartColumn(cellMatrix: ObjectMatrix<Nullable<ICellData>>, row: num
}

export function isNumberCell(cell: Nullable<ICellData>) {
if (cell?.p) {
const body = cell?.p.body;

if (body == null) {
return false;
}

const data = body.dataStream;
const lastString = data.substring(data.length - 2, data.length);
const newDataStream = lastString === DEFAULT_EMPTY_DOCUMENT_VALUE ? data.substring(0, data.length - 2) : data;

return isRealNum(newDataStream);
}
return cell && (cell.t === CellValueType.NUMBER || getCellValueType(cell) === CellValueType.NUMBER);
}

Expand Down

0 comments on commit 51ae0e1

Please sign in to comment.