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: optimize data validation dropdown list allows cross-worksheet data referencing (#6572)

wpxp12345647a330ac2923
diff --git a/packages/sheets-data-validation-ui/src/services/data-validation-panel.service.ts b/packages/sheets-data-validation-ui/src/services/data-validation-panel.service.ts
index 87461ee3dd..90f3e71854 100644
--- a/packages/sheets-data-validation-ui/src/services/data-validation-panel.service.ts
+++ b/packages/sheets-data-validation-ui/src/services/data-validation-panel.service.ts
@@ -38,6 +38,9 @@ export class DataValidationPanelService extends Disposable {
private _closeDisposable: Nullable<IDisposable> = null;
+ // Record the worksheet to which the rule using the range selector belongs, so that the rule list is not switched when making cross-sheet selections
+ private _focusFormulaEditorActiveRuleSubUnitId: string | null = null;
+
constructor(
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@ISidebarService private readonly _sidebarService: ISidebarService
@@ -91,4 +94,12 @@ export class DataValidationPanelService extends Disposable {
this._activeRule = rule;
this._activeRule$.next(rule);
}
+
+ setFocusFormulaEditorActiveRuleSubUnitId(subUnitId: string | null): void {
+ this._focusFormulaEditorActiveRuleSubUnitId = subUnitId;
+ }
+
+ getFocusFormulaEditorActiveRuleSubUnitId(): string | null {
+ return this._focusFormulaEditorActiveRuleSubUnitId;
+ }
}
diff --git a/packages/sheets-data-validation-ui/src/views/components/detail/index.tsx b/packages/sheets-data-validation-ui/src/views/components/detail/index.tsx
index 6a88b758c4..c0856e682b 100644
--- a/packages/sheets-data-validation-ui/src/views/components/detail/index.tsx
+++ b/packages/sheets-data-validation-ui/src/views/components/detail/index.tsx
@@ -21,11 +21,12 @@ import { debounce, ICommandService, isUnitRangesEqual, IUniverInstanceService, L
import { DataValidationModel, DataValidatorRegistryScope, DataValidatorRegistryService, getRuleOptions, getRuleSetting, TWO_FORMULA_OPERATOR_COUNT } from '@univerjs/data-validation';
import { Button, Checkbox, FormLayout, Select } from '@univerjs/design';
import { deserializeRangeWithSheet, serializeRange } from '@univerjs/engine-formula';
-import { SheetsSelectionsService } from '@univerjs/sheets';
+import { SetWorksheetActiveOperation, SheetsSelectionsService } from '@univerjs/sheets';
import { RemoveSheetDataValidationCommand, UpdateSheetDataValidationOptionsCommand, UpdateSheetDataValidationRangeCommand, UpdateSheetDataValidationSettingCommand } from '@univerjs/sheets-data-validation';
import { RangeSelector } from '@univerjs/sheets-formula-ui';
import { ComponentManager, useDependency, useEvent, useObservable } from '@univerjs/ui';
import { useEffect, useMemo, useRef, useState } from 'react';
+import { of } from 'rxjs';
import { DataValidationPanelService } from '../../../services/data-validation-panel.service';
import { DataValidationOptions } from '../options';
@@ -56,6 +57,13 @@ export function DataValidationDetail() {
const commandService = useDependency(ICommandService);
const dataValidationModel = useDependency(DataValidationModel);
const localeService = useDependency(LocaleService);
+ const workbook = useObservable(
+ () => univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET),
+ undefined,
+ undefined,
+ []
+ );
+ const worksheet = useObservable(() => workbook?.activeSheet$ ?? of(null), undefined, undefined, []);
const [localRule, setLocalRule] = useState<ISheetDataValidationRule>(rule);
const validator = validatorService.getValidatorItem(localRule.type);
const [showError, setShowError] = useState(false);
@@ -99,6 +107,15 @@ export function DataValidationDetail() {
const operatorNames = validator.operatorNames;
const isTwoFormula = localRule.operator ? TWO_FORMULA_OPERATOR_COUNT.includes(localRule.operator) : false;
+ const goBackActiveRuleSheet = () => {
+ if (worksheet?.getSheetId() !== subUnitId) {
+ commandService.syncExecuteCommand(SetWorksheetActiveOperation.id, {
+ unitId,
+ subUnitId,
+ });
+ }
+ };
+
const handleOk = () => {
if (rangeSelectorInstance.current?.editor?.isFocus()) {
handleUpdateRuleRanges(rangeSelectorInstance.current?.getValue());
@@ -112,6 +129,8 @@ export function DataValidationDetail() {
} else {
setShowError(true);
}
+
+ goBackActiveRuleSheet();
};
const handleUpdateRuleRanges = useEvent((rangeText: string) => {
@@ -180,6 +199,7 @@ export function DataValidationDetail() {
subUnitId,
});
dataValidationPanelService.setActiveRule(null);
+ goBackActiveRuleSheet();
};
const baseRule = {
diff --git a/packages/sheets-data-validation-ui/src/views/components/formula-input/ListFormulaInput.tsx b/packages/sheets-data-validation-ui/src/views/components/formula-input/ListFormulaInput.tsx
index 6d1da2ace4..684f26a131 100644
--- a/packages/sheets-data-validation-ui/src/views/components/formula-input/ListFormulaInput.tsx
+++ b/packages/sheets-data-validation-ui/src/views/components/formula-input/ListFormulaInput.tsx
@@ -27,6 +27,7 @@ import { useDependency, useEvent, useObservable, useSidebarClick } from '@univer
import { useEffect, useMemo, useRef, useState } from 'react';
import { debounceTime } from 'rxjs';
import { DROP_DOWN_DEFAULT_COLOR } from '../../../const';
+import { DataValidationPanelService } from '../../../services/data-validation-panel.service';
const DEFAULT_COLOR_PRESET = [
'#FFFFFF',
@@ -171,6 +172,7 @@ export function ListFormulaInput(props: IFormulaInputProps) {
const dataValidatorRegistryService = useDependency(DataValidatorRegistryService);
const dataValidationModel = useDependency(DataValidationModel);
const dataValidationFormulaController = useDependency(DataValidationFormulaController);
+ const dataValidationPanelService = useDependency(DataValidationPanelService);
const [refColors, setRefColors] = useState(() => formula2.split(','));
const listValidator = dataValidatorRegistryService.getValidatorItem(DataValidationType.LIST) as ListValidator;
const [refOptions, setRefOptions] = useState<string[]>([]);
@@ -328,6 +330,14 @@ export function ListFormulaInput(props: IFormulaInputProps) {
isOutSide && setIsFocusFormulaEditor(false);
});
+ useEffect(() => {
+ if (isFocusFormulaEditor) {
+ dataValidationPanelService.setFocusFormulaEditorActiveRuleSubUnitId(subUnitId);
+ } else {
+ dataValidationPanelService.setFocusFormulaEditorActiveRuleSubUnitId(null);
+ }
+ }, [isFocusFormulaEditor, subUnitId, dataValidationPanelService]);
+
return (
<>
<FormLayout label={localeService.t('dataValidation.list.options')}>
diff --git a/packages/sheets-data-validation-ui/src/views/components/panel/index.tsx b/packages/sheets-data-validation-ui/src/views/components/panel/index.tsx
index 07c60e8a9a..c05700f04e 100644
--- a/packages/sheets-data-validation-ui/src/views/components/panel/index.tsx
+++ b/packages/sheets-data-validation-ui/src/views/components/panel/index.tsx
@@ -36,7 +36,7 @@ export const DataValidationPanel = () => {
if (!workbook || !worksheet) return null;
return (
- activeRule && activeRule.subUnitId === worksheet.getSheetId()
+ activeRule && (activeRule.subUnitId === worksheet.getSheetId() || activeRule.subUnitId === dataValidationPanelService.getFocusFormulaEditorActiveRuleSubUnitId())
? <DataValidationDetail key={activeRule.rule.uid} />
: <DataValidationList workbook={workbook} />
);