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(sheets-formula-ui): highlight issues on across workbook editing (#5360)

WEI ZHANG827acfd3c2d3
diff --git a/examples/src/sheets-multi-units/main.ts b/examples/src/sheets-multi-units/main.ts
index 82d69ba7a0..3113f40080 100644
--- a/examples/src/sheets-multi-units/main.ts
+++ b/examples/src/sheets-multi-units/main.ts
@@ -39,6 +39,7 @@ import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor';
import { UniverThreadCommentUIPlugin } from '@univerjs/thread-comment-ui';
import { UniverUIPlugin } from '@univerjs/ui';
+import { SwitchUnits } from './switch-units';
import '@univerjs/sheets/facade';
import '@univerjs/ui/facade';
import '@univerjs/docs-ui/facade';
@@ -297,3 +298,8 @@ declare global {
univerAPI?: ReturnType<typeof FUniver.newAPI>;
}
}
+
+univerAPI.registerUIPart(
+ univerAPI.Enum.BuiltInUIPart.CUSTOM_HEADER,
+ SwitchUnits
+);
diff --git a/examples/src/sheets-multi-units/switch-units.tsx b/examples/src/sheets-multi-units/switch-units.tsx
new file mode 100644
index 0000000000..16a00d6f36
--- /dev/null
+++ b/examples/src/sheets-multi-units/switch-units.tsx
@@ -0,0 +1,98 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { Workbook } from '@univerjs/core';
+import { IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { clsx } from '@univerjs/design';
+import { useDependency, useObservable } from '@univerjs/ui';
+import { useMemo } from 'react';
+
+export const SwitchUnits = () => {
+ const univerInstanceService = useDependency(IUniverInstanceService);
+ const activeSheet = useObservable(useMemo(() => univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET), [univerInstanceService]));
+ if (!activeSheet) {
+ return null;
+ }
+
+ const switchSheet = (sheet: Workbook) => {
+ univerInstanceService.focusUnit(sheet.getUnitId());
+ };
+
+ const allSheets = univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ const activeSheetId = activeSheet?.getUnitId();
+
+ return (
+ <div className="univer-w-full univer-border-b univer-border-gray-200 univer-bg-white univer-shadow-sm">
+ <div className="univer-px-4 univer-py-2">
+ <div
+ className={`
+ univer-scrollbar-hide univer-flex univer-items-center univer-gap-1 univer-overflow-x-auto
+ univer-overflow-y-hidden
+ `}
+ >
+ <span
+ className={`
+ univer-mr-3 univer-whitespace-nowrap univer-text-sm univer-font-medium univer-text-gray-600
+ `}
+ >
+ 工作表:
+ </span>
+ {allSheets.map((sheet) => {
+ const isActive = sheet.getUnitId() === activeSheetId;
+ return (
+ <div
+ key={sheet.getUnitId()}
+ onClick={() => switchSheet(sheet)}
+ className={clsx(`
+ univer-relative univer-min-w-0 univer-flex-shrink-0 univer-transform
+ univer-cursor-pointer univer-whitespace-nowrap univer-rounded-lg univer-px-4
+ univer-py-2 univer-text-sm univer-font-medium univer-transition-all
+ univer-duration-200
+ focus:univer-outline-none focus:univer-ring-2 focus:univer-ring-blue-500
+ focus:univer-ring-offset-2
+ active:univer-scale-95
+ `, {
+ [`
+ univer-border-2 univer-border-blue-300 univer-bg-blue-100 univer-text-blue-700
+ univer-shadow-sm
+ `]: isActive,
+ [`
+ univer-border-2 univer-border-transparent univer-bg-gray-50 univer-text-gray-700
+ hover:univer-border-gray-300 hover:univer-bg-gray-100 hover:univer-text-gray-900
+ `]: !isActive,
+ })}
+ title={sheet.getUnitId()}
+ >
+ <span className="univer-max-w-32 univer-truncate">
+ {sheet.getUnitId()}
+ </span>
+ {isActive && (
+ <div
+ className={`
+ univer-absolute -univer-bottom-1 univer-left-1/2 univer-h-1 univer-w-1
+ -univer-translate-x-1/2 univer-transform univer-rounded-full
+ univer-bg-blue-500
+ `}
+ />
+ )}
+ </div>
+ );
+ })}
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/packages/engine-formula/src/engine/reference-object/base-reference-object.ts b/packages/engine-formula/src/engine/reference-object/base-reference-object.ts
index 8151f4706c..dab2d19fed 100644
--- a/packages/engine-formula/src/engine/reference-object/base-reference-object.ts
+++ b/packages/engine-formula/src/engine/reference-object/base-reference-object.ts
@@ -199,6 +199,10 @@ export class BaseReferenceObject extends ObjectClassType {
}
getFirstCell() {
+ if (this._checkIfWorksheetMiss()) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
const { startRow, startColumn } = this.getRangePosition();
const cell = this.getCellData(startRow, startColumn);
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts
index 6090dd4dd7..5b88595d48 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts
+++ b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts
@@ -26,7 +26,7 @@ import { IRenderManagerService } from '@univerjs/engine-render';
import { IRefSelectionsService, setEndForRange } from '@univerjs/sheets';
import { IDescriptionService } from '@univerjs/sheets-formula';
import { SheetSkeletonManagerService } from '@univerjs/sheets-ui';
-import { useDependency, useEvent } from '@univerjs/ui';
+import { useDependency, useEvent, useObservable } from '@univerjs/ui';
import { useEffect, useMemo } from 'react';
import { genFormulaRefSelectionStyle } from '../../../common/selection';
import { RefSelectionsRenderService } from '../../../services/render-services/ref-selections.render-service';
@@ -87,6 +87,10 @@ export function calcHighlightRanges(opts: {
const { unitId: refUnitId, sheetName, range: rawRange } = unitRangeName;
const refSheetId = getSheetIdByName(sheetName);
+ if (!refSheetId && sheetName) {
+ continue;
+ }
+
if (currentUnitId !== unitId && refUnitId !== currentUnitId) {
continue;
}
@@ -134,9 +138,10 @@ export function useSheetHighlight(unitId: string, subUnitId: string) {
const themeService = useDependency(ThemeService);
const refSelectionsService = useDependency(IRefSelectionsService);
const renderManagerService = useDependency(IRenderManagerService);
- const render = renderManagerService.getRenderById(unitId);
- const refSelectionsRenderService = render?.with(RefSelectionsRenderService);
- const sheetSkeletonManagerService = render?.with(SheetSkeletonManagerService);
+ const currentWorkbook = useObservable(useMemo(() => univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET), [univerInstanceService]));
+ const currentRender = currentWorkbook ? renderManagerService.getRenderById(currentWorkbook.getUnitId()) : null;
+ const refSelectionsRenderService = currentRender?.with(RefSelectionsRenderService);
+ const sheetSkeletonManagerService = currentRender?.with(SheetSkeletonManagerService);
const highlightSheet = useEvent((refSelections: IRefSelection[], editor?: Editor) => {
const currentWorkbook = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
@@ -154,13 +159,9 @@ export function useSheetHighlight(unitId: string, subUnitId: string) {
themeService,
univerInstanceService,
});
+
if (!selectionWithStyle) return;
- const allControls = refSelectionsRenderService?.getSelectionControls() || [];
- if (allControls.length === selectionWithStyle.length) {
- refSelectionsRenderService?.resetSelectionsByModelData(selectionWithStyle);
- } else {
- refSelectionsService.setSelections(selectionWithStyle);
- }
+ refSelectionsService.setSelections(selectionWithStyle);
});
useEffect(() => {