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(sheet): add highlightRanges facade apis (#6015)

VicKun94533a5388e8
diff --git a/packages/sheets-ui/src/facade/f-worksheet.ts b/packages/sheets-ui/src/facade/f-worksheet.ts
index dffc033e76..5d22a9bddc 100644
--- a/packages/sheets-ui/src/facade/f-worksheet.ts
+++ b/packages/sheets-ui/src/facade/f-worksheet.ts
@@ -14,14 +14,16 @@
* limitations under the License.
*/
-import type { IDisposable, IRange, Nullable } from '@univerjs/core';
+import type { IDisposable, IRange, ISelectionCell, Nullable } from '@univerjs/core';
import type { IColumnsHeaderCfgParam, IRowsHeaderCfgParam, RenderComponentType, RenderManagerService, SpreadsheetColumnHeader, SpreadsheetRowHeader, SpreadsheetSkeleton } from '@univerjs/engine-render';
+import type { ISelectionStyle } from '@univerjs/sheets';
import type { IScrollState, IViewportScrollState } from '@univerjs/sheets-ui';
+import type { FRange } from '@univerjs/sheets/facade';
import { ICommandService, toDisposable } from '@univerjs/core';
import { IRenderManagerService, SHEET_VIEWPORT_KEY, sheetContentViewportKeys } from '@univerjs/engine-render';
import { SetWorksheetRowIsAutoHeightCommand } from '@univerjs/sheets';
-import { SetColumnHeaderHeightCommand, SetRowHeaderWidthCommand, SetWorksheetColAutoWidthCommand, SetZoomRatioCommand, SHEET_VIEW_KEY, SheetScrollManagerService, SheetSkeletonManagerService, SheetsScrollRenderController } from '@univerjs/sheets-ui';
+import { IMarkSelectionService, SetColumnHeaderHeightCommand, SetRowHeaderWidthCommand, SetWorksheetColAutoWidthCommand, SetZoomRatioCommand, SHEET_VIEW_KEY, SheetScrollManagerService, SheetSkeletonManagerService, SheetsScrollRenderController } from '@univerjs/sheets-ui';
import { FWorksheet } from '@univerjs/sheets/facade';
/**
@@ -40,6 +42,25 @@ export interface IFWorksheetSkeletonMixin {
*/
refreshCanvas(): FWorksheet;
+ /**
+ * Highlight multiple ranges on the worksheet.
+ * @param {FRange[]} ranges The ranges to highlight.
+ * @param {Nullable<Partial<ISelectionStyle>>} style - style for highlight ranges.
+ * @param {Nullable<ISelectionCell>} primary - primary cell for highlight ranges.
+ * @return {IDisposable} An IDisposable to remove the highlights.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorksheet = fWorkbook.getActiveSheet();
+ * const ranges = [fWorksheet.getRange('A1:B2'), fWorksheet.getRange('D4:E5')];
+ * const disposable = fWorksheet.highlightRanges(ranges, { backgroundColor: 'yellow' });
+ *
+ * // To remove the highlights later
+ * disposable.dispose();
+ * ```
+ */
+ highlightRanges(ranges: FRange[], style?: Nullable<Partial<ISelectionStyle>>, primary?: Nullable<ISelectionCell>): IDisposable;
+
/**
* Set zoom ratio of the worksheet.
* @param {number} zoomRatio The zoom ratio to set.It should be in the range of 0.1 to 4.0.
@@ -309,6 +330,28 @@ export class FWorksheetSkeletonMixin extends FWorksheet implements IFWorksheetSk
return this;
}
+ override highlightRanges(ranges: FRange[], style?: Nullable<Partial<ISelectionStyle>>, primary?: Nullable<ISelectionCell>): IDisposable {
+ const markSelectionService = this._injector.get(IMarkSelectionService);
+ const ids: string[] = [];
+ for (const range of ranges) {
+ const iRange = range.getRange();
+ const id = markSelectionService.addShapeWithNoFresh({ range: iRange, style, primary });
+ if (id) {
+ ids.push(id);
+ }
+ }
+ markSelectionService.refreshShapes();
+
+ if (ids.length === 0) {
+ throw new Error('Failed to highlight current range');
+ }
+ return toDisposable(() => {
+ ids.forEach((id) => {
+ markSelectionService.removeShape(id);
+ });
+ });
+ }
+
override zoom(zoomRatio: number): FWorksheet {
const commandService = this._injector.get(ICommandService);
const _zoomRatio = Math.min(Math.max(zoomRatio, 0.1), 4);