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(paste): fix the style error when copying cells with conditional formatting, data validation, number formatting, etc. (#6326)

wpxp12345615456740cf1e
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 7865220df2..9a772001cc 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -261,7 +261,10 @@ export class SheetClipboardController extends RxDisposable {
}
const mergedCellByRowCol = currentSheet!.getMergedCell(row, col);
-
+ /**
+ * This is for generating the copied HTML, which requires all the styles applied to the cell, including conditional formatting, data validation, number format, etc.
+ * So here we use `getComposedCellStyle` to get the final style of the cell.
+ */
const textStyle = currentSheet!.getComposedCellStyle(row, col);
let style = '';
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 45a2514ee2..c2429d5170 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -665,7 +665,12 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const cellMatrix = new ObjectMatrix<ICellDataWithSpanInfo>();
cachedMatrix.forValue((row, col, value) => {
const { row: actualRow, col: actualColumn } = mapFunc(row, col);
- const style = worksheet?.getComposedCellStyle(actualRow, actualColumn);
+ /**
+ * This only needs to include a combination of default styles, row and column styles, and cell styles.
+ * Conditional formatting, data validation, and number format are not included here because they will be applied automatically after pasting.
+ * So here we use `getComposedCellStyleByCellData` to get the final style of the cell raw data.
+ */
+ const style = worksheet?.getComposedCellStyleByCellData(actualRow, actualColumn, value);
const newValue = cloneCellDataWithSpanInfo(value)!;
newValue.s = style;
cellMatrix.setValue(row, col, newValue);