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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(sheets): update namedrange obs
Dushusir77f471bb7057
diff --git a/packages/base-sheets/src/Basics/Register/NamedRangeInsertRowActionExtension.ts b/packages/base-sheets/src/Basics/Register/NamedRangeInsertRowActionExtension.tsindex c132ffc4e4..4d575750d6 100644--- a/packages/base-sheets/src/Basics/Register/NamedRangeInsertRowActionExtension.ts+++ b/packages/base-sheets/src/Basics/Register/NamedRangeInsertRowActionExtension.ts@@ -1,14 +1,49 @@-import { ACTION_NAMES, BaseActionExtension, BaseActionExtensionFactory, ContextBase, IInsertRowActionData, ISheetActionData, Plugin } from '@univer/core';-import { IActionData } from '@univer/core/src/Command/ActionBase';+import { ACTION_NAMES, BaseActionExtension, BaseActionExtensionFactory, Command, IInsertRowActionData, ISetNamedRangeActionData, ISheetActionData, Tools } from '@univer/core';import { SheetPlugin } from '../../SheetPlugin';+/**+ * TODO insertColumn/insertRange/insertRange/deleteRange+ */export class NamedRangeInsertRowActionExtension extends BaseActionExtension<IInsertRowActionData, SheetPlugin> {execute() {const unitId = this._plugin.getContext().getWorkBook().getUnitId();const sheetId = this.actionData.sheetId;const commandManager = this._plugin.getContext().getCommandManager();- const workBook = this._plugin.getContext().getWorkBook();+ const workbook = this._plugin.getContext().getWorkBook();++ const namedRanges = Tools.deepClone(this._plugin.getWorkbook().getConfig().namedRanges);++ const { rowCount, rowIndex } = this.actionData;++ const actionDatas = [];+ for (let i = 0; i < namedRanges.length; i++) {+ const namedRangeData = namedRanges[i].range.rangeData;+ if (rowIndex > namedRangeData.endRow) {+ continue;+ } else if (rowIndex >= namedRangeData.startRow && rowIndex <= namedRangeData.endRow) {+ namedRangeData.endRow += rowCount;+ } else {+ namedRangeData.startRow += rowCount;+ namedRangeData.endRow += rowCount;+ }++ const actionData: ISetNamedRangeActionData = {+ actionName: ACTION_NAMES.SET_NAMED_RANGE_ACTION,+ namedRange: namedRanges[i],+ sheetId,+ };+ actionDatas.push(actionData);+ }++ // Execute action+ const command = new Command(+ {+ WorkBookUnit: workbook,+ },+ ...actionDatas+ );+ commandManager.invoke(command);}}@@ -24,8 +59,4 @@ export class NamedRangeInsertRowActionExtensionFactory extends BaseActionExtensicreate(actionData: IInsertRowActionData, actionDataList: ISheetActionData[]): BaseActionExtension<IInsertRowActionData> {return new NamedRangeInsertRowActionExtension(actionData, actionDataList, this._plugin);}-- check(actionData: IInsertRowActionData, actionDataList: IActionData[]): false | BaseActionExtension<IInsertRowActionData, Plugin<any, ContextBase>> {- return false;- }}diff --git a/packages/base-sheets/src/SheetPlugin.tsx b/packages/base-sheets/src/SheetPlugin.tsxindex c2002fba84..0778f29c01 100644--- a/packages/base-sheets/src/SheetPlugin.tsx+++ b/packages/base-sheets/src/SheetPlugin.tsx@@ -1,6 +1,18 @@import { getRefElement, IMainProps, isElement, ISlotProps, RefObject, render } from '@univer/base-component';import { Engine, RenderEngine } from '@univer/base-render';-import { AsyncFunction, SheetContext, IWorkbookConfig, Plugin, PLUGIN_NAMES, Tools } from '@univer/core';+import {+ AsyncFunction,+ SheetContext,+ IWorkbookConfig,+ Plugin,+ PLUGIN_NAMES,+ Tools,+ CommandManager,+ IActionObserverProps,+ SheetActionBase,+ ACTION_NAMES,+ ISetNamedRangeActionData,+} from '@univer/core';import { install, SheetPluginObserve, uninstall } from './Basics/Observer';import { RightMenuProps } from './Model/RightMenuModel';@@ -240,6 +252,19 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {// }// this._initializeRender(ctx);++ CommandManager.getActionObservers().add((actionObs: IActionObserverProps): void => {+ const currentUnitId = this.getContext().getWorkBook().getUnitId();+ const action = actionObs.action as SheetActionBase<ISetNamedRangeActionData, ISetNamedRangeActionData>;+ const actionData = action.getDoActionData();+ const actionUnitId = action.getWorkSheet().getContext().getWorkBook().getUnitId();++ if (currentUnitId !== actionUnitId) return;++ if (actionData.actionName === ACTION_NAMES.SET_NAMED_RANGE_ACTION) {+ const namedRange = actionData.namedRange;+ }+ });}get config() {diff --git a/packages/core/src/Command/ActionBase.ts b/packages/core/src/Command/ActionBase.tsindex c53cf07bfe..14840e778f 100644--- a/packages/core/src/Command/ActionBase.ts+++ b/packages/core/src/Command/ActionBase.ts@@ -29,10 +29,15 @@ export enum ActionOperationType {*/UNDO_ACTION = 3,+ /**+ * action extension+ */+ EXTENSION_ACTION = 4,+/*** default obs*/- DEFAULT_ACTION = 1 | 2 | 3,+ DEFAULT_ACTION = 1 | 2 | 3 | 4,}/**diff --git a/packages/core/src/Command/ActionExtensionFactory.ts b/packages/core/src/Command/ActionExtensionFactory.tsindex 48a4690837..2551f3066d 100644--- a/packages/core/src/Command/ActionExtensionFactory.ts+++ b/packages/core/src/Command/ActionExtensionFactory.ts@@ -1,6 +1,7 @@import { ACTION_NAMES } from '../Const';import { IActionData } from './ActionBase';import { Plugin } from '../Plugin';+import { ActionOperation } from './ActionOperation';/*** Manipulate the list of actions in a command@@ -79,7 +80,11 @@ export class BaseActionExtensionFactory<actionData: T,actionDataList: IActionData[]): false | BaseActionExtension<T> {- if (actionData.actionName !== this.actionName) {+ if (+ actionData.actionName !== this.actionName ||+ (actionData.operation != null &&+ !ActionOperation.hasExtension(actionData))+ ) {return false;}diff --git a/packages/core/src/Command/ActionOperation.ts b/packages/core/src/Command/ActionOperation.tsindex 97aca5e2a7..8cb674065a 100644--- a/packages/core/src/Command/ActionOperation.ts+++ b/packages/core/src/Command/ActionOperation.ts@@ -33,6 +33,16 @@ export class ActionOperation<T extends IActionData> {return false;}+ static hasExtension<U extends IActionData>(action: U): boolean {+ if (action.operation) {+ return (+ (action.operation & ActionOperationType.EXTENSION_ACTION) ===+ ActionOperationType.EXTENSION_ACTION+ );+ }+ return false;+ }+static make<U extends IActionData>(action: U) {action.operation = ActionOperationType.DEFAULT_ACTION;return new ActionOperation<U>(action);@@ -63,6 +73,13 @@ export class ActionOperation<T extends IActionData> {return this;}+ removeExtension() {+ if (this._action.operation) {+ this._action.operation &= ~ActionOperationType.EXTENSION_ACTION;+ }+ return this;+ }+getAction(): T {return this._action;}diff --git a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.tsindex 665d1c77fe..320c0346d1 100644--- a/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts+++ b/packages/sheets-plugin-numfmt/src/Controller/NumfmtController.ts@@ -1,4 +1,14 @@-import { Command, Range, IRangeData, ObjectMatrix, ObjectMatrixPrimitiveType, PLUGIN_NAMES, ACTION_NAMES as CORE_ACTION_NAME } from '@univer/core';+import {+ Command,+ Range,+ IRangeData,+ ObjectMatrix,+ ObjectMatrixPrimitiveType,+ PLUGIN_NAMES,+ ACTION_NAMES as CORE_ACTION_NAME,+ ActionOperation,+ ISetRangeDataActionData,+} from '@univer/core';import { BaseComponentRender } from '@univer/base-component';import { IToolBarItemProps, SheetPlugin } from '@univer/base-sheets';import { numfmt } from '@univer/base-numfmt-engine';@@ -50,7 +60,8 @@ export class NumfmtController {rangeData: range,cellValue: matrix.getData(),};- const cmd = new Command({ WorkBookUnit: workbook }, setRangeDataAction);+ const newSetRangeDataAction = ActionOperation.make<ISetRangeDataActionData>(setRangeDataAction).removeExtension().getAction();+ const cmd = new Command({ WorkBookUnit: workbook }, newSetRangeDataAction);workbook.getCommandManager().invoke(cmd);});// update numfmt data