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.

feat: implement uniscript demo (#589)

Wenzhao Huea5f18ef7517
diff --git a/.eslintignore b/.eslintignore
index 7206202180..551d44c719 100644
--- a/.eslintignore
+++ b/.eslintignore
@@ -32,3 +32,6 @@ yarn-error.log*
# yarn v2
.yarn
+
+# Uniscript example
+**/examples/china-id-validation.js
\ No newline at end of file
diff --git a/packages/base-sheets/src/commands/commands/set-range-values.command.ts b/packages/base-sheets/src/commands/commands/set-range-values.command.ts
index 169d14af19..0ebd0b2fe9 100644
--- a/packages/base-sheets/src/commands/commands/set-range-values.command.ts
+++ b/packages/base-sheets/src/commands/commands/set-range-values.command.ts
@@ -1,27 +1,21 @@
+import type { ICellData, ICommand, IRange, ObjectMatrixPrimitiveType } from '@univerjs/core';
import {
CommandType,
- ICellData,
- ICommand,
ICommandService,
INTERCEPTOR_POINT,
- IRange,
isICellData,
IUndoRedoService,
IUniverInstanceService,
ObjectMatrix,
- ObjectMatrixPrimitiveType,
sequenceExecute,
SheetInterceptorService,
Tools,
} from '@univerjs/core';
-import { IAccessor } from '@wendellhu/redi';
+import type { IAccessor } from '@wendellhu/redi';
import { SelectionManagerService } from '../../services/selection-manager.service';
-import {
- ISetRangeValuesMutationParams,
- SetRangeValuesMutation,
- SetRangeValuesUndoMutationFactory,
-} from '../mutations/set-range-values.mutation';
+import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
+import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
export interface ISetRangeValuesCommandParams {
worksheetId?: string;
@@ -54,6 +48,7 @@ export const SetRangeValuesCommand: ICommand = {
workbookId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId(),
worksheetId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId(),
} = params;
+
const currentSelections = range ? [range] : selectionManagerService.getSelectionRanges();
if (!currentSelections || !currentSelections.length) {
return false;
diff --git a/packages/base-sheets/src/commands/commands/set-style.command.ts b/packages/base-sheets/src/commands/commands/set-style.command.ts
index e50929837e..059797db2e 100644
--- a/packages/base-sheets/src/commands/commands/set-style.command.ts
+++ b/packages/base-sheets/src/commands/commands/set-style.command.ts
@@ -1,39 +1,42 @@
-import {
- BooleanNumber,
- CommandType,
- FontItalic,
- FontWeight,
+import type {
HorizontalAlign,
ICellData,
IColorStyle,
ICommand,
- ICommandService,
+ IRange,
IStyleData,
ITextRotation,
+ VerticalAlign,
+ WrapStrategy,
+} from '@univerjs/core';
+import {
+ BooleanNumber,
+ CommandType,
+ FontItalic,
+ FontWeight,
+ ICommandService,
IUndoRedoService,
IUniverInstanceService,
ObjectMatrix,
sequenceExecute,
SheetInterceptorService,
Tools,
- VerticalAlign,
- WrapStrategy,
} from '@univerjs/core';
-import { IAccessor } from '@wendellhu/redi';
+import type { IAccessor } from '@wendellhu/redi';
import { SelectionManagerService } from '../../services/selection-manager.service';
-import {
- ISetRangeValuesMutationParams,
- SetRangeValuesMutation,
- SetRangeValuesUndoMutationFactory,
-} from '../mutations/set-range-values.mutation';
+import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
+import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
export interface IStyleTypeValue<T> {
type: keyof IStyleData;
value: T | T[][];
}
-export interface ISetStyleParams<T> {
+export interface ISetStyleCommandParams<T> {
+ worksheetId?: string;
+ workbookId?: string;
+ range?: IRange;
style: IStyleTypeValue<T>;
}
@@ -41,26 +44,28 @@ export interface ISetStyleParams<T> {
* The command to set cell style.
* Set style to a bunch of ranges.
*/
-export const SetStyleCommand: ICommand<ISetStyleParams<unknown>> = {
+export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {
type: CommandType.COMMAND,
id: 'sheet.command.set-style',
- handler: async <T>(accessor: IAccessor, params: ISetStyleParams<T>) => {
+ handler: async <T>(accessor: IAccessor, params: ISetStyleCommandParams<T>) => {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+
+ const {
+ workbookId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId(),
+ worksheetId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId(),
+ range,
+ style,
+ } = params;
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const selectionManagerService = accessor.get(SelectionManagerService);
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const ranges = selectionManagerService.getSelectionRanges();
+ const ranges = range ? [range] : selectionManagerService.getSelectionRanges();
if (!ranges?.length) {
return false;
}
- const workbookId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();
- const worksheetId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();
-
- const { style } = params;
-
const workbook = univerInstanceService.getUniverSheetInstance(workbookId);
const worksheet = workbook?.getSheetBySheetId(worksheetId);
if (worksheet == null) {
@@ -68,7 +73,6 @@ export const SetStyleCommand: ICommand<ISetStyleParams<unknown>> = {
}
const cellValue = new ObjectMatrix<ICellData>();
- // let value: ObjectMatrixPrimitiveType<ICellData> | undefined;
if (Tools.isArray(style.value)) {
for (let i = 0; i < ranges.length; i++) {
@@ -84,8 +88,6 @@ export const SetStyleCommand: ICommand<ISetStyleParams<unknown>> = {
}
}
}
-
- // value = cellValue.getData();
} else {
for (let i = 0; i < ranges.length; i++) {
const { startRow, endRow, startColumn, endColumn } = ranges[i];
@@ -95,7 +97,7 @@ export const SetStyleCommand: ICommand<ISetStyleParams<unknown>> = {
[style.type]: style.value,
},
};
- // value = Tools.fillObjectMatrix(endRow - startRow + 1, endColumn - startColumn + 1, colorObj);
+
for (let r = startRow; r <= endRow; r++) {
for (let c = startColumn; c <= endColumn; c++) {
cellValue.setValue(r, c, colorObj);
@@ -159,7 +161,7 @@ export const SetBoldCommand: ICommand = {
const { actualRow, actualColumn } = selection.primary;
const currentlyBold = worksheet.getRange(actualRow, actualColumn).getFontWeight() === FontWeight.BOLD;
- const setStyleParams: ISetStyleParams<BooleanNumber> = {
+ const setStyleParams: ISetStyleCommandParams<BooleanNumber> = {
style: {
type: 'bl',
value: currentlyBold ? BooleanNumber.FALSE : BooleanNumber.TRUE,
@@ -193,7 +195,7 @@ export const SetItalicCommand: ICommand = {
currentlyItalic = worksheet.getRange(startRow, startColumn).getFontStyle() === FontItalic.ITALIC;
}
- const setStyleParams: ISetStyleParams<BooleanNumber> = {
+ const setStyleParams: ISetStyleCommandParams<BooleanNumber> = {
style: {
type: 'it',
value: currentlyItalic ? BooleanNumber.FALSE : BooleanNumber.TRUE,
@@ -228,7 +230,7 @@ export const SetUnderlineCommand: ICommand = {
.getUnderline().s;
}
- const setStyleParams: ISetStyleParams<{ s: number }> = {
+ const setStyleParams: ISetStyleCommandParams<{ s: number }> = {
style: {
type: 'ul',
value: {
@@ -265,7 +267,7 @@ export const SetStrikeThroughCommand: ICommand = {
.getStrikeThrough().s;
}
- const setStyleParams: ISetStyleParams<{ s: number }> = {
+ const setStyleParams: ISetStyleCommandParams<{ s: number }> = {
style: {
type: 'st',
value: { s: currentlyStrokeThrough ? BooleanNumber.FALSE : BooleanNumber.TRUE },
@@ -300,7 +302,7 @@ export const SetOverlineCommand: ICommand = {
.getOverline().s;
}
- const setStyleParams: ISetStyleParams<{ s: number }> = {
+ const setStyleParams: ISetStyleCommandParams<{ s: number }> = {
style: {
type: 'ol',
value: {
@@ -326,7 +328,7 @@ export const SetFontFamilyCommand: ICommand<ISetFontFamilyCommandParams> = {
}
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<string> = {
+ const setStyleParams: ISetStyleCommandParams<string> = {
style: {
type: 'ff',
value: params.value,
@@ -350,7 +352,7 @@ export const SetFontSizeCommand: ICommand<ISetFontSizeCommandParams> = {
}
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<number> = {
+ const setStyleParams: ISetStyleCommandParams<number> = {
style: {
type: 'fs',
value: params.value,
@@ -374,7 +376,7 @@ export const SetTextColorCommand: ICommand<ISetColorCommandParams> = {
}
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<IColorStyle> = {
+ const setStyleParams: ISetStyleCommandParams<IColorStyle> = {
style: {
type: 'cl',
value: {
@@ -392,7 +394,7 @@ export const ResetTextColorCommand: ICommand = {
id: 'sheet.command.reset-text-color',
handler: async (accessor) => {
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<IColorStyle> = {
+ const setStyleParams: ISetStyleCommandParams<IColorStyle> = {
style: {
type: 'cl',
value: {
@@ -414,7 +416,7 @@ export const SetBackgroundColorCommand: ICommand<ISetColorCommandParams> = {
}
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<IColorStyle> = {
+ const setStyleParams: ISetStyleCommandParams<IColorStyle> = {
style: {
type: 'bg',
value: {
@@ -432,7 +434,7 @@ export const ResetBackgroundColorCommand: ICommand = {
id: 'sheet.command.reset-background-color',
handler: async (accessor) => {
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<IColorStyle> = {
+ const setStyleParams: ISetStyleCommandParams<IColorStyle> = {
style: {
type: 'bg',
value: {
@@ -458,7 +460,7 @@ export const SetVerticalTextAlignCommand: ICommand<ISetVerticalTextAlignCommandP
}
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<VerticalAlign> = {
+ const setStyleParams: ISetStyleCommandParams<VerticalAlign> = {
style: {
type: 'vt',
value: params.value,
@@ -482,7 +484,7 @@ export const SetHorizontalTextAlignCommand: ICommand<ISetHorizontalTextAlignComm
}
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<HorizontalAlign> = {
+ const setStyleParams: ISetStyleCommandParams<HorizontalAlign> = {
style: {
type: 'ht',
value: params.value,
@@ -506,7 +508,7 @@ export const SetTextWrapCommand: ICommand<ISetTextWrapCommandParams> = {
}
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<WrapStrategy> = {
+ const setStyleParams: ISetStyleCommandParams<WrapStrategy> = {
style: {
type: 'tb',
value: params.value,
@@ -532,7 +534,7 @@ export const SetTextRotationCommand: ICommand<ISetTextRotationCommandParams> = {
const value = typeof params.value === 'number' ? { a: params.value } : { a: 0, v: BooleanNumber.TRUE };
const commandService = accessor.get(ICommandService);
- const setStyleParams: ISetStyleParams<ITextRotation> = {
+ const setStyleParams: ISetStyleCommandParams<ITextRotation> = {
style: {
type: 'tr',
value,
diff --git a/packages/base-sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/base-sheets/src/commands/mutations/set-range-values.mutation.ts
index 6cbeafe591..25acd187c3 100644
--- a/packages/base-sheets/src/commands/mutations/set-range-values.mutation.ts
+++ b/packages/base-sheets/src/commands/mutations/set-range-values.mutation.ts
@@ -1,6 +1,4 @@
-import {
- CellValueType,
- CommandType,
+import type {
IBorderData,
ICellData,
ICellV,
@@ -12,13 +10,11 @@ import {
IRange,
IStyleData,
ITextStyle,
- IUniverInstanceService,
Nullable,
- ObjectMatrix,
ObjectMatrixPrimitiveType,
- Tools,
} from '@univerjs/core';
-import { IAccessor } from '@wendellhu/redi';
+import { CellValueType, CommandType, IUniverInstanceService, ObjectMatrix, Tools } from '@univerjs/core';
+import type { IAccessor } from '@wendellhu/redi';
/** Params of `SetRangeValuesMutation` */
export interface ISetRangeValuesMutationParams extends IMutationCommonParams {
@@ -231,7 +227,7 @@ function checkCellValueType(v: Nullable<ICellV>): Nullable<CellValueType> {
if (v === null) return null;
if (typeof v === 'string') {
- if (isNumeric(v)) {
+ if (isSafeNumeric(v)) {
return CellValueType.NUMBER;
}
return CellValueType.STRING;
@@ -388,3 +384,12 @@ export function mergeRichTextStyle(p: IDocumentData, newStyle: Nullable<IStyleDa
function isNumeric(str: string) {
return /^-?\d+(\.\d+)?$/.test(str);
}
+
+function isSafeNumeric(str: string) {
+ const numberic = isNumeric(str);
+ if (!numberic) {
+ return false;
+ }
+
+ return Number(str) <= Number.MAX_SAFE_INTEGER;
+}
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 69f27630ca..88f32defec 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -97,8 +97,7 @@ export {
SetSpecificColsVisibleCommand,
} from './commands/commands/set-col-visible.command';
export { SetFrozenCommand } from './commands/commands/set-frozen.command';
-export type { ISetRangeValuesCommandParams } from './commands/commands/set-range-values.command';
-export { SetRangeValuesCommand } from './commands/commands/set-range-values.command';
+export { type ISetRangeValuesCommandParams, SetRangeValuesCommand } from './commands/commands/set-range-values.command';
export {
type ISetSpecificRowsVisibleCommandParams,
SetRowHiddenCommand,
@@ -106,7 +105,7 @@ export {
SetSpecificRowsVisibleCommand,
} from './commands/commands/set-row-visible.command';
export {
- type ISetStyleParams,
+ type ISetStyleCommandParams,
ResetBackgroundColorCommand,
ResetTextColorCommand,
SetBackgroundColorCommand,
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 3988206f1d..34345e0df4 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -17,6 +17,8 @@ export {
export * from './observer';
export { Plugin, PluginType } from './plugin/plugin';
export {
+ type CommandListener,
+ CommandService,
CommandType,
type ICommand,
type ICommandInfo,
diff --git a/packages/ui-plugin-sheets/src/controllers/auto-height.controller.ts b/packages/ui-plugin-sheets/src/controllers/auto-height.controller.ts
index 2ea8ab3e8d..fd90056070 100644
--- a/packages/ui-plugin-sheets/src/controllers/auto-height.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/auto-height.controller.ts
@@ -1,11 +1,12 @@
-// TODO@wzhudev: re-export from base-sheets
-import {
- DeltaColumnWidthCommand,
+import type {
ISetRangeValuesRangeMutationParams,
- ISetStyleParams,
+ ISetStyleCommandParams,
ISetWorksheetColWidthMutationParams,
ISetWorksheetRowAutoHeightMutationParams,
ISetWorksheetRowIsAutoHeightMutationParams,
+} from '@univerjs/base-sheets';
+import {
+ DeltaColumnWidthCommand,
SelectionManagerService,
SetColWidthCommand,
SetRangeValuesCommand,
@@ -14,7 +15,8 @@ import {
SetWorksheetRowAutoHeightMutationFactory,
SetWorksheetRowIsAutoHeightCommand,
} from '@univerjs/base-sheets';
-import { IRange, IUniverInstanceService, LifecycleStages, OnLifecycle, SheetInterceptorService } from '@univerjs/core';
+import type { IRange } from '@univerjs/core';
+import { IUniverInstanceService, LifecycleStages, OnLifecycle, SheetInterceptorService } from '@univerjs/core';
import { Inject, Injector } from '@wendellhu/redi';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
@@ -118,7 +120,7 @@ export class AutoHeightController {
// for intercept set style command.
sheetInterceptorService.interceptCommand({
- getMutations: (command: { id: string; params: ISetStyleParams<number> }) => {
+ getMutations: (command: { id: string; params: ISetStyleCommandParams<number> }) => {
if (command.id !== SetStyleCommand.id) {
return {
redos: [],
diff --git a/packages/uniscript/examples/china-id-validation.js b/packages/uniscript/examples/china-id-validation.js
new file mode 100644
index 0000000000..4aa134a1c1
--- /dev/null
+++ b/packages/uniscript/examples/china-id-validation.js
@@ -0,0 +1,66 @@
+/* eslint-disable no-magic-numbers */
+
+const mutliNum = (function generate() {
+ const obj = {};
+ for (let i = 0, len = 17; i < len; i++) {
+ obj[i] = 2 ** (17 - i) % 11;
+ }
+ return obj;
+})();
+
+const xCodeMap = {
+ 0: '1',
+ 1: '0',
+ 2: 'X',
+ 3: '9',
+ 4: '8',
+ 5: '7',
+ 6: '6',
+ 7: '5',
+ 8: '4',
+ 9: '3',
+ 10: '2',
+};
+
+function getXCode(szID) {
+ let sum = 0;
+ for (let i = 0, len = szID.length - 1; i < len; i++) {
+ sum += szID[i] * mutliNum[i];
+ }
+ let xCode = sum % 11;
+ xCode = xCodeMap[xCode];
+ return xCode === 10 ? 'X' : `${xCode}`;
+}
+
+function validIDNum(szID) {
+ if (szID.length !== 18) {
+ return false;
+ }
+
+ const xCode = getXCode(szID);
+ if (xCode === szID[17]) {
+ return true;
+ }
+
+ return false;
+}
+
+const redColor = 'rgb(240, 55, 31)';
+
+const activeSheet = Univer.getCurrentSheet().getActiveSheet();
+const range = activeSheet.getSelection().getActiveRange();
+const row = range.getRow();
+const col = range.getColumn();
+const width = range.getWidth();
+const height = range.getHeight();
+
+for (let i = row; i < row + height; i++) {
+ for (let j = col; j < col + width; j++) {
+ const range = activeSheet.getRange(i, j);
+ const ID = range.getValue();
+ const valid = validIDNum(ID);
+ if (!valid) {
+ range.setBackgroundColor(redColor);
+ }
+ }
+}
diff --git a/packages/uniscript/src/facade/facade.ts b/packages/uniscript/src/facade/facade.ts
index 033482210c..4ca7139975 100644
--- a/packages/uniscript/src/facade/facade.ts
+++ b/packages/uniscript/src/facade/facade.ts
@@ -1 +1,24 @@
-export class FUniver {}
+import { IUniverInstanceService } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
+
+import { FWorkbook } from './sheet/f-workbook';
+
+export class FUniver {
+ static newInstance(injector: Injector): FUniver {
+ return injector.createInstance(FUniver);
+ }
+
+ constructor(
+ @Inject(Injector) private readonly _injector: Injector,
+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService
+ ) {}
+
+ getCurrentSheet(): FWorkbook | null {
+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();
+ if (!workbook) {
+ return null;
+ }
+
+ return this._injector.createInstance(FWorkbook, workbook);
+ }
+}
diff --git a/packages/uniscript/src/facade/sheet/f-range.ts b/packages/uniscript/src/facade/sheet/f-range.ts
new file mode 100644
index 0000000000..f7a50569e9
--- /dev/null
+++ b/packages/uniscript/src/facade/sheet/f-range.ts
@@ -0,0 +1,49 @@
+import type { ISetStyleCommandParams } from '@univerjs/base-sheets';
+import { SetStyleCommand } from '@univerjs/base-sheets';
+import type { ICellV, IColorStyle, IRange, Workbook, Worksheet } from '@univerjs/core';
+import { ICommandService } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
+
+export class FRange {
+ constructor(
+ private readonly _workbook: Workbook,
+ private readonly _worksheet: Worksheet,
+ private readonly _range: IRange,
+ @Inject(Injector) private readonly _injector: Injector,
+ @ICommandService private readonly _commandService: ICommandService
+ ) {}
+
+ getRow(): number {
+ return this._range.startRow;
+ }
+
+ getColumn(): number {
+ return this._range.startColumn;
+ }
+
+ getWidth(): number {
+ return this._range.endColumn - this._range.startColumn + 1;
+ }
+
+ getHeight(): number {
+ return this._range.endRow - this._range.startRow + 1;
+ }
+
+ getValue(): ICellV | null {
+ return this._worksheet.getCell(this._range.startRow, this._range.startColumn)?.v ?? null;
+ }
+
+ setBackgroundColor(color: string): void {
+ this._commandService.executeCommand(SetStyleCommand.id, {
+ workbookId: this._workbook.getUnitId(),
+ worksheetId: this._worksheet.getSheetId(),
+ range: this._range,
+ style: {
+ type: 'bg',
+ value: {
+ rgb: color,
+ },
+ },
+ } as ISetStyleCommandParams<IColorStyle>);
+ }
+}
diff --git a/packages/uniscript/src/facade/sheet/f-selection.ts b/packages/uniscript/src/facade/sheet/f-selection.ts
new file mode 100644
index 0000000000..51125540c9
--- /dev/null
+++ b/packages/uniscript/src/facade/sheet/f-selection.ts
@@ -0,0 +1,23 @@
+import type { ISelectionWithStyle } from '@univerjs/base-sheets';
+import type { Workbook, Worksheet } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
+
+import { FRange } from './f-range';
+
+export class FSelection {
+ constructor(
+ private readonly _workbook: Workbook,
+ private readonly _worksheet: Worksheet,
+ private readonly _selections: Readonly<ISelectionWithStyle[]>,
+ @Inject(Injector) private readonly _injector: Injector
+ ) {}
+
+ getActiveRange(): FRange | null {
+ const active = this._selections.find((selection) => !!selection.primary);
+ if (!active) {
+ return null;
+ }
+
+ return this._injector.createInstance(FRange, this._workbook, this._worksheet, active.range);
+ }
+}
diff --git a/packages/uniscript/src/facade/sheet/f-workbook.ts b/packages/uniscript/src/facade/sheet/f-workbook.ts
new file mode 100644
index 0000000000..0dd1645109
--- /dev/null
+++ b/packages/uniscript/src/facade/sheet/f-workbook.ts
@@ -0,0 +1,20 @@
+import type { Workbook } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
+
+import { FWorksheet } from './f-worksheet';
+
+export class FWorkbook {
+ constructor(
+ private readonly _workbook: Workbook,
+ @Inject(Injector) private readonly _injector: Injector
+ ) {}
+
+ getActiveSheet(): FWorksheet | null {
+ const activeSheet = this._workbook.getActiveSheet();
+ if (!activeSheet) {
+ return null;
+ }
+
+ return this._injector.createInstance(FWorksheet, this._workbook, activeSheet);
+ }
+}
diff --git a/packages/uniscript/src/facade/sheet/f-worksheet.ts b/packages/uniscript/src/facade/sheet/f-worksheet.ts
new file mode 100644
index 0000000000..016b984201
--- /dev/null
+++ b/packages/uniscript/src/facade/sheet/f-worksheet.ts
@@ -0,0 +1,35 @@
+import { SelectionManagerService } from '@univerjs/base-sheets';
+import type { IRange, Workbook, Worksheet } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
+
+import { FRange } from './f-range';
+import { FSelection } from './f-selection';
+
+export class FWorksheet {
+ constructor(
+ private readonly _workbook: Workbook,
+ private readonly _worksheet: Worksheet,
+ @Inject(Injector) private readonly _injector: Injector,
+ @Inject(SelectionManagerService) private readonly _selectionManagerService: SelectionManagerService
+ ) {}
+
+ getSelection(): FSelection | null {
+ const selections = this._selectionManagerService.getSelections();
+ if (!selections) {
+ return null;
+ }
+
+ return this._injector.createInstance(FSelection, this._workbook, this._worksheet, selections);
+ }
+
+ getRange(row: number, col: number): FRange | null {
+ const range: IRange = {
+ startRow: row,
+ endRow: row,
+ startColumn: col,
+ endColumn: col,
+ };
+
+ return this._injector.createInstance(FRange, this._workbook, this._worksheet, range);
+ }
+}
diff --git a/packages/uniscript/src/facade/sheet/range.ts b/packages/uniscript/src/facade/sheet/range.ts
deleted file mode 100644
index b9aa16567b..0000000000
--- a/packages/uniscript/src/facade/sheet/range.ts
+++ /dev/null
@@ -1 +0,0 @@
-export class FRange {}
diff --git a/packages/uniscript/src/facade/sheet/sheet-facade.ts b/packages/uniscript/src/facade/sheet/sheet-facade.ts
deleted file mode 100644
index 8caab8cbb7..0000000000
--- a/packages/uniscript/src/facade/sheet/sheet-facade.ts
+++ /dev/null
@@ -1 +0,0 @@
-export class FSheet {}
diff --git a/packages/uniscript/src/facade/sheet/workbook.ts b/packages/uniscript/src/facade/sheet/workbook.ts
deleted file mode 100644
index 416096e8a4..0000000000
--- a/packages/uniscript/src/facade/sheet/workbook.ts
+++ /dev/null
@@ -1 +0,0 @@
-export class FWorkbook {}
diff --git a/packages/uniscript/src/facade/sheet/worksheet.ts b/packages/uniscript/src/facade/sheet/worksheet.ts
deleted file mode 100644
index 4f8bce7170..0000000000
--- a/packages/uniscript/src/facade/sheet/worksheet.ts
+++ /dev/null
@@ -1 +0,0 @@
-export class FWorksheet {}
diff --git a/packages/uniscript/src/services/command/batch-command.service.ts b/packages/uniscript/src/services/command/batch-command.service.ts
new file mode 100644
index 0000000000..8258931966
--- /dev/null
+++ b/packages/uniscript/src/services/command/batch-command.service.ts
@@ -0,0 +1,35 @@
+import type { CommandListener, ICommand, ICommandService, IExecutionOptions } from '@univerjs/core';
+import type { IDisposable } from '@wendellhu/redi';
+
+export class BatchCommandService implements ICommandService {
+ registerCommand(command: ICommand<object, boolean>): IDisposable {
+ throw new Error('Method not implemented.');
+ }
+
+ registerAsMultipleCommand(command: ICommand<object, boolean>): IDisposable {
+ throw new Error('Method not implemented.');
+ }
+
+ executeCommand<P extends object = object, R = boolean>(
+ id: string,
+ params?: P | undefined,
+ options?: IExecutionOptions | undefined
+ ): Promise<R> {
+ throw new Error('Method not implemented.');
+ }
+
+ syncExecuteCommand<P extends object = object, R = boolean>(
+ id: string,
+ params?: P | undefined,
+ options?: IExecutionOptions | undefined
+ ): R {
+ throw new Error('Method not implemented.');
+ }
+
+ onCommandExecuted(listener: CommandListener): IDisposable {
+ throw new Error('Method not implemented.');
+ }
+}
+
+// TODO: @wzhudev: the facade injector should register this command service to implement command batching
+// for performance reasons, and undo redo experience.
diff --git a/packages/uniscript/src/services/script-execution.service.ts b/packages/uniscript/src/services/script-execution.service.ts
index d5e85ec69a..d5fda82c6f 100644
--- a/packages/uniscript/src/services/script-execution.service.ts
+++ b/packages/uniscript/src/services/script-execution.service.ts
@@ -1,10 +1,32 @@
-import { Disposable } from '@univerjs/core';
+import { Disposable, ILogService } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
+
+import { FUniver } from '../facade/facade';
/**
* This service is to
*/
export class UniscriptExecutionService extends Disposable {
+ constructor(
+ @ILogService private readonly _logService: ILogService,
+ @Inject(Injector) private readonly _injector: Injector
+ ) {
+ super();
+ }
+
async execute(code: string): Promise<boolean> {
- return true;
+ this._logService.log('[UniscriptExecutionService]', 'executing Uniscript...');
+
+ const facadeInstance = FUniver.newInstance(this._injector);
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
+ const scriptFunction = new Function('Univer', `(() => {${code}})()`);
+
+ try {
+ scriptFunction(facadeInstance);
+ return true;
+ } catch (e) {
+ this._logService.error(e);
+ return false;
+ }
}
}
diff --git a/packages/uniscript/tsconfig.json b/packages/uniscript/tsconfig.json
new file mode 100644
index 0000000000..6c29f38b0b
--- /dev/null
+++ b/packages/uniscript/tsconfig.json
@@ -0,0 +1,15 @@
+{
+ "extends": "../../tsconfig.json",
+ "compilerOptions": {
+ "rootDir": "./",
+ "outDir": "lib",
+ "baseUrl": "./",
+ "esModuleInterop": true,
+ "strictPropertyInitialization": false
+ },
+ "include": [
+ "src/**/*",
+ "examples/**/*",
+ "test/**/*.test.(ts|tsx|js)"
+ ]
+}
\ No newline at end of file