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

feat: add function to extract pure text from cell #1575

Merged
merged 1 commit into from
Mar 14, 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
56 changes: 50 additions & 6 deletions packages/core/src/sheets/__tests__/worksheet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { Univer } from '../../basics/univer';
import type { IWorkbookData } from '../../types/interfaces/i-workbook-data';
import { LocaleType } from '../../types/enum/locale-type';
import type { Worksheet } from '../worksheet';
import { extractPureTextFromCell, type Worksheet } from '../worksheet';
import { type IRange, RANGE_TYPE } from '../../types/interfaces/i-range';
import { DisposableCollection } from '../../shared/lifecycle';
import { createCoreTestBed } from './create-core-test-bed';

describe('Test worksheet', () => {
describe('test worksheet', () => {
let univer: Univer;
let worksheet: Worksheet;
let caseDisposable: DisposableCollection;
Expand All @@ -40,7 +40,7 @@ describe('Test worksheet', () => {
caseDisposable.dispose();
});

describe('Test "worksheet.iterateByRow"', () => {
describe('test "worksheet.iterateByRow"', () => {
const TEST_WORKBOOK_DATA_WITH_MERGED_CELL: IWorkbookData = {
id: 'test',
appVersion: '3.0.0-alpha',
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('Test worksheet', () => {
caseDisposable = new DisposableCollection();
});

it('Should "iteratorByRow" work with merged cells', () => {
it('should "iteratorByRow" work with merged cells', () => {
// This interceptor just returns the raw cell data.
worksheet.__interceptViewModel((viewModel) => {
const cellInterceptorDisposable = viewModel.registerCellContentInterceptor({
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Test worksheet', () => {
});
});

describe('Test "worksheet.iterateByColumn"', () => {
describe('test "worksheet.iterateByColumn"', () => {
const TEST_WORKBOOK_DATA_WITH_MERGED_CELL: IWorkbookData = {
id: 'test',
appVersion: '3.0.0-alpha',
Expand Down Expand Up @@ -169,7 +169,7 @@ describe('Test worksheet', () => {
caseDisposable = new DisposableCollection();
});

it('Should "iterateByColumn" work with merged cells', () => {
it('should "iterateByColumn" work with merged cells', () => {
// This interceptor just returns the raw cell data.
worksheet.__interceptViewModel((viewModel) => {
const cellInterceptorDisposable = viewModel.registerCellContentInterceptor({
Expand Down Expand Up @@ -210,3 +210,47 @@ describe('Test worksheet', () => {
});
});
});

describe('test "extractPureTextFromCell"', () => {
it('should extract from rich text', () => {
expect(extractPureTextFromCell({
p: {
id: 'd',
body: {
dataStream: 'Some rich\ntext.',
textRuns: [
{
st: 0,
ed: 5,
ts: {
cl: {
rgb: 'rgb(92,92,92)',
},
},
},
],
},
documentStyle: {
pageSize: {
width: Number.POSITIVE_INFINITY,
height: Number.POSITIVE_INFINITY,
},
marginTop: 0,
marginBottom: 0,
marginRight: 2,
marginLeft: 2,
},
},
})).toBe('Some rich\ntext.');
});

it('should extract from formula and plain text', () => {
expect(extractPureTextFromCell({ v: 6, f: '=SUM(3, 3)' })).toBe('6');
});

it('should support number and boolean values', () => {
expect(extractPureTextFromCell({ v: false })).toBe('FALSE');
expect(extractPureTextFromCell({ v: true })).toBe('TRUE');
expect(extractPureTextFromCell({ v: 1 })).toBe('1');
});
});
12 changes: 12 additions & 0 deletions packages/core/src/sheets/worksheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,3 +660,15 @@ export interface ICell {
colSpan?: number;
value: ICellData;
}

/**
* Get pure text in a cell.
* @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}`;
if (typeof rawValue === 'boolean') return rawValue ? 'TRUE' : 'FALSE';
return rawValue;
}