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(sheets): boolean should store as number #1605

Merged
merged 2 commits into from
Mar 16, 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
12 changes: 8 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
https://github.com/dream-num/univer/blob/dev/CONTRIBUTING.md#submitting-pull-requests
-->

- [ ] I am sure that the code is update-to-date with the `dev` branch.

<!-- Associate an issue with the pull request. -->
<!-- Associate issues with the pull request if there is one. Separate them width commas. -->
<!-- Feel free to delete this if there is no related issue. -->

close #
close #xxx, #yyy, #zzzz

<!-- A description of the proposed changes. -->

<!-- How to test them. -->

<!-- Uncomment the below lines if there are breaking changes introduced in this PR. -->
<!-- BREAKING CHANGE:
Before:

After: -->
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export {
} from './sheets/sheet-snapshot-utils';
export { SheetViewModel } from './sheets/view-model';
export { getWorksheetUID, Workbook } from './sheets/workbook';
export { Worksheet } from './sheets/worksheet';
export { Worksheet, extractPureTextFromCell } from './sheets/worksheet';
export * from './slides/domain';
export * from './types/const';
export * from './types/enum';
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/sheets/__tests__/worksheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { LocaleType } from '../../types/enum/locale-type';
import { extractPureTextFromCell, type Worksheet } from '../worksheet';
import { type IRange, RANGE_TYPE } from '../../types/interfaces/i-range';
import { DisposableCollection } from '../../shared/lifecycle';
import { CellValueType } from '../../types/enum';
import { createCoreTestBed } from './create-core-test-bed';

describe('test worksheet', () => {
Expand Down Expand Up @@ -253,4 +254,11 @@ describe('test "extractPureTextFromCell"', () => {
expect(extractPureTextFromCell({ v: true })).toBe('TRUE');
expect(extractPureTextFromCell({ v: 1 })).toBe('1');
});

describe('test "CellType"', () => {
it('should return boolean literal when cell type is boolean', () => {
expect(extractPureTextFromCell({ t: CellValueType.BOOLEAN, v: 1 })).toBe('TRUE');
expect(extractPureTextFromCell({ t: CellValueType.BOOLEAN, v: 0 })).toBe('FALSE');
});
});
});
24 changes: 20 additions & 4 deletions packages/core/src/sheets/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { CellValueType } from '@univerjs/protocol';

import type { Nullable } from '../shared';
import { ObjectMatrix, Rectangle, Tools } from '../shared';
import { createRowColIter } from '../shared/row-col-iter';
Expand Down Expand Up @@ -634,9 +636,23 @@ export interface ICell {
* @param cell
* @returns pure text in this cell
*/
export function extractPureTextFromCell(cell: ICellData): Nullable<string> {
const rawValue = cell?.p?.body?.dataStream ?? cell?.v;
if (typeof rawValue === 'number') return `${rawValue}`;
export function extractPureTextFromCell(cell: ICellData): string {
const richTextValue = cell.p?.body?.dataStream;
if (richTextValue) return richTextValue;

const rawValue = cell.v;

if (typeof rawValue === 'string') {
if (cell.t === CellValueType.BOOLEAN) return rawValue.toUpperCase();
return rawValue;
};

if (typeof rawValue === 'number') {
if (cell.t === CellValueType.BOOLEAN) return rawValue ? 'TRUE' : 'FALSE';
return rawValue.toString();
};

if (typeof rawValue === 'boolean') return rawValue ? 'TRUE' : 'FALSE';
return rawValue;

return '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
CellValueType,
DEFAULT_EMPTY_DOCUMENT_VALUE,
DocumentDataModel,
extractPureTextFromCell,
getColorStyle,
HorizontalAlign,
IContextService,
Expand Down Expand Up @@ -1088,7 +1089,7 @@ export class SpreadsheetSkeleton extends Skeleton {
const textStyle = this._getFontFormat(style);
fontString = getFontStyleString(textStyle, this._localService).fontCache;

documentModel = this._getDocumentDataByStyle(cell.v.toString(), textStyle, {
documentModel = this._getDocumentDataByStyle(extractPureTextFromCell(cell), textStyle, {
...cellOtherConfig,
textRotation,
cellValueType: cell.t!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,12 @@ export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, bo

// Set to null, clear content
if (newVal.v !== undefined) {
oldVal.v = type === CellValueType.NUMBER ? Number(newVal.v) : newVal.v;
oldVal.v = type === CellValueType.NUMBER
? Number(newVal.v)
: type === CellValueType.BOOLEAN
// if the value is a boolean, we should store it as 1 or 0
? (newVal.v!.toString()).toUpperCase() === 'TRUE' ? 1 : 0
: newVal.v;
}

if (oldVal.v !== undefined) {
Expand Down