Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix: fixed some issues with defaultStyle (#5290)

wpxp12345699dab69d61f4
diff --git a/packages/core/src/sheets/__tests__/worksheet.spec.ts b/packages/core/src/sheets/__tests__/worksheet.spec.ts
index dfeb11233c..fbe1e8ac40 100644
--- a/packages/core/src/sheets/__tests__/worksheet.spec.ts
+++ b/packages/core/src/sheets/__tests__/worksheet.spec.ts
@@ -16,12 +16,13 @@
import type { Univer } from '../../univer';
import type { IRange, IWorkbookData } from '../typedef';
+import type { Worksheet } from '../worksheet';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { DisposableCollection } from '../../shared/lifecycle';
import { CellValueType } from '../../types/enum';
import { LocaleType } from '../../types/enum/locale-type';
import { RANGE_TYPE } from '../typedef';
-import { extractPureTextFromCell, type Worksheet } from '../worksheet';
+import { extractPureTextFromCell } from '../worksheet';
import { createCoreTestBed } from './create-core-test-bed';
describe('test worksheet', () => {
@@ -209,6 +210,120 @@ describe('test worksheet', () => {
expect(value6.value).toBeUndefined();
});
});
+
+ describe('test "worksheet.getComposedCellStyle"', () => {
+ const TEST_WORKBOOK_DATA_WITH_DEFAULT_STYLE: IWorkbookData = {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ defaultStyle: {
+ fs: 20,
+ cl: {
+ rgb: 'red',
+ },
+ bd: {
+ t: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ b: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ l: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ r: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ },
+ },
+ mergeData: [
+ { startRow: 0, endRow: 0, startColumn: 1, endColumn: 2 },
+ ],
+ cellData: {
+ 0: {
+ 0: {
+ v: 'A1',
+ },
+ 1: {
+ v: 'B1:C1',
+ },
+ },
+ 1: {
+ // should skip over empty cells
+ // 0: {
+ // v: 'A1',
+ // },
+ 1: {
+ v: 'B2',
+ },
+ 2: {
+ v: 'C2',
+ },
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: 'TEST_WORKBOOK_DATA_WITH_DEFAULT_STYLE',
+ sheetOrder: ['sheet1'],
+ styles: {},
+ };
+
+ beforeEach(() => {
+ prepare(TEST_WORKBOOK_DATA_WITH_DEFAULT_STYLE);
+ caseDisposable = new DisposableCollection();
+ });
+
+ it('test style', () => {
+ const style = worksheet.getComposedCellStyle(0, 0);
+ expect(style).toEqual({
+ fs: 20,
+ cl: {
+ rgb: 'red',
+ },
+ bd: {
+ t: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ b: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ l: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ r: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ },
+ });
+ });
+ });
});
describe('test "extractPureTextFromCell"', () => {
diff --git a/packages/core/src/sheets/sheet-skeleton.ts b/packages/core/src/sheets/sheet-skeleton.ts
index 27e7083909..93ac74a574 100644
--- a/packages/core/src/sheets/sheet-skeleton.ts
+++ b/packages/core/src/sheets/sheet-skeleton.ts
@@ -66,10 +66,6 @@ export class SheetSkeleton extends Skeleton {
protected _renderRawFormula = false;
protected _cellData: ObjectMatrix<Nullable<ICellData>>;
protected _imageCacheMap: ImageCacheMap;
- /**
- * Whether the row style precedes the column style.
- */
- protected _isRowStylePrecedeColumnStyle = false;
/**
* Whether auto height for merged cells
@@ -94,7 +90,7 @@ export class SheetSkeleton extends Skeleton {
initConfig() {
this._skipAutoHeightForMergedCells = !(this._configService.getConfig(AUTO_HEIGHT_FOR_MERGED_CELLS) ?? false);
- this._isRowStylePrecedeColumnStyle = this._configService.getConfig(IS_ROW_STYLE_PRECEDE_COLUMN_STYLE) ?? false;
+ this.worksheet.setIsRowStylePrecedeColumnStyle(this._configService.getConfig(IS_ROW_STYLE_PRECEDE_COLUMN_STYLE) ?? false);
}
resetCache() {
diff --git a/packages/core/src/sheets/worksheet.ts b/packages/core/src/sheets/worksheet.ts
index a86a392d71..a3055e0bce 100644
--- a/packages/core/src/sheets/worksheet.ts
+++ b/packages/core/src/sheets/worksheet.ts
@@ -83,6 +83,11 @@ export class Worksheet {
protected _spanModel: SpanModel;
+ /**
+ * Whether the row style precedes the column style.
+ */
+ protected _isRowStylePrecedeColumnStyle: boolean = true;
+
private _getCellHeight: Nullable<(row: number, col: number) => number>;
constructor(
@@ -147,6 +152,10 @@ export class Worksheet {
return this._spanModel;
}
+ setIsRowStylePrecedeColumnStyle(isRowStylePrecedeColumnStyle: boolean): void {
+ this._isRowStylePrecedeColumnStyle = isRowStylePrecedeColumnStyle;
+ }
+
getStyleDataByHash(hash: string): Nullable<IStyleData> {
const data = this._styles.get(hash);
return { ...data };
@@ -246,14 +255,15 @@ export class Worksheet {
* @param {number} col The column index of the cell
* @returns {IStyleData} The composed style of the cell
*/
- getComposedCellStyle(row: number, col: number, rowPriority = true): IStyleData {
- const cell = this.getCellStyle(row, col);
+ getComposedCellStyle(row: number, col: number, rowPriority?: boolean): IStyleData {
const defaultStyle = this.getDefaultCellStyleInternal();
const rowStyle = this.getRowStyle(row);
const colStyle = this.getColumnStyle(col);
- return rowPriority
- ? composeStyles(defaultStyle, rowStyle, colStyle, cell)
- : composeStyles(defaultStyle, colStyle, rowStyle, cell);
+ const cell = this.getCell(row, col);
+ const cellStyle = this._styles.getStyleByCell(cell);
+ return (rowPriority ?? this._isRowStylePrecedeColumnStyle)
+ ? composeStyles(defaultStyle, colStyle, rowStyle, cell?.themeStyle, cellStyle)
+ : composeStyles(defaultStyle, rowStyle, colStyle, cell?.themeStyle, cellStyle);
}
/**
@@ -1064,21 +1074,20 @@ export class Worksheet {
* @param options
*/
// eslint-disable-next-line complexity, max-lines-per-function
- private _getCellDocumentModel(
+ getCellDocumentModel(
cell: Nullable<ICellDataForSheetInterceptor>,
+ style: Nullable<IStyleData>,
options: ICellDocumentModelOption = DEFAULT_CELL_DOCUMENT_MODEL_OPTION
): Nullable<IDocumentLayoutObject> {
+ if (!cell) {
+ return;
+ }
+
const { isDeepClone, displayRawFormula, ignoreTextRotation } = {
...DEFAULT_CELL_DOCUMENT_MODEL_OPTION,
...options,
};
- const style = this._styles.getStyleByCell(cell);
-
- if (!cell) {
- return;
- }
-
let documentModel: Nullable<DocumentDataModel>;
let fontString = 'document';
const cellOtherConfig = extractOtherStyle(style);
@@ -1209,11 +1218,10 @@ export class Worksheet {
/**
* Only used for cell edit, and no need to rotate text when edit cell content!
*/
- getBlankCellDocumentModel(cell: Nullable<ICellData>): IDocumentLayoutObject {
- const documentModelObject = this._getCellDocumentModel(cell, { ignoreTextRotation: true });
-
- const style = this._styles.getStyleByCell(cell);
+ getBlankCellDocumentModel(cell: Nullable<ICellData>, row: number, column: number): IDocumentLayoutObject {
+ const style = this.getComposedCellStyle(row, column);
const textStyle = getFontFormat(style);
+ const documentModelObject = this.getCellDocumentModel(cell, style, { ignoreTextRotation: true });
if (documentModelObject != null) {
if (documentModelObject.documentModel == null) {
@@ -1248,8 +1256,9 @@ export class Worksheet {
}
// Only used for cell edit, and no need to rotate text when edit cell content!
- getCellDocumentModelWithFormula(cell: ICellData): Nullable<IDocumentLayoutObject> {
- return this._getCellDocumentModel(cell, {
+ getCellDocumentModelWithFormula(cell: ICellData, row: number, column: number): Nullable<IDocumentLayoutObject> {
+ const style = this.getComposedCellStyle(row, column);
+ return this.getCellDocumentModel(cell, style, {
isDeepClone: true,
displayRawFormula: true,
ignoreTextRotation: true,
diff --git a/packages/core/src/types/interfaces/i-style-data.ts b/packages/core/src/types/interfaces/i-style-data.ts
index 538f93ab5a..3995f1cfe7 100644
--- a/packages/core/src/types/interfaces/i-style-data.ts
+++ b/packages/core/src/types/interfaces/i-style-data.ts
@@ -208,7 +208,8 @@ export interface IStyleData extends IStyleBase {
*/
tr?: Nullable<ITextRotation>;
/**
- * textDirection
+ * @internal
+ * textDirection @TODO
*/
td?: Nullable<TextDirection>;
/**
@@ -224,7 +225,8 @@ export interface IStyleData extends IStyleBase {
*/
tb?: Nullable<WrapStrategy>;
/**
- * padding
+ * @internal
+ * padding @TODO
*/
pd?: Nullable<IPaddingData>;
diff --git a/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts b/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts
index 60aea4115c..6ab9699792 100644
--- a/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts
+++ b/packages/engine-render/src/components/sheets/sheet.render-skeleton.ts
@@ -45,7 +45,6 @@ import {
addLinkToDocumentModel,
BooleanNumber,
CellValueType,
- composeStyles,
DEFAULT_STYLES,
extractPureTextFromCell,
getColorStyle,
@@ -486,7 +485,7 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
// TODO@weird94: in future, we should use `getCellRaw` instead of `getCell` to improve performance.
const cell = this.worksheet.getCell(row, col);
- const style = this._styles.getStyleByCell(cell);
+ const style = this.worksheet.getComposedCellStyle(row, col);
if (cell?.interceptorAutoHeight) {
const cellHeight = cell.interceptorAutoHeight();
@@ -516,7 +515,7 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
colWidth -= sideGap;
if (isRichText) {
- const modelObject = cell && this._getCellDocumentModel(cell);
+ const modelObject = cell && this.worksheet.getCellDocumentModel(cell, style);
if (modelObject == null) {
return undefined;
}
@@ -699,7 +698,7 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
if (row + MEASURE_EXTENT_FOR_PARAGRAPH <= startRowOfViewMain || row - MEASURE_EXTENT_FOR_PARAGRAPH >= endRowOfViewMain) continue;
}
- let measuredWidth = this._getMeasuredWidthByCell(cell, currColWidth);
+ let measuredWidth = this._getMeasuredWidthByCell(cell, row, colIndex, currColWidth);
if (cell.fontRenderExtension) {
measuredWidth += ((cell.fontRenderExtension?.leftOffset || 0) + (cell.fontRenderExtension?.rightOffset || 0));
}
@@ -729,7 +728,7 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
* @param cell
* @returns {number} currColWidth
*/
- _getMeasuredWidthByCell(cell: ICellDataForSheetInterceptor, currColWidth: number) {
+ _getMeasuredWidthByCell(cell: ICellDataForSheetInterceptor, row: number, column: number, currColWidth: number) {
let measuredWidth = 0;
// isSkip means the text in this cell would not rendering.
@@ -739,8 +738,8 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
return cellWidth;
}
}
-
- const modelObject = this._getCellDocumentModel(cell);
+ const style = this.worksheet.getComposedCellStyle(row, column);
+ const modelObject = this.worksheet.getCellDocumentModel(cell, style);
if (modelObject == null) {
return measuredWidth;
}
@@ -752,9 +751,8 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
const documentViewModel = new DocumentViewModel(documentModel);
const { vertexAngle: angle } = convertTextRotation(textRotation);
- const cellStyle = this._styles.getStyleByCell(cell);
- if (cellStyle?.tb === WrapStrategy.WRAP) {
+ if (style?.tb === WrapStrategy.WRAP) {
documentModel.updateDocumentDataPageSize(currColWidth, Infinity);
} else {
documentModel.updateDocumentDataPageSize(Infinity, Infinity);
@@ -1333,7 +1331,7 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
}
}
- _setFontStylesCache(row: number, col: number, cellData: Nullable<ICellDataForSheetInterceptor>) {
+ _setFontStylesCache(row: number, col: number, cellData: Nullable<ICellDataForSheetInterceptor>, style: IStyleData) {
if (isNullCell(cellData)) return;
let config: Partial<IFontCacheItem> = {
@@ -1350,12 +1348,11 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
this._stylesCache.fontMatrix.setValue(row, col, cacheValue as IFontCacheItem);
return;
}
- const style = this._styles.getStyleByCell(cellData);
const { vertexAngle, centerAngle } = convertTextRotation(style?.tr ?? { a: 0 });
const isRichText = cellData?.p || vertexAngle || centerAngle;
const modelObject = isRichText ?
- this._getCellDocumentModel(cellData, { displayRawFormula: this._renderRawFormula })
+ this.worksheet.getCellDocumentModel(cellData, style, { displayRawFormula: this._renderRawFormula })
: null;
if (modelObject) {
@@ -1426,7 +1423,10 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
}
const { isMerged, isMergedMainCell, startRow, startColumn, endRow, endColumn } = this.worksheet.getCellInfoInMergeData(row, col);
- options.mergeRange = { startRow, startColumn, endRow, endColumn };
+
+ if (isMerged) {
+ options.mergeRange = { startRow, startColumn, endRow, endColumn };
+ }
const hidden = this.worksheet.getColVisible(col) === false || this.worksheet.getRowVisible(row) === false;
@@ -1442,18 +1442,11 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
}
const cell = this.worksheet.getCell(row, col) || this.worksheet.getCellRaw(row, col);
- const cellStyle = this._styles.getStyleByCell(cell);
- const columnStyle = this.worksheet.getColumnStyle(col);
- const rowStyle = this.worksheet.getRowStyle(row);
- const defaultStyle = this.worksheet.getDefaultCellStyleInternal();
-
- const style = this._isRowStylePrecedeColumnStyle
- ? composeStyles(defaultStyle, columnStyle, rowStyle, (cell as ICellDataForSheetInterceptor)?.themeStyle, cellStyle)
- : composeStyles(defaultStyle, rowStyle, columnStyle, (cell as ICellDataForSheetInterceptor)?.themeStyle, cellStyle);
+ const style = this.worksheet.getComposedCellStyle(row, col);
this._setBgStylesCache(row, col, style, options);
this._setBorderStylesCache(row, col, style, options);
- this._setFontStylesCache(row, col, { ...cell, ...{ s: style } });
+ this._setFontStylesCache(row, col, { ...cell, ...{ s: style } }, style);
}
/**
@@ -1507,7 +1500,7 @@ export class SpreadsheetSkeleton extends SheetSkeleton {
const themeStyleBackground = cell.themeStyle?.bd;
- const style = this._styles.getStyleByCell(cell);
+ const style = this.worksheet.getComposedCellStyle(row, column);
if (!style && !themeStyleBackground) {
isAddBorders = false;
break;
diff --git a/packages/sheets-hyper-link/src/commands/commands/add-hyper-link.command.ts b/packages/sheets-hyper-link/src/commands/commands/add-hyper-link.command.ts
index 92b9cb6b31..43e0e4b54f 100644
--- a/packages/sheets-hyper-link/src/commands/commands/add-hyper-link.command.ts
+++ b/packages/sheets-hyper-link/src/commands/commands/add-hyper-link.command.ts
@@ -54,7 +54,7 @@ export const AddHyperLinkCommand: ICommand<IAddHyperLinkCommandParams> = {
const { payload, display, row, column, id } = link;
const cellData = worksheet.getCell(row, column);
- const doc = worksheet.getBlankCellDocumentModel(cellData);
+ const doc = worksheet.getBlankCellDocumentModel(cellData, row, column);
const snapshot = doc.documentModel!.getSnapshot();
const body = Tools.deepClone(snapshot.body);
if (!body) return false;
diff --git a/packages/sheets-hyper-link/src/commands/commands/remove-hyper-link.command.ts b/packages/sheets-hyper-link/src/commands/commands/remove-hyper-link.command.ts
index fd6e8f9b82..032d5b2359 100644
--- a/packages/sheets-hyper-link/src/commands/commands/remove-hyper-link.command.ts
+++ b/packages/sheets-hyper-link/src/commands/commands/remove-hyper-link.command.ts
@@ -56,7 +56,7 @@ export const CancelHyperLinkCommand: ICommand<ICancelHyperLinkCommandParams> = {
const cellData = worksheet.getCell(row, column);
if (!cellData) return false;
- const doc = worksheet.getCellDocumentModelWithFormula(cellData);
+ const doc = worksheet.getCellDocumentModelWithFormula(cellData, row, column);
if (!doc?.documentModel) return false;
const snapshot = Tools.deepClone(doc.documentModel!.getSnapshot());
diff --git a/packages/sheets-hyper-link/src/commands/commands/update-hyper-link.command.ts b/packages/sheets-hyper-link/src/commands/commands/update-hyper-link.command.ts
index 9396d5ebd6..344c0b3aa4 100644
--- a/packages/sheets-hyper-link/src/commands/commands/update-hyper-link.command.ts
+++ b/packages/sheets-hyper-link/src/commands/commands/update-hyper-link.command.ts
@@ -59,7 +59,7 @@ export const UpdateHyperLinkCommand: ICommand<IUpdateHyperLinkCommandParams> = {
const cellData = worksheet.getCell(row, column);
if (!cellData) return false;
- const doc = worksheet.getCellDocumentModelWithFormula(cellData);
+ const doc = worksheet.getCellDocumentModelWithFormula(cellData, row, column);
if (!doc?.documentModel) return false;
const snapshot = doc.documentModel.getSnapshot();
diff --git a/packages/sheets-hyper-link/src/controllers/set-range.controller.ts b/packages/sheets-hyper-link/src/controllers/set-range.controller.ts
index e686982ccb..e367827df6 100644
--- a/packages/sheets-hyper-link/src/controllers/set-range.controller.ts
+++ b/packages/sheets-hyper-link/src/controllers/set-range.controller.ts
@@ -146,7 +146,7 @@ export class SheetHyperLinkSetRangeController extends Disposable {
}
if (typeof cell.v === 'string' && Tools.isLegalUrl(cell.v) && cell.v[cell.v.length - 1] !== ' ') {
- const { unitId, subUnitId } = context;
+ const { unitId, subUnitId, row, col } = context;
const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
const worksheet = workbook?.getSheetBySheetId(subUnitId);
@@ -158,7 +158,7 @@ export class SheetHyperLinkSetRangeController extends Disposable {
// if (!skeleton) {
// return next(cell);
// }
- const doc = worksheet.getBlankCellDocumentModel(cell);
+ const doc = worksheet.getBlankCellDocumentModel(cell, row, col);
if (!doc.documentModel) {
return next(cell);
}
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 00c337fff8..d21380d0ac 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -257,13 +257,9 @@ export class SheetClipboardController extends RxDisposable {
properties.colspan = `${colSpan || 1}`;
}
- // TODO@wzhudev: should deprecate Range and
- // use worksheet.getStyle()
- const range = currentSheet!.getRange(row, col);
-
const mergedCellByRowCol = currentSheet!.getMergedCell(row, col);
- const textStyle = range.getTextStyle();
+ const textStyle = currentSheet!.getComposedCellStyle(row, col);
let style = '';
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-with-default-style.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-with-default-style.spec.ts
new file mode 100644
index 0000000000..81da416352
--- /dev/null
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-copy-with-default-style.spec.ts
@@ -0,0 +1,157 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 type { Injector, Univer } from '@univerjs/core';
+import type { ISetSelectionsOperationParams } from '@univerjs/sheets';
+import { ICommandService, LocaleType, RANGE_TYPE, set, ThemeService } from '@univerjs/core';
+import {
+ AddWorksheetMergeMutation,
+ RemoveWorksheetMergeMutation,
+ SetRangeValuesMutation,
+ SetSelectionsOperation,
+ SetWorksheetColWidthMutation,
+ SetWorksheetRowHeightMutation,
+} from '@univerjs/sheets';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { SheetCopyCommand } from '../../../commands/commands/clipboard.command';
+import { ISheetClipboardService } from '../clipboard.service';
+import { clipboardTestBed } from './clipboard-test-bed';
+
+describe('Test clipboard', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+ let sheetClipboardService: ISheetClipboardService;
+ let themeService: ThemeService;
+
+ beforeEach(async () => {
+ const testBed = clipboardTestBed({
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ defaultStyle: {
+ fs: 20,
+ cl: {
+ rgb: 'red',
+ },
+ bd: {
+ t: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ b: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ l: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ r: {
+ s: 1,
+ cl: {
+ rgb: '#000',
+ },
+ },
+ },
+ },
+ cellData: {
+ 0: {
+ 0: {
+ v: 1,
+ },
+ },
+ 1: {
+ 0: {
+ v: 2,
+ },
+ },
+ 2: {
+ 0: {
+ v: 3,
+ },
+ },
+ 3: {
+ 0: {
+ v: 4,
+ },
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+ });
+
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(SetRangeValuesMutation);
+ commandService.registerCommand(SetWorksheetRowHeightMutation);
+ commandService.registerCommand(SetWorksheetColWidthMutation);
+ commandService.registerCommand(AddWorksheetMergeMutation);
+ commandService.registerCommand(RemoveWorksheetMergeMutation);
+ commandService.registerCommand(SetSelectionsOperation);
+
+ sheetClipboardService = get(ISheetClipboardService);
+
+ themeService = get(ThemeService);
+ const theme = themeService.getCurrentTheme();
+ const newTheme = set(theme, 'black', '#35322b');
+ themeService.setTheme(newTheme);
+ });
+
+ afterEach(() => {
+ univer?.dispose();
+ });
+
+ describe('Copy cells of a worksheet with default styles', () => {
+ it('worksheet has default style', async () => {
+ const selection = {
+ range: { startRow: 0, startColumn: 0, endRow: 3, endColumn: 0, rangeType: RANGE_TYPE.NORMAL },
+ primary: { actualRow: 0, actualColumn: 0, isMerged: false, isMergedMainCell: false },
+ style: null,
+ };
+ expect(await commandService.executeCommand(SetSelectionsOperation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ selections: [selection],
+ } as ISetSelectionsOperationParams)).toBeTruthy();
+
+ expect(await commandService.executeCommand(SheetCopyCommand.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ })).toBeTruthy();
+
+ const { html, copyId } = sheetClipboardService.generateCopyContent('test', 'sheet1', selection.range)!;
+ const result = `<google data-copy-id="${copyId}"-sheets-html-origin><table xmlns="http://www.w3.org/1999/xhtml" cellspacing="0" cellpadding="0" dir="ltr" style="table-layout:fixed;font-size:10pt;font-family:Arial;width:0px;border-collapse:collapse;border:none"><colgroup><col width="88" /></colgroup>
+<tbody><tr style="height: 24px;"><td style="font-size: 20pt; color: red; border-bottom: 0.5pt solid #000; border-top: 0.5pt solid #000; border-right: 0.5pt solid #000; border-left: 0.5pt solid #000; ">1</td></tr><tr style="height: 24px;"><td style="font-size: 20pt; color: red; border-bottom: 0.5pt solid #000; border-top: 0.5pt solid #000; border-right: 0.5pt solid #000; border-left: 0.5pt solid #000; ">2</td></tr><tr style="height: 24px;"><td style="font-size: 20pt; color: red; border-bottom: 0.5pt solid #000; border-top: 0.5pt solid #000; border-right: 0.5pt solid #000; border-left: 0.5pt solid #000; ">3</td></tr><tr style="height: 24px;"><td style="font-size: 20pt; color: red; border-bottom: 0.5pt solid #000; border-top: 0.5pt solid #000; border-right: 0.5pt solid #000; border-left: 0.5pt solid #000; ">4</td></tr></tbody></table>`;
+ expect(html).toBe(result);
+ });
+ });
+});
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 3bed741851..c24e80c85c 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -640,13 +640,15 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
return false;
}
- const styles = this._univerInstanceService.getUniverSheetInstance(copyUnitId)?.getStyles();
+ const { mapFunc } = virtualizeDiscreteRanges([range]);
+ const worksheet = this._univerInstanceService.getUniverSheetInstance(copyUnitId)?.getSheetBySheetId(copySubUnitId);
+
cellMatrix.forValue((row, col, value) => {
- if (typeof value.s === 'string') {
- const newValue = Tools.deepClone(value);
- newValue.s = styles?.getStyleByCell(value);
- cellMatrix.setValue(row, col, newValue);
- }
+ const { row: actualRow, col: actualColumn } = mapFunc(row, col);
+ const style = worksheet?.getComposedCellStyle(actualRow, actualColumn);
+ const newValue = Tools.deepClone(value);
+ newValue.s = style;
+ cellMatrix.setValue(row, col, newValue);
if (value.colSpan || value.rowSpan) {
for (let rStart = 0; rStart < value.rowSpan!; rStart++) {
@@ -655,7 +657,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const r = row + rStart;
const c = col + cStart;
- cellMatrix.setValue(r, c, { s: styles?.getStyleByCell(value) });
+ cellMatrix.setValue(r, c, { s: style });
}
}
}
diff --git a/packages/sheets-ui/src/services/editor-bridge.service.ts b/packages/sheets-ui/src/services/editor-bridge.service.ts
index 9df72e5653..385b8b900a 100644
--- a/packages/sheets-ui/src/services/editor-bridge.service.ts
+++ b/packages/sheets-ui/src/services/editor-bridge.service.ts
@@ -356,7 +356,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
location
);
- documentLayoutObject = cell && skeleton.getCellDocumentModelWithFormula(cell);
+ documentLayoutObject = cell && worksheet.getCellDocumentModelWithFormula(cell, location.row, location.col);
// Rewrite the cellValueType to STRING to avoid render the value on the right side when number type.
const renderConfig = documentLayoutObject?.documentModel?.documentStyle.renderConfig;
@@ -365,7 +365,7 @@ export class EditorBridgeService extends Disposable implements IEditorBridgeServ
}
if (!documentLayoutObject || documentLayoutObject.documentModel == null) {
- const blankModel = skeleton.getBlankCellDocumentModel(cell);
+ const blankModel = worksheet.getBlankCellDocumentModel(cell, location.row, location.col);
if (documentLayoutObject != null) {
const { verticalAlign, horizontalAlign, wrapStrategy, textRotation, fill } = documentLayoutObject;