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: set style should not clear p (#2553)

ybzky9c4ea35cd6cf
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 9a74e41b50..818d32157a 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -933,6 +933,8 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const rowCount = endRow - startRow + 1;
const colCount = endColumn - startColumn + 1;
+ if (rowCount <= 0 || colCount <= 0) return null;
+
const pasteSelectionRangeRowLen = discreteRange.rows.length;
const pasteSelectionRangeColLen = discreteRange.cols.length;
diff --git a/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
index 30941b25a0..4341e5a4f8 100644
--- a/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
@@ -565,6 +565,143 @@ describe('Test set range values commands', () => {
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
expect(getValues(1, 1, 1, 4)).toStrictEqual([[{ s: 's2' }, null, null, { s: 's3' }]]);
});
+
+ it('paste value when origin cell has rich text', async () => {
+ function getParams() {
+ const richTextDemo: IDocumentData = {
+ id: 'd',
+ body: {
+ dataStream: 'Instructions: ①Project division - Fill in the specific division of labor after the project is disassembled: ②Responsible Person - Enter the responsible person\'s name here: ③Date-The specific execution time of the project (detailed to the date of a certain month), and the gray color block marks the planned real-time time of the division of labor of the project (for example, the specific execution time of [regional scene model arrangement and construction] is the 2 days marked in gray. \r\n',
+ textRuns: [
+ {
+ st: 0,
+ ed: 488,
+ ts: {
+ cl: {
+ rgb: 'rgb(92,92,92)',
+ },
+ },
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 489,
+ paragraphStyle: {
+ spaceAbove: 10,
+ lineSpacing: 1.2,
+ },
+ },
+ ],
+ },
+ documentStyle: {
+ pageSize: {
+ width: Number.POSITIVE_INFINITY,
+ height: Number.POSITIVE_INFINITY,
+ },
+ marginTop: 0,
+ marginBottom: 0,
+ marginRight: 2,
+ marginLeft: 2,
+ },
+ };
+
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ p: richTextDemo,
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ };
+
+ return params;
+ }
+
+ expect(await commandService.executeCommand(SetRangeValuesCommand.id, getParams())).toBeTruthy();
+ expect(getValue()).toStrictEqual(getParams().value);
+ expect(await commandService.executeCommand(SetRangeValuesMutation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ cellValue: {
+ 0: {
+ 0: {
+ v: 'hello',
+ s: null,
+ },
+ },
+ },
+ })).toBeTruthy();
+ expect(getValue()?.p).toBeFalsy();
+ });
+
+ it('set styles when origin cell has rich text', async () => {
+ function getParams() {
+ const richTextDemo: IDocumentData = {
+ id: 'd',
+ body: {
+ dataStream: 'Instructions: ①Project division - Fill in the specific division of labor after the project is disassembled: ②Responsible Person - Enter the responsible person\'s name here: ③Date-The specific execution time of the project (detailed to the date of a certain month), and the gray color block marks the planned real-time time of the division of labor of the project (for example, the specific execution time of [regional scene model arrangement and construction] is the 2 days marked in gray. \r\n',
+ textRuns: [
+ {
+ st: 0,
+ ed: 488,
+ ts: {
+ cl: {
+ rgb: 'rgb(92,92,92)',
+ },
+ },
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 489,
+ paragraphStyle: {
+ spaceAbove: 10,
+ lineSpacing: 1.2,
+ },
+ },
+ ],
+ },
+ documentStyle: {
+ pageSize: {
+ width: Number.POSITIVE_INFINITY,
+ height: Number.POSITIVE_INFINITY,
+ },
+ marginTop: 0,
+ marginBottom: 0,
+ marginRight: 2,
+ marginLeft: 2,
+ },
+ };
+
+ const params: ISetRangeValuesCommandParams = {
+ value: {
+ p: richTextDemo,
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ };
+
+ return params;
+ }
+
+ expect(await commandService.executeCommand(SetRangeValuesCommand.id, getParams())).toBeTruthy();
+ expect(getValue()).toStrictEqual(getParams().value);
+ expect(await commandService.executeCommand(SetRangeValuesMutation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ cellValue: {
+ 0: {
+ 0: {
+ s: {
+ bg: {
+ rgb: '#232323',
+ },
+ },
+ },
+ },
+ },
+ })).toBeTruthy();
+ expect(getValue()?.p).toBeTruthy();
+ });
});
describe('fault situations', () => {
diff --git a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
index 071348cc3e..3eba109a04 100644
--- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
+++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts
@@ -242,13 +242,13 @@ export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, bo
oldVal.s = styles.setValue(merge);
}
- const newValueStream = `${newVal.v}\r\n`;
+ const newValueStream = newVal.v ? `${newVal.v}\r\n` : '';
// Only need to copy newValue.s to oldValue.p when you modify the cell style, not when you modify the cell value.
if (!newVal.p && oldVal.p) {
- if (newValueStream === oldVal.p.body?.dataStream) {
- mergeRichTextStyle(oldVal.p, newVal.s ? (newVal.s as Nullable<IStyleData>) : null);
- } else {
+ if (newValueStream && newValueStream !== oldVal.p.body?.dataStream) {
delete oldVal.p;
+ } else {
+ mergeRichTextStyle(oldVal.p, newVal.s ? (newVal.s as Nullable<IStyleData>) : null);
}
}
}