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(editor): fix the issue of incorrect cell styles after editing (#6435)

wpxp12345684dbabbab4c5
diff --git a/packages/core/src/sheets/worksheet.ts b/packages/core/src/sheets/worksheet.ts
index 28983b40cc..adfa753953 100644
--- a/packages/core/src/sheets/worksheet.ts
+++ b/packages/core/src/sheets/worksheet.ts
@@ -28,6 +28,7 @@ import { createRowColIter } from '../shared/row-col-iter';
import { generateRandomId } from '../shared/tools';
import { DEFAULT_STYLES } from '../types/const';
import { CellValueType } from '../types/enum';
+import { cloneWorksheetData } from './clone';
import { ColumnManager } from './column-manager';
import { Range } from './range';
import { RowManager } from './row-manager';
@@ -36,7 +37,6 @@ import { SpanModel } from './span-model';
import { CellModeEnum } from './typedef';
import { addLinkToDocumentModel, createDocumentModelWithStyle, DEFAULT_PADDING_DATA, extractOtherStyle, getFontFormat, isNotNullOrUndefined } from './util';
import { SheetViewModel } from './view-model';
-import { cloneWorksheetData } from './clone';
export interface IDocumentLayoutObject {
documentModel: Nullable<DocumentDataModel>;
@@ -290,6 +290,34 @@ export class Worksheet {
: composeStyles(defaultStyle, rowStyle, colStyle, cellData?.themeStyle, cellStyle);
}
+ /**
+ * Get the composed style of the cell without its own style.
+ * @param {number} row The row index of the cell
+ * @param {number} col The column index of the cell
+ * @param {Nullable<ICellDataForSheetInterceptor>} [cellData] The cell data of the cell.
+ * @param {boolean} [rowPriority] If true, row style will precede column style, otherwise use this._isRowStylePrecedeColumnStyle
+ * @returns {IStyleData} The composed style of the cell without its own style
+ */
+ getComposedCellStyleWithoutSelf(row: number, col: number, cellData?: Nullable<ICellDataForSheetInterceptor>, rowPriority?: boolean): IStyleData {
+ const composedCellStyle = cellData === undefined
+ ? this.getComposedCellStyle(row, col, rowPriority)
+ : this.getComposedCellStyleByCellData(row, col, cellData, rowPriority);
+
+ const cellDataRaw = this.getCellRaw(row, col);
+ if (!cellDataRaw || !cellDataRaw.s) return composedCellStyle;
+
+ const style = typeof cellDataRaw.s === 'string' ? this._styles.get(cellDataRaw.s) : cellDataRaw.s;
+ if (!style) return composedCellStyle;
+
+ for (const key in style) {
+ if (key in composedCellStyle) {
+ delete composedCellStyle[key as keyof IStyleData];
+ }
+ }
+
+ return composedCellStyle;
+ }
+
/**
* Returns WorkSheet Cell Data Matrix
* @returns WorkSheet Cell Data Matrix
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index 26d306cb7f..f942e9f597 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -627,7 +627,7 @@ export class EditingRenderController extends Disposable {
}
// Remove the same style attributes that have been set by composed style in the cell data.
- this._removeComposedCellStyleInCellData(cellData, worksheet.getComposedCellStyle(row, column));
+ this._removeComposedCellStyleInCellData(cellData, worksheet.getComposedCellStyleWithoutSelf(row, column));
const finalCell = this._sheetInterceptorService.onWriteCell(workbook, worksheet, row, column, cellData) as ICellData;
if (Tools.diffValue(cleanCellDataObject(finalCell), cleanCellDataObject(worksheet.getCellRaw(row, column)))) {