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(sheet): set column width error (#697)

Univer52757788c16e
diff --git a/packages/core/src/sheets/column-manager.ts b/packages/core/src/sheets/column-manager.ts
index 477304cd1b..87d8183375 100644
--- a/packages/core/src/sheets/column-manager.ts
+++ b/packages/core/src/sheets/column-manager.ts
@@ -94,33 +94,16 @@ export class ColumnManager {
* @param columnPos column index
* @returns
*/
- getColumnWidth(columnPos: number): number;
- /**
- * Get the width of column
- * @param columnPos column index
- * @param numColumns column count
- * @returns
- */
- getColumnWidth(columnPos: number, numColumns: number): number;
- getColumnWidth(...argument: any): number {
+ getColumnWidth(columnPos: number): number {
const { _columnData } = this;
const config = this._config;
let width: number = 0;
- if (argument.length === 1) {
- const column = _columnData.obtain(argument[0], {
- hd: BooleanNumber.FALSE,
- w: config.defaultColumnWidth,
- });
- width = column.w;
- } else if (argument.length === 2) {
- for (let i = argument[0]; i < argument[0] + argument[1]; i++) {
- const column = _columnData.obtain(i, {
- hd: BooleanNumber.FALSE,
- w: config.defaultColumnWidth,
- });
- width += column.w;
- }
- }
+
+ const column = _columnData.obtain(columnPos, {
+ hd: BooleanNumber.FALSE,
+ w: config.defaultColumnWidth,
+ });
+ width = column.w || config.defaultColumnWidth;
return width;
}
diff --git a/packages/core/src/sheets/row-manager.ts b/packages/core/src/sheets/row-manager.ts
index cd2db34da0..d3da60b8b7 100644
--- a/packages/core/src/sheets/row-manager.ts
+++ b/packages/core/src/sheets/row-manager.ts
@@ -48,7 +48,7 @@ export class RowManager {
hd: BooleanNumber.FALSE,
h: config.defaultRowHeight,
});
- const { isAutoHeight, ah, h } = row;
+ const { isAutoHeight, ah, h = config.defaultRowHeight } = row;
height += (isAutoHeight == null || !!isAutoHeight) && typeof ah === 'number' ? ah : h;
}
diff --git a/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts
index fd6b4ba6ba..e9e1bba213 100644
--- a/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts
+++ b/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts
@@ -1,5 +1,14 @@
import type { IWorkbookData, Workbook } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
+import {
+ BooleanNumber,
+ ILogService,
+ IUniverInstanceService,
+ LocaleType,
+ LogLevel,
+ Plugin,
+ PluginType,
+ Univer,
+} from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
@@ -23,6 +32,16 @@ const TEST_WORKBOOK_DATA_DEMO: IWorkbookData = {
},
},
},
+ columnData: {
+ 1: {
+ hd: BooleanNumber.FALSE,
+ },
+ },
+ rowData: {
+ 1: {
+ hd: BooleanNumber.FALSE,
+ },
+ },
},
},
locale: LocaleType.ZH_CN,
diff --git a/packages/sheets/src/commands/commands/__tests__/set-col-width.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-col-width.command.spec.ts
index 9a8ea3d8f7..bcd064fec3 100644
--- a/packages/sheets/src/commands/commands/__tests__/set-col-width.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/set-col-width.command.spec.ts
@@ -89,7 +89,7 @@ describe('Test set col width commands', () => {
});
it('Should expand only the anchor col in other situations', () => {
- expect(getColumnWidth(1)).toBe(73);
+ expect(getColumnWidth(1)).toBe(50);
expect(getColumnWidth(7)).toBe(73);
commandService.executeCommand<IDeltaColumnWidthCommandParams>(DeltaColumnWidthCommand.id, {
@@ -97,15 +97,15 @@ describe('Test set col width commands', () => {
anchorCol: 7,
});
- expect(getColumnWidth(1)).toBe(73);
- expect(getColumnWidth(2)).toBe(73);
- expect(getColumnWidth(5)).toBe(73);
+ expect(getColumnWidth(1)).toBe(50);
+ expect(getColumnWidth(2)).toBe(50);
+ expect(getColumnWidth(5)).toBe(50);
expect(getColumnWidth(7)).toBe(50);
});
});
it('Direct change col widths', async () => {
- expect(getColumnWidth(1)).toBe(73);
+ expect(getColumnWidth(1)).toBe(50);
await commandService.executeCommand<ISetColWidthCommandParams>(SetColWidthCommand.id, {
value: 40,
@@ -116,7 +116,7 @@ describe('Test set col width commands', () => {
await commandService.executeCommand(UndoCommand.id);
expect(getColumnWidth(1)).toBe(73);
- expect(getColumnWidth(2)).toBe(73);
+ expect(getColumnWidth(2)).toBe(50);
expect(getColumnWidth(5)).toBe(73);
await commandService.executeCommand(RedoCommand.id);
diff --git a/packages/sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts
index 678e15a560..d46268e0a5 100644
--- a/packages/sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts
@@ -114,22 +114,22 @@ describe('Test set row height commands', () => {
});
it('Should expand only the anchor row in other situations', async () => {
- expect(getRowHeight(1)).toBe(19);
+ expect(getRowHeight(1)).toBe(14);
expect(getRowHeight(7)).toBe(19);
await commandService.executeCommand<IDeltaRowHeightCommand>(DeltaRowHeightCommand.id, {
deltaY: -5,
anchorRow: 7,
});
- expect(getRowHeight(1)).toBe(19);
- expect(getRowHeight(2)).toBe(19);
- expect(getRowHeight(5)).toBe(19);
+ expect(getRowHeight(1)).toBe(14);
+ expect(getRowHeight(2)).toBe(14);
+ expect(getRowHeight(5)).toBe(14);
expect(getRowHeight(7)).toBe(14);
});
});
it('Direct change row heights', async () => {
- expect(getRowHeight(1)).toBe(19);
+ expect(getRowHeight(1)).toBe(14);
await commandService.executeCommand<ISetRowHeightCommandParams>(SetRowHeightCommand.id, {
value: 77,
@@ -142,9 +142,9 @@ describe('Test set row height commands', () => {
expect(getRowIsAutoHeight(5)).toBe(false);
await commandService.executeCommand(UndoCommand.id);
- expect(getRowHeight(1)).toBe(19);
- expect(getRowHeight(2)).toBe(19);
- expect(getRowHeight(5)).toBe(19);
+ expect(getRowHeight(1)).toBe(14);
+ expect(getRowHeight(2)).toBe(14);
+ expect(getRowHeight(5)).toBe(14);
await commandService.executeCommand(RedoCommand.id);
expect(getRowHeight(1)).toBe(77);
@@ -161,7 +161,7 @@ describe('Test set row height commands', () => {
await commandService.executeCommand(UndoCommand.id);
expect(getRowIsAutoHeight(1)).toBe(undefined);
- expect(getRowIsAutoHeight(2)).toBe(undefined);
+ expect(getRowIsAutoHeight(2)).toBe(false);
expect(getRowIsAutoHeight(5)).toBe(undefined);
await commandService.executeCommand(RedoCommand.id);