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.

feat(sheet): numfmt support autofill (#573)

Gpound.liucc3f2778507d
diff --git a/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts b/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts
index 95299f0646..7cb64c27f4 100644
--- a/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts
+++ b/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts
@@ -23,6 +23,7 @@ import {
Disposable,
DisposableCollection,
ICommandService,
+ IMutationInfo,
INTERCEPTOR_POINT,
IRange,
IUniverInstanceService,
@@ -30,11 +31,18 @@ import {
LocaleType,
Nullable,
Range,
+ Rectangle,
SheetInterceptorService,
ThemeService,
toDisposable,
} from '@univerjs/core';
-import { SheetSkeletonManagerService } from '@univerjs/ui-plugin-sheets';
+import {
+ APPLY_TYPE,
+ getRepeatRange,
+ IAutoFillHook,
+ IAutoFillService,
+ SheetSkeletonManagerService,
+} from '@univerjs/ui-plugin-sheets';
import { IDisposable, Inject, Injector } from '@wendellhu/redi';
import { combineLatest, Observable } from 'rxjs';
import { bufferTime, distinctUntilChanged, filter, map, switchMap, tap } from 'rxjs/operators';
@@ -93,7 +101,8 @@ export class NumfmtController extends Disposable implements INumfmtController {
@Inject(ComponentManager) private _componentManager: ComponentManager,
@Inject(IMenuService) private _menuService: IMenuService,
@Inject(ISidebarService) private _sidebarService: ISidebarService,
- @Inject(LocaleService) private _localeService: LocaleService
+ @Inject(LocaleService) private _localeService: LocaleService,
+ @Inject(IAutoFillService) private _autoFillService: IAutoFillService
) {
super();
this._initInterceptorCellContent();
@@ -106,6 +115,7 @@ export class NumfmtController extends Disposable implements INumfmtController {
this._initLocal();
this._initPanel();
this._initCommands();
+ this._initAutoFill();
}
openPanel = () => {
@@ -180,6 +190,121 @@ export class NumfmtController extends Disposable implements INumfmtController {
});
};
+ private _initAutoFill() {
+ const noopReturnFunc = () => ({ redos: [], undos: [] });
+ const loopFunc = (
+ sourceStartCell: { row: number; col: number },
+ targetStartCell: { row: number; col: number },
+ relativeRange: IRange
+ ) => {
+ const workbookId = this._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();
+ const worksheetId = this._univerInstanceService
+ .getCurrentUniverSheetInstance()
+ .getActiveSheet()
+ .getSheetId();
+ const sourceRange = {
+ startRow: sourceStartCell.row,
+ startColumn: sourceStartCell.col,
+ endColumn: sourceStartCell.col,
+ endRow: sourceStartCell.row,
+ };
+ const targetRange = {
+ startRow: targetStartCell.row,
+ startColumn: targetStartCell.col,
+ endColumn: targetStartCell.col,
+ endRow: targetStartCell.row,
+ };
+
+ const values: SetNumfmtMutationParams['values'] = [];
+
+ Range.foreach(relativeRange, (row, col) => {
+ const sourcePositionRange = Rectangle.getPositionRange(
+ {
+ startRow: row,
+ startColumn: col,
+ endColumn: col,
+ endRow: row,
+ },
+ sourceRange
+ );
+ const oldNumfmtValue = this._numfmtService.getValue(
+ workbookId,
+ worksheetId,
+ sourcePositionRange.startRow,
+ sourcePositionRange.startColumn
+ );
+ if (oldNumfmtValue) {
+ const targetPositionRange = Rectangle.getPositionRange(
+ {
+ startRow: row,
+ startColumn: col,
+ endColumn: col,
+ endRow: row,
+ },
+ targetRange
+ );
+ values.push({
+ pattern: oldNumfmtValue.pattern,
+ type: oldNumfmtValue.type,
+ row: targetPositionRange.startRow,
+ col: targetPositionRange.startColumn,
+ });
+ }
+ });
+ if (values.length) {
+ const redo: IMutationInfo<SetNumfmtMutationParams> = {
+ id: SetNumfmtMutation.id,
+ params: {
+ values,
+ workbookId,
+ worksheetId,
+ },
+ };
+ const undo = {
+ id: SetNumfmtMutation.id,
+ params: {
+ values: factorySetNumfmtUndoMutation(this._injector, redo.params),
+ workbookId,
+ worksheetId,
+ },
+ };
+ return {
+ redos: [redo],
+ undos: [undo],
+ };
+ }
+ return { redos: [], undos: [] };
+ };
+ const generalApplyFunc = (sourceRange: IRange, targetRange: IRange) => {
+ const totalUndos: IMutationInfo[] = [];
+ const totalRedos: IMutationInfo[] = [];
+ const sourceStartCell = {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn,
+ };
+ const repeats = getRepeatRange(sourceRange, targetRange);
+ repeats.forEach((repeat) => {
+ const { undos, redos } = loopFunc(sourceStartCell, repeat.repeatStartCell, repeat.relativeRange);
+ totalUndos.push(...undos);
+ totalRedos.push(...redos);
+ });
+ return {
+ undos: totalUndos,
+ redos: totalRedos,
+ };
+ };
+ const hook: IAutoFillHook = {
+ hookName: SHEET_NUMFMT_PLUGIN,
+ hook: {
+ [APPLY_TYPE.COPY]: generalApplyFunc,
+ [APPLY_TYPE.NO_FORMAT]: noopReturnFunc,
+ [APPLY_TYPE.ONLY_FORMAT]: generalApplyFunc,
+ [APPLY_TYPE.SERIES]: generalApplyFunc,
+ },
+ };
+ this.disposeWithMe(this._autoFillService.addHook(hook));
+ }
+
private _initCommands() {
[
AddDecimalCommand,
diff --git a/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts b/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
index 659d715922..658211f644 100644
--- a/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/auto-fill.controller.ts
@@ -641,33 +641,11 @@ export class AutoFillController extends Disposable {
const extendRedos: IMutationInfo[] = [];
const extendUndos: IMutationInfo[] = [];
const hookApplyType: APPLY_TYPE = this._hasFillingStyle ? applyType : APPLY_TYPE.NO_FORMAT;
- const repeats = this._getRepeatRange(sourceRange, applyRange, direction);
hooks.forEach((h) => {
const { hook } = h;
- if (hookApplyType === APPLY_TYPE.COPY) {
- repeats.forEach((repeat) => {
- const { repeatStartCell, relativeRange } = repeat;
- const { redos, undos } = hook?.[hookApplyType](
- { row: sourceRange.startRow, col: sourceRange.startColumn },
- repeatStartCell,
- relativeRange
- );
- redos.forEach((redo) => {
- extendRedos.push(redo);
- });
- undos.forEach((undo) => {
- extendUndos.push(undo);
- });
- });
- } else {
- const { redos, undos } = hook?.[hookApplyType](sourceRange, applyRange);
- redos.forEach((redo) => {
- extendRedos.push(redo);
- });
- undos.forEach((undo) => {
- extendUndos.push(undo);
- });
- }
+ const { redos, undos } = hook?.[hookApplyType](sourceRange, applyRange);
+ extendRedos.push(...redos);
+ extendUndos.push(...undos);
});
this._commandService.executeCommand(AutoFillCommand.id, {
@@ -695,137 +673,4 @@ export class AutoFillController extends Disposable {
return res;
});
}
-
- private _getRepeatRange(sourceRange: IRange, targetRange: IRange, direction: Direction) {
- const repeats: Array<{
- repeatStartCell: { col: number; row: number };
- relativeRange: IRange;
- }> = [];
- // according to direction, calculate every repeat range.
- // repeatStartCell is the start cell of each repeat range, relativeRange is the relative range of each repeat range
- if (direction === Direction.DOWN || direction === Direction.UP) {
- const sourceLength = sourceRange.endRow - sourceRange.startRow + 1;
- const targetLength = targetRange.endRow - targetRange.startRow + 1;
- const mod = targetLength / sourceLength;
- const rest = targetLength % sourceLength;
- const relativeRange = {
- startRow: 0,
- startColumn: 0,
- endRow: sourceRange.endRow - sourceRange.startRow,
- endColumn: sourceRange.endColumn - sourceRange.startColumn,
- };
- if (direction === Direction.DOWN) {
- for (let i = 0; i < mod; i++) {
- repeats.push({
- repeatStartCell: {
- row: sourceRange.startRow + (i + 1) * sourceLength,
- col: sourceRange.startColumn,
- },
- relativeRange,
- });
- }
- if (rest > 0) {
- repeats.push({
- repeatStartCell: {
- row: sourceRange.startRow + (mod + 1) * sourceLength,
- col: sourceRange.startColumn,
- },
- relativeRange: {
- startRow: 0,
- startColumn: 0,
- endRow: rest - 1,
- endColumn: sourceRange.endColumn - sourceRange.startColumn,
- },
- });
- }
- } else {
- for (let i = 0; i < mod; i++) {
- repeats.push({
- repeatStartCell: {
- row: sourceRange.startRow - (i + 1) * sourceLength,
- col: sourceRange.startColumn,
- },
- relativeRange,
- });
- }
- if (rest > 0) {
- repeats.push({
- repeatStartCell: {
- row: sourceRange.startRow - (mod + 1) * sourceLength,
- col: sourceRange.startColumn,
- },
- relativeRange: {
- startRow: sourceLength - rest,
- endRow: sourceLength - 1,
- startColumn: 0,
- endColumn: sourceRange.endColumn - sourceRange.startColumn,
- },
- });
- }
- }
- }
- if (direction === Direction.RIGHT || direction === Direction.LEFT) {
- const sourceLength = sourceRange.endColumn - sourceRange.startColumn + 1;
- const targetLength = targetRange.endColumn - targetRange.startColumn + 1;
- const mod = targetLength / sourceLength;
- const rest = targetLength % sourceLength;
- const relativeRange = {
- startRow: 0,
- startColumn: 0,
- endRow: sourceRange.endRow - sourceRange.startRow,
- endColumn: sourceRange.endColumn - sourceRange.startColumn,
- };
- if (direction === Direction.RIGHT) {
- for (let i = 0; i < mod; i++) {
- repeats.push({
- repeatStartCell: {
- row: sourceRange.startRow,
- col: sourceRange.startColumn + (i + 1) * sourceLength,
- },
- relativeRange,
- });
- }
- if (rest > 0) {
- repeats.push({
- repeatStartCell: {
- row: sourceRange.startRow,
- col: sourceRange.startColumn + (mod + 1) * sourceLength,
- },
- relativeRange: {
- startRow: 0,
- startColumn: 0,
- endRow: sourceRange.endRow - sourceRange.startRow,
- endColumn: rest - 1,
- },
- });
- }
- } else {
- for (let i = 0; i < mod; i++) {
- repeats.push({
- repeatStartCell: {
- row: sourceRange.startRow,
- col: sourceRange.startColumn - (i + 1) * sourceLength,
- },
- relativeRange,
- });
- }
- if (rest > 0) {
- repeats.push({
- repeatStartCell: {
- row: sourceRange.startRow,
- col: sourceRange.startColumn - (mod + 1) * sourceLength,
- },
- relativeRange: {
- startRow: 0,
- startColumn: sourceLength - rest,
- endRow: sourceRange.endRow - sourceRange.startRow,
- endColumn: sourceLength - 1,
- },
- });
- }
- }
- }
-
- return repeats;
- }
}
diff --git a/packages/ui-plugin-sheets/src/index.ts b/packages/ui-plugin-sheets/src/index.ts
index 3ca97f205a..f9c1b57932 100644
--- a/packages/ui-plugin-sheets/src/index.ts
+++ b/packages/ui-plugin-sheets/src/index.ts
@@ -13,6 +13,10 @@ export {
getSheetObject,
getTransformCoord,
} from './controllers/utils/component-tools';
+export { IAutoFillService } from './services/auto-fill/auto-fill.service';
+export { getRepeatRange } from './services/auto-fill/tools';
+export type { IAutoFillHook } from './services/auto-fill/type';
+export { APPLY_TYPE, DATA_TYPE } from './services/auto-fill/type';
export { CellEditorManagerService, ICellEditorManagerService } from './services/editor/cell-editor-manager.service';
export {
EditorBridgeService,
diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts b/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts
index d5c975b453..06728748b9 100644
--- a/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts
+++ b/packages/ui-plugin-sheets/src/services/auto-fill/auto-fill.service.ts
@@ -6,8 +6,9 @@ import {
LifecycleStages,
OnLifecycle,
SheetInterceptorService,
+ toDisposable,
} from '@univerjs/core';
-import { createIdentifier, Inject } from '@wendellhu/redi';
+import { createIdentifier, IDisposable, Inject } from '@wendellhu/redi';
import { BehaviorSubject, Observable } from 'rxjs';
import {
@@ -35,6 +36,7 @@ export interface IAutoFillService {
setDisableApplyType: (type: APPLY_TYPE, disable: boolean) => void;
registerRule(rule: IAutoFillRule): void;
getHooks(): IAutoFillHook[];
+ addHook(hook: IAutoFillHook): IDisposable;
}
export interface IApplyMenuItem {
@@ -105,6 +107,12 @@ export class AutoFillService extends Disposable implements IAutoFillService {
throw new Error(`Add hook failed, hook name '${hook.hookName}' already exist!`);
}
this._hooks.push(hook);
+ return toDisposable(() => {
+ const index = this._hooks.findIndex((item) => item === hook);
+ if (index > -1) {
+ this._hooks.splice(index, 1);
+ }
+ });
}
registerRule(rule: IAutoFillRule) {
diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/tools.ts b/packages/ui-plugin-sheets/src/services/auto-fill/tools.ts
index c61b77f6fa..150e7071b6 100644
--- a/packages/ui-plugin-sheets/src/services/auto-fill/tools.ts
+++ b/packages/ui-plugin-sheets/src/services/auto-fill/tools.ts
@@ -1,4 +1,4 @@
-import { Direction, ICellData, Nullable, Tools } from '@univerjs/core';
+import { Direction, ICellData, IRange, Nullable, Tools } from '@univerjs/core';
export const chnNumChar = { 零: 0, 一: 1, 二: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9 };
export const chnNumChar2 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
@@ -665,6 +665,151 @@ export function fillCopyFormula(data: Array<Nullable<ICellData>>, len: number, d
return applyData;
}
+export function getRepeatRange(sourceRange: IRange, targetRange: IRange) {
+ const repeats: Array<{
+ repeatStartCell: { col: number; row: number };
+ relativeRange: IRange;
+ }> = [];
+ let direction: Direction;
+ if (targetRange.startRow < sourceRange.startRow) {
+ direction = Direction.UP;
+ } else if (targetRange.endRow > sourceRange.endRow) {
+ direction = Direction.DOWN;
+ } else if (targetRange.startColumn < sourceRange.startColumn) {
+ direction = Direction.LEFT;
+ } else if (targetRange.endColumn > sourceRange.endColumn) {
+ direction = Direction.RIGHT;
+ } else {
+ return [];
+ }
+ // according to direction, calculate every repeat range.
+ // repeatStartCell is the start cell of each repeat range, relativeRange is the relative range of each repeat range
+ if (direction === Direction.DOWN || direction === Direction.UP) {
+ const sourceLength = sourceRange.endRow - sourceRange.startRow + 1;
+ const targetLength = targetRange.endRow - targetRange.startRow + 1;
+ const mod = targetLength / sourceLength;
+ const rest = targetLength % sourceLength;
+ const relativeRange = {
+ startRow: 0,
+ startColumn: 0,
+ endRow: sourceRange.endRow - sourceRange.startRow,
+ endColumn: sourceRange.endColumn - sourceRange.startColumn,
+ };
+ if (direction === Direction.DOWN) {
+ for (let i = 0; i < mod; i++) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow + (i + 1) * sourceLength,
+ col: sourceRange.startColumn,
+ },
+ relativeRange,
+ });
+ }
+ if (rest > 0) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow + (mod + 1) * sourceLength,
+ col: sourceRange.startColumn,
+ },
+ relativeRange: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: rest - 1,
+ endColumn: sourceRange.endColumn - sourceRange.startColumn,
+ },
+ });
+ }
+ } else {
+ for (let i = 0; i < mod; i++) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow - (i + 1) * sourceLength,
+ col: sourceRange.startColumn,
+ },
+ relativeRange,
+ });
+ }
+ if (rest > 0) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow - (mod + 1) * sourceLength,
+ col: sourceRange.startColumn,
+ },
+ relativeRange: {
+ startRow: sourceLength - rest,
+ endRow: sourceLength - 1,
+ startColumn: 0,
+ endColumn: sourceRange.endColumn - sourceRange.startColumn,
+ },
+ });
+ }
+ }
+ }
+ if (direction === Direction.RIGHT || direction === Direction.LEFT) {
+ const sourceLength = sourceRange.endColumn - sourceRange.startColumn + 1;
+ const targetLength = targetRange.endColumn - targetRange.startColumn + 1;
+ const mod = targetLength / sourceLength;
+ const rest = targetLength % sourceLength;
+ const relativeRange = {
+ startRow: 0,
+ startColumn: 0,
+ endRow: sourceRange.endRow - sourceRange.startRow,
+ endColumn: sourceRange.endColumn - sourceRange.startColumn,
+ };
+ if (direction === Direction.RIGHT) {
+ for (let i = 0; i < mod; i++) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn + (i + 1) * sourceLength,
+ },
+ relativeRange,
+ });
+ }
+ if (rest > 0) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn + (mod + 1) * sourceLength,
+ },
+ relativeRange: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: sourceRange.endRow - sourceRange.startRow,
+ endColumn: rest - 1,
+ },
+ });
+ }
+ } else {
+ for (let i = 0; i < mod; i++) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn - (i + 1) * sourceLength,
+ },
+ relativeRange,
+ });
+ }
+ if (rest > 0) {
+ repeats.push({
+ repeatStartCell: {
+ row: sourceRange.startRow,
+ col: sourceRange.startColumn - (mod + 1) * sourceLength,
+ },
+ relativeRange: {
+ startRow: 0,
+ startColumn: sourceLength - rest,
+ endRow: sourceRange.endRow - sourceRange.startRow,
+ endColumn: sourceLength - 1,
+ },
+ });
+ }
+ }
+ }
+
+ return repeats;
+}
+
function shiftFormula(originalFormula: string, shift: number, direction: Direction): string {
const tokens = tokenizeFormula(originalFormula);
const shiftedTokens = [];
diff --git a/packages/ui-plugin-sheets/src/services/auto-fill/type.ts b/packages/ui-plugin-sheets/src/services/auto-fill/type.ts
index 66b0eaafec..b06a4826fa 100644
--- a/packages/ui-plugin-sheets/src/services/auto-fill/type.ts
+++ b/packages/ui-plugin-sheets/src/services/auto-fill/type.ts
@@ -41,11 +41,7 @@ export interface IAutoFillHook {
[APPLY_TYPE.SERIES]: (sourceRange: IRange, targetRange: IRange) => IMutations;
[APPLY_TYPE.NO_FORMAT]: (sourceRange: IRange, targetRange: IRange) => IMutations;
[APPLY_TYPE.ONLY_FORMAT]: (sourceRange: IRange, targetRange: IRange) => IMutations;
- [APPLY_TYPE.COPY]: (
- sourceStartCell: { row: number; col: number },
- targetStartCell: { row: number; col: number },
- relativeRange: IRange
- ) => IMutations;
+ [APPLY_TYPE.COPY]: (sourceRange: IRange, targetRange: IRange) => IMutations;
};
}