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: fix auto-fill api opreate in non-active sheet (#6205)

wpxp12345692593431aef6
diff --git a/packages/sheets-ui/src/commands/commands/auto-fill.command.ts b/packages/sheets-ui/src/commands/commands/auto-fill.command.ts
index c90af55901..33a28383c8 100644
--- a/packages/sheets-ui/src/commands/commands/auto-fill.command.ts
+++ b/packages/sheets-ui/src/commands/commands/auto-fill.command.ts
@@ -24,6 +24,8 @@ import { IAutoFillService } from '../../services/auto-fill/auto-fill.service';
export interface IAutoFillCommandParams {
sourceRange: IRange;
targetRange: IRange;
+ unitId?: string; // if not provided, use current unitId
+ subUnitId?: string; // if not provided, use current subUnitId
applyType?: APPLY_TYPE; // manual apply type
}
@@ -37,7 +39,7 @@ export const AutoFillCommand: ICommand = {
const { sourceRange, targetRange, applyType } = params;
- const commandTarget = getSheetCommandTarget(univerInstanceService);
+ const commandTarget = getSheetCommandTarget(univerInstanceService, params);
if (!commandTarget) return false;
const { subUnitId, unitId } = commandTarget;
diff --git a/packages/sheets-ui/src/controllers/auto-fill.controller.ts b/packages/sheets-ui/src/controllers/auto-fill.controller.ts
index cc2ae30c1b..fe5373afd5 100644
--- a/packages/sheets-ui/src/controllers/auto-fill.controller.ts
+++ b/packages/sheets-ui/src/controllers/auto-fill.controller.ts
@@ -29,7 +29,6 @@ import type {
IRuleConfirmedData,
ISheetAutoFillHook,
} from '../services/auto-fill/type';
-import type { IDiscreteRange } from './utils/range-tools';
import {
Direction,
Disposable,
@@ -424,13 +423,12 @@ export class AutoFillController extends Disposable {
}
}
- private _getCopyData(source: IDiscreteRange, direction: Direction) {
- const worksheet = this._univerInstanceService
- .getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!
- .getActiveSheet();
+ private _getCopyData(location: IAutoFillLocation, direction: Direction) {
+ const { unitId, subUnitId, source } = location;
+ const worksheet = this._univerInstanceService.getUnit<Workbook>(unitId)?.getSheetBySheetId(subUnitId);
if (!worksheet) {
- throw new Error('No active sheet found');
+ throw new Error('No worksheet found');
}
const currentCellDatas = worksheet.getCellMatrix();
@@ -501,8 +499,9 @@ export class AutoFillController extends Disposable {
return copyDataPiece;
}
- private _getMergeApplyData(source: IRange, target: IRange, direction: Direction, csLen: number) {
- const worksheet = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet();
+ private _getMergeApplyData(source: IRange, target: IRange, direction: Direction, csLen: number, location: IAutoFillLocation) {
+ const { unitId, subUnitId } = location;
+ const worksheet = this._univerInstanceService.getUnit<Workbook>(unitId)?.getSheetBySheetId(subUnitId);
if (!worksheet) {
throw new Error('No active sheet found');
@@ -573,11 +572,9 @@ export class AutoFillController extends Disposable {
}
private _presetAndCacheData(location: IAutoFillLocation, direction: Direction) {
- const { source, target } = location;
+ const { unitId, subUnitId, target } = location;
// cache original data of apply range
- const worksheet = this._univerInstanceService
- .getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!
- .getActiveSheet();
+ const worksheet = this._univerInstanceService.getUnit<Workbook>(unitId)?.getSheetBySheetId(subUnitId);
if (!worksheet) {
throw new Error('No active sheet found');
@@ -594,7 +591,7 @@ export class AutoFillController extends Disposable {
applyData.push(row);
});
this._beforeApplyData = applyData;
- this._copyData = this._getCopyData(source, direction);
+ this._copyData = this._getCopyData(location, direction);
this._currentLocation = location;
if (this._shouldDisableSeries(this._copyData)) {
this._autoFillService.setDisableApplyType(APPLY_TYPE.SERIES, true);
@@ -680,9 +677,9 @@ export class AutoFillController extends Disposable {
// deal with styles
let applyMergeRanges: IRange[] = [];
- const style = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getStyles();
+ const style = this._univerInstanceService.getUnit<Workbook>(unitId)?.getStyles();
if (hasStyle) {
- applyMergeRanges = this._getMergeApplyData(sourceRange, targetRange, direction, csLen);
+ applyMergeRanges = this._getMergeApplyData(sourceRange, targetRange, direction, csLen, location);
applyDatas.forEach((row) => {
row.forEach((cellData) => {
if (cellData && style) {
diff --git a/packages/sheets-ui/src/facade/f-range.ts b/packages/sheets-ui/src/facade/f-range.ts
index 33938340fe..5535f91cf3 100644
--- a/packages/sheets-ui/src/facade/f-range.ts
+++ b/packages/sheets-ui/src/facade/f-range.ts
@@ -238,6 +238,25 @@ interface IFRangeSheetsUIMixin {
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:A4');
*
+ * // Auto-fill without specifying applyType (default behavior)
+ * await fRange.autoFill(fWorksheet.getRange('A1:A20'))
+ *
+ * // Auto-fill with 'COPY' type
+ * await fRange.autoFill(fWorksheet.getRange('A1:A20'), 'COPY')
+ *
+ * // Auto-fill with 'SERIES' type
+ * await fRange.autoFill(fWorksheet.getRange('A1:A20'), 'SERIES')
+ * ```
+ *
+ * ```ts
+ * // Operate on a specific worksheet
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorksheet = fWorkbook.getSheetBySheetId('sheetId');
+ * const fRange = fWorksheet.getRange('A1:A4');
+ *
+ * // Auto-fill without specifying applyType (default behavior)
+ * await fRange.autoFill(fWorksheet.getRange('A1:A20'))
+ *
* // Auto-fill with 'COPY' type
* await fRange.autoFill(fWorksheet.getRange('A1:A20'), 'COPY')
*
@@ -414,6 +433,8 @@ class FRangeSheetsUIMixin extends FRange implements IFRangeSheetsUIMixin {
return this._commandService.executeCommand(AutoFillCommand.id, {
sourceRange: _sourceRange,
targetRange: _targetRange,
+ unitId: this.getUnitId(),
+ subUnitId: this.getSheetId(),
applyType,
});
}
diff --git a/packages/sheets-ui/src/services/auto-fill/auto-fill.service.ts b/packages/sheets-ui/src/services/auto-fill/auto-fill.service.ts
index 431e7d127e..01fbb72063 100644
--- a/packages/sheets-ui/src/services/auto-fill/auto-fill.service.ts
+++ b/packages/sheets-ui/src/services/auto-fill/auto-fill.service.ts
@@ -387,7 +387,10 @@ export class AutoFillService extends Disposable implements IAutoFillService {
// eslint-disable-next-line max-lines-per-function
fillData(applyType: APPLY_TYPE) {
this.applyType = applyType;
- const { source, target, unitId = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getUnitId(), subUnitId = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!.getActiveSheet()?.getSheetId() } = this.autoFillLocation || {};
+ const activeWorkbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ const activeUnitId = activeWorkbook?.getUnitId();
+ const activeSubUnitId = activeWorkbook?.getActiveSheet()?.getSheetId();
+ const { source, target, unitId = activeUnitId, subUnitId = activeSubUnitId } = this.autoFillLocation || {};
if (!source || !target || !unitId || !subUnitId) {
return false;
@@ -406,19 +409,22 @@ export class AutoFillService extends Disposable implements IAutoFillService {
const activeHooks = this.getActiveHooks();
const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)!;
- this._commandService.syncExecuteCommand(SetSelectionsOperation.id, {
- selections: [
- {
- primary: { ...(this._selectionManagerService.getCurrentLastSelection()?.primary ?? selection) },
- range: {
- ...selection,
- rangeType: RANGE_TYPE.NORMAL,
+ // If filling the active sheet, update the selection immediately
+ if (unitId === activeUnitId && subUnitId === activeSubUnitId) {
+ this._commandService.syncExecuteCommand(SetSelectionsOperation.id, {
+ selections: [
+ {
+ primary: { ...(this._selectionManagerService.getCurrentLastSelection()?.primary ?? selection) },
+ range: {
+ ...selection,
+ rangeType: RANGE_TYPE.NORMAL,
+ },
},
- },
- ],
- unitId,
- subUnitId,
- });
+ ],
+ unitId,
+ subUnitId,
+ });
+ }
const undos: IMutationInfo[] = [];
const redos: IMutationInfo[] = [];