@univerjs/sheets
Version:
UniverSheet normal base-sheets
1,229 lines • 232 kB
JavaScript
var tt = Object.defineProperty;
var nt = (r, e, t) => e in r ? tt(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
var p = (r, e, t) => nt(r, typeof e != "symbol" ? e + "" : e, t);
import { Inject as u, Injector as x, ICommandService as l, IPermissionService as ue, IAuthzIoService as Fe, LocaleService as He, generateRandomId as Oe, Rectangle as M, RANGE_TYPE as w, HorizontalAlign as T, VerticalAlign as A, TextStyleValue as we, RichTextValue as P, BooleanNumber as g, WrapStrategy as L, DEFAULT_STYLES as ke, covertCellValue as re, covertCellValues as Se, Dimension as N, isNullCell as D, ILogService as je, Direction as H, ObjectMatrix as J, IResourceLoaderService as rt, IUniverInstanceService as se, mergeWorksheetSnapshotWithDefault as Ce, Tools as ve, UndoCommand as st, RedoCommand as ot, toDisposable as Le, UniverInstanceType as U, CanceledError as f } from "@univerjs/core";
import { FBase as ge, FBaseInitialable as le, FUniver as ze, FEnum as it, FEventName as at } from "@univerjs/core/facade";
import { WorksheetProtectionRuleModel as Ge, RangeProtectionRuleModel as $e, WorksheetProtectionPointModel as qe, SCOPE_WORKBOOK_VALUE_DEFINED_NAME as F, SetDefinedNameCommand as oe, RemoveDefinedNameCommand as ht, PermissionPointsDefinitions as dt, WorkbookEditablePermission as Ke, UnitObject as W, AddWorksheetProtectionMutation as ct, DeleteWorksheetProtectionMutation as ut, getAllWorksheetPermissionPoint as gt, getAllWorksheetPermissionPointByPointPanel as fe, SetWorksheetPermissionPointsMutation as lt, AddRangeProtectionMutation as mt, DeleteRangeProtectionMutation as _t, SetRangeProtectionMutation as wt, WorksheetEditPermission as kt, WorksheetViewPermission as St, SetBorderBasicCommand as Ct, SetStyleCommand as b, SetTextRotationCommand as vt, SetRangeValuesCommand as B, SetTextWrapCommand as ie, SetVerticalTextAlignCommand as ft, SetHorizontalTextAlignCommand as It, addMergeCellsUtil as Q, getAddMergeMutationRangeByType as Ie, RemoveWorksheetMergeCommand as Rt, getPrimaryForRange as z, SetSelectionsOperation as me, SplitTextToColumnsCommand as bt, SetWorksheetRangeThemeStyleCommand as Et, DeleteWorksheetRangeThemeStyleCommand as pt, SheetRangeThemeService as Ye, ClearSelectionAllCommand as Je, ClearSelectionContentCommand as Qe, ClearSelectionFormatCommand as Xe, InsertRangeMoveDownCommand as yt, InsertRangeMoveRightCommand as Ut, DeleteRangeMoveUpCommand as xt, DeleteRangeMoveLeftCommand as Pt, getNextPrimaryCell as Nt, SheetsSelectionsService as Ze, SetWorksheetDefaultStyleMutation as Mt, SetColDataCommand as Re, SetRowDataCommand as be, copyRangeStyles as O, InsertRowByRangeCommand as Ee, RemoveRowByRangeCommand as Tt, MoveRowsCommand as At, SetRowHiddenCommand as pe, SetSpecificRowsVisibleCommand as ye, SetWorksheetRowIsAutoHeightMutation as Dt, SetRowHeightCommand as Ue, SetWorksheetRowIsAutoHeightCommand as X, InsertColByRangeCommand as xe, RemoveColByRangeCommand as Wt, MoveColsCommand as Bt, SetColHiddenCommand as Pe, SetSpecificColsVisibleCommand as Ne, SetColWidthCommand as Vt, SetFrozenCommand as Z, CancelFrozenCommand as Ft, ToggleGridlinesCommand as ae, SetGridlinesColorCommand as he, SetTabColorCommand as Ht, SetRangeValuesMutation as Me, SetWorksheetHideCommand as Ot, SetWorksheetShowCommand as jt, SetWorksheetNameCommand as de, AppendRowCommand as Lt, SetWorksheetRowCountCommand as zt, SetWorksheetColumnCountCommand as Gt, SetWorkbookNameCommand as $t, InsertSheetCommand as G, SetWorksheetActiveOperation as V, RemoveSheetCommand as ce, CopySheetCommand as qt, SetWorksheetOrderCommand as Kt, RegisterWorksheetRangeThemeStyleCommand as Yt, UnregisterWorksheetRangeThemeStyleCommand as Jt, RangeThemeStyle as Qt, SetWorksheetOrderMutation as Te, SetTabColorMutation as Ae, SetWorksheetHideMutation as De, COMMAND_LISTENER_VALUE_CHANGE as Xt, getValueChangedEffectedRange as Zt, SheetValueChangeType as en, SheetSkeletonChangeType as tn, SplitDelimiterEnum as nn } from "@univerjs/sheets";
import { IDefinedNamesService as et, serializeRange as _e, FormulaDataModel as rn, serializeRangeWithSheet as sn, deserializeRangeWithSheet as on } from "@univerjs/engine-formula";
var an = Object.getOwnPropertyDescriptor, hn = (r, e, t, n) => {
for (var s = n > 1 ? void 0 : n ? an(e, t) : e, o = r.length - 1, i; o >= 0; o--)
(i = r[o]) && (s = i(s) || s);
return s;
}, I = (r, e) => (t, n) => e(t, n, r);
function dn(r, e, t) {
const n = t.getDefinedNameMap(r);
if (n == null)
return e.t("definedName.defaultName") + 1;
const o = Array.from(Object.values(n)).length + 1, i = e.t("definedName.defaultName") + o;
if (t.getValueByName(r, i) == null)
return i;
let a = o + 1;
for (; ; ) {
const h = e.t("definedName.defaultName") + a;
if (t.getValueByName(r, h) == null)
return h;
a++;
}
}
class Y {
constructor() {
p(this, "_definedNameParam");
this._definedNameParam = {
id: Oe(10),
unitId: "",
name: "",
formulaOrRefString: "",
localSheetId: F
};
}
/**
* Sets the name of the defined name builder.
* @param {string} name The name of the defined name.
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setRef('Sheet1!$A$1')
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
setName(e) {
return this._definedNameParam.name = e, this;
}
/**
* Sets the formula of the defined name builder.
* @param {string }formula The formula of the defined name.
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setFormula('SUM(Sheet1!$A$1)')
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
setFormula(e) {
return this._definedNameParam.formulaOrRefString = `=${e}`, this;
}
/**
* Sets the reference of the defined name builder.
* @param {string} a1Notation The reference of the defined name.
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setRef('Sheet1!$A$1')
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
setRef(e) {
return this._definedNameParam.formulaOrRefString = e, this;
}
/**
* Sets the reference of the defined name builder by range .
* @param {number} row The start row index of the range. index start at 0.
* @param {number} column The start column index of the range. index start at 0.
* @param {number} numRows The number of rows in the range.
* @param {number} numColumns The number of columns in the range.
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setRefByRange(1, 3, 2, 5) // D2:H3
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
setRefByRange(e, t, n, s) {
return this._definedNameParam.formulaOrRefString = _e({
startRow: e,
endRow: e + (n != null ? n : 1) - 1,
startColumn: t,
endColumn: t + (s != null ? s : 1) - 1
}), this;
}
/**
* Sets the comment of the defined name builder.
* @param {string} comment The comment of the defined name.
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setRef('Sheet1!$A$1')
* .setComment('A reference to A1 cell in Sheet1')
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
setComment(e) {
return this._definedNameParam.comment = e, this;
}
/**
* Sets the scope of the defined name to the worksheet.
* @param {FWorksheet} worksheet The worksheet to set the scope to.
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const sheets = fWorkbook.getSheets();
*
* // Create a defined name and make it available only in the second worksheet
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setRef('Sheet1!$A$1')
* .setScopeToWorksheet(sheets[1])
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
setScopeToWorksheet(e) {
return this._definedNameParam.localSheetId = e.getSheetId(), this;
}
/**
* Sets the scope of the defined name to the workbook.
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
*
* // Create a defined name and make it available in the entire workbook
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setRef('Sheet1!$A$1')
* .setScopeToWorkbook()
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
setScopeToWorkbook() {
return this._definedNameParam.localSheetId = F, this;
}
/**
* Sets the hidden status of the defined name builder.
* @param {boolean} hidden The hidden status of the defined name.
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setRef('Sheet1!$A$1')
* .setHidden(true)
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
setHidden(e) {
return this._definedNameParam.hidden = e, this;
}
/**
* Builds the defined name parameter.
* @returns {ISetDefinedNameMutationParam} The defined name mutation parameter.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedNameBuilder = univerAPI.newDefinedName()
* .setName('MyDefinedName')
* .setRef('Sheet1!$A$1')
* .setComment('A reference to A1 cell in Sheet1')
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
build() {
return this._definedNameParam;
}
/**
* Loads the defined name mutation parameter.
* @param {ISetDefinedNameMutationParam} param - defined name mutation parameter
* @returns {FDefinedNameBuilder} The instance of `FDefinedNameBuilder` for method chaining.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedNameParam = {
* id: '4TMPceoqg8',
* unitId: fWorkbook.getId(),
* name: 'MyDefinedName',
* formulaOrRefString: 'Sheet1!$A$1',
* }
* const definedNameBuilder = univerAPI.newDefinedName()
* .load(definedNameParam)
* .build();
* fWorkbook.insertDefinedNameBuilder(definedNameBuilder);
* ```
*/
load(e) {
return this._definedNameParam = e, this;
}
}
let $ = class extends ge {
constructor(r, e, t, n, s, o, i, a, h, d) {
super(), this._definedNameParam = r, this._injector = e, this._commandService = t, this._permissionService = n, this._worksheetProtectionRuleModel = s, this._rangeProtectionRuleModel = o, this._worksheetProtectionPointRuleModel = i, this._authzIoService = a, this._localeService = h, this._definedNamesService = d;
}
_apply() {
this._definedNameParam.name === "" && (this._definedNameParam.name = dn(this._definedNameParam.unitId, this._localeService, this._definedNamesService)), this._commandService.syncExecuteCommand(oe.id, this._definedNameParam);
}
/**
* Gets the name of the defined name.
* @returns {string} The name of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* console.log(definedName?.getName());
* ```
*/
getName() {
return this._definedNameParam.name;
}
/**
* Sets the name of the defined name.
* @param {string} name The name of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.setName('NewDefinedName');
* ```
*/
setName(r) {
this._definedNameParam.name = r, this._apply();
}
/**
* Sets the formula of the defined name.
* @param {string} formula The formula of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.setFormula('SUM(Sheet1!$A$1)');
* ```
*/
setFormula(r) {
this._definedNameParam.formulaOrRefString = `=${r}`, this._apply();
}
/**
* Sets the reference of the defined name.
* @param {string} refString The reference of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.setRef('Sheet1!$A$1');
* ```
*/
setRef(r) {
this._definedNameParam.formulaOrRefString = r, this._apply();
}
/**
* Gets the formula or reference string of the defined name.
* @returns {string} The formula or reference string of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* console.log(definedName?.getFormulaOrRefString());
* ```
*/
getFormulaOrRefString() {
return this._definedNameParam.formulaOrRefString;
}
/**
* Sets the reference of the defined name by range.
* @param {number} row The start row of the range.
* @param {number} column The start column of the range.
* @param {number} numRows The number of rows in the range.
* @param {number} numColumns The number of columns in the range.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.setRefByRange(1, 3, 2, 5); // D2:H3
* ```
*/
setRefByRange(r, e, t, n) {
this._definedNameParam.formulaOrRefString = _e({
startRow: r,
endRow: r + (t != null ? t : 1) - 1,
startColumn: e,
endColumn: e + (n != null ? n : 1) - 1
}), this._apply();
}
/**
* Gets the comment of the defined name.
* @returns {string | undefined} The comment of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* console.log(definedName?.getComment());
* ```
*/
getComment() {
return this._definedNameParam.comment;
}
/**
* Sets the comment of the defined name.
* @param {string} comment The comment of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.setComment('This is a comment');
* ```
*/
setComment(r) {
this._definedNameParam.comment = r, this._apply();
}
/**
* Sets the scope of the defined name to the worksheet.
* @param {FWorksheet} worksheet The worksheet to set the scope to.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const sheets = fWorkbook.getSheets();
*
* // Get the first defined name and make it available only in the second worksheet
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.setScopeToWorksheet(sheets[1]);
* ```
*/
setScopeToWorksheet(r) {
this._definedNameParam.localSheetId = r.getSheetId(), this._apply();
}
/**
* Sets the scope of the defined name to the workbook.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.setScopeToWorkbook();
* ```
*/
setScopeToWorkbook() {
this._definedNameParam.localSheetId = F, this._apply();
}
/**
* Sets the hidden status of the defined name.
* @param {boolean} hidden The hidden status of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.setHidden(true);
* ```
*/
setHidden(r) {
this._definedNameParam.hidden = r, this._apply();
}
/**
* Deletes the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* definedName?.delete();
* ```
*/
delete() {
this._commandService.syncExecuteCommand(ht.id, this._definedNameParam);
}
/**
* Gets the local sheet id of the defined name.
* @returns {string | undefined} The local sheet id of the defined name.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* console.log(definedName?.getLocalSheetId());
* ```
*/
getLocalSheetId() {
return this._definedNameParam.localSheetId;
}
/**
* Checks if the defined name is in the workbook scope.
* @returns {boolean} True if the defined name is in the workbook scope, false otherwise.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* console.log(definedName?.isWorkbookScope());
* ```
*/
isWorkbookScope() {
return this._definedNameParam.localSheetId === F;
}
/**
* Converts the defined name to a defined name builder.
* @returns {FDefinedNameBuilder} The defined name builder.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const definedName = fWorkbook.getDefinedNames()[0];
* if (!definedName) return;
* const definedNameBuilder = definedName
* .toBuilder()
* .setName('NewDefinedName')
* .setFormula('SUM(Sheet1!$A$1)')
* .build();
* fWorkbook.updateDefinedNameBuilder(definedNameBuilder);
* ```
*/
toBuilder() {
const r = this._injector.createInstance(Y);
return r.load(this._definedNameParam), r;
}
};
$ = hn([
I(1, u(x)),
I(2, l),
I(3, ue),
I(4, u(Ge)),
I(5, u($e)),
I(6, u(qe)),
I(7, u(Fe)),
I(8, u(He)),
I(9, et)
], $);
var cn = Object.getOwnPropertyDescriptor, un = (r, e, t, n) => {
for (var s = n > 1 ? void 0 : n ? cn(e, t) : e, o = r.length - 1, i; o >= 0; o--)
(i = r[o]) && (s = i(s) || s);
return s;
}, y = (r, e) => (t, n) => e(t, n, r);
let q = class extends ge {
constructor(e, t, n, s, o, i, a) {
super();
/**
* Permission point definition, can read the point constructor want to modify from here
*/
p(this, "permissionPointsDefinition", dt);
/**
* An observable object used to monitor permission change events within a range, thereby triggering corresponding subsequent processing.
*/
p(this, "rangeRuleChangedAfterAuth$");
/**
* An observable object used to monitor permission change events within a worksheet, thereby triggering corresponding subsequent processing.
*/
p(this, "sheetRuleChangedAfterAuth$");
this._injector = e, this._commandService = t, this._permissionService = n, this._worksheetProtectionRuleModel = s, this._rangeProtectionRuleModel = o, this._worksheetProtectionPointRuleModel = i, this._authzIoService = a, this.rangeRuleChangedAfterAuth$ = this._rangeProtectionRuleModel.ruleRefresh$, this.sheetRuleChangedAfterAuth$ = this._worksheetProtectionRuleModel.ruleRefresh$;
}
/**
* Configures a specific permission point for a workbook.
* This function sets or updates a permission point for a workbook identified by `unitId`.
* It creates a new permission point if it does not already exist, and updates the point with the provided value.
* @param {string} unitId - The unique identifier of the workbook for which the permission is being set.
* @param {WorkbookPermissionPointConstructor} FPointClass - The constructor function for creating a permission point instance. Other point constructors can See the [permission-point documentation](https://github.com/dream-num/univer/tree/dev/packages/sheets/src/services/permission/permission-point) for more details.
* @param {boolean} value - The boolean value to determine whether the permission point is enabled or disabled.
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* permission.setWorkbookPermissionPoint(unitId, permission.permissionPointsDefinition.WorkbookEditablePermission, false)
* ```
*/
setWorkbookPermissionPoint(e, t, n) {
const s = new t(e);
this._permissionService.getPermissionPoint(s.id) || this._permissionService.addPermissionPoint(s), this._permissionService.updatePermissionPoint(s.id, n);
}
/**
* This function is used to set whether the workbook can be edited
* @param {string} unitId - The unique identifier of the workbook for which the permission is being set.
* @param {boolean} value - A value that controls whether the workbook can be edited
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* permission.setWorkbookEditPermission(unitId, false);
* ```
*/
setWorkbookEditPermission(e, t) {
this.setWorkbookPermissionPoint(e, Ke, t);
}
/**
* This function is used to add a base permission for a worksheet.
* Note that after adding, only the background mask of the permission module will be rendered. If you want to modify the function permissions,
* you need to modify the permission points with the permissionId returned by this function.
* @param {string} unitId - The unique identifier of the workbook for which the permission is being set.
* @param {string} subUnitId - The unique identifier of the worksheet for which the permission is being set.
* @returns {Promise<string | undefined>} - Returns the `permissionId` if the permission is successfully added. If the operation fails or no result is returned, it resolves to `undefined`.
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* const worksheet = workbook.getActiveSheet();
* const subUnitId = worksheet.getSheetId();
* // Note that there will be no permission changes after this step is completed. It only returns an ID for subsequent permission changes.
* // For details, please see the example of the **`setWorksheetPermissionPoint`** API.
* const permissionId = await permission.addWorksheetBasePermission(unitId, subUnitId)
* // Can still edit and read it.
* console.log('debugger', permissionId)
* ```
*/
async addWorksheetBasePermission(e, t) {
if (this._rangeProtectionRuleModel.getSubunitRuleList(e, t).length > 0)
throw new Error("sheet protection cannot intersect with range protection");
const s = await this._authzIoService.create({
objectType: W.Worksheet,
worksheetObject: {
collaborators: [],
unitID: e,
strategies: [],
name: "",
scope: void 0
}
});
if (this._commandService.syncExecuteCommand(ct.id, {
unitId: e,
subUnitId: t,
rule: {
permissionId: s,
unitType: W.Worksheet,
unitId: e,
subUnitId: t
}
}))
return s;
}
/**
* Delete the entire table protection set for the worksheet and reset the point permissions of the worksheet to true
* @param {string} unitId - The unique identifier of the workbook for which the permission is being set.
* @param {string} subUnitId - The unique identifier of the worksheet for which the permission is being set.
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* const worksheet = workbook.getActiveSheet();
* const subUnitId = worksheet.getSheetId();
* permission.removeWorksheetPermission(unitId, subUnitId);
* ```
*/
removeWorksheetPermission(e, t) {
this._commandService.syncExecuteCommand(ut.id, {
unitId: e,
subUnitId: t
}), [...gt(), ...fe()].forEach((n) => {
const s = new n(e, t);
this._permissionService.updatePermissionPoint(s.id, !0);
}), this._worksheetProtectionPointRuleModel.deleteRule(e, t);
}
/**
* Sets the worksheet permission point by updating or adding the permission point for the worksheet.
* If the worksheet doesn't have a base permission, it creates one to used render
* @param {string} unitId - The unique identifier of the workbook.
* @param {string} subUnitId - The unique identifier of the worksheet.
* @param {WorkSheetPermissionPointConstructor} FPointClass - The constructor for the permission point class.
* See the [permission-point documentation](https://github.com/dream-num/univer/tree/dev/packages/sheets/src/services/permission/permission-point) for more details.
* @param {boolean} value - The new permission value to be set for the worksheet.
* @returns {Promise<string | undefined>} - Returns the `permissionId` if the permission point is successfully set or created. If no permission is set, it resolves to `undefined`.
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* const worksheet = workbook.getActiveSheet();
* const subUnitId = worksheet.getSheetId();
* const permissionId = await permission.addWorksheetBasePermission(unitId, subUnitId)
* // After this line of code , the worksheet will no longer be editable
* permission.setWorksheetPermissionPoint(unitId, subUnitId, permission.permissionPointsDefinition.WorksheetEditPermission, false);
* ```
*/
async setWorksheetPermissionPoint(e, t, n, s) {
const o = this._worksheetProtectionRuleModel.getRule(e, t);
let i;
if (n === kt || n === St)
if (o)
i = o.permissionId;
else {
if (this._rangeProtectionRuleModel.getSubunitRuleList(e, t).length > 0)
throw new Error("sheet protection cannot intersect with range protection");
i = await this.addWorksheetBasePermission(e, t);
}
else {
const c = this._worksheetProtectionPointRuleModel.getRule(e, t);
c ? i = c.permissionId : (i = await this._authzIoService.create({
objectType: W.Worksheet,
worksheetObject: {
collaborators: [],
unitID: e,
strategies: [],
name: "",
scope: void 0
}
}), this._commandService.syncExecuteCommand(lt.id, { unitId: e, subUnitId: t, rule: { unitId: e, subUnitId: t, permissionId: i } }));
}
const h = new n(e, t);
return this._permissionService.getPermissionPoint(h.id) || this._permissionService.addPermissionPoint(h), this._permissionService.updatePermissionPoint(h.id, s), i;
}
/**
* Adds a range protection to the worksheet.
* Note that after adding, only the background mask of the permission module will be rendered. If you want to modify the function permissions,
* you need to modify the permission points with the permissionId returned by this function.
* @param {string} unitId - The unique identifier of the workbook.
* @param {string} subUnitId - The unique identifier of the worksheet.
* @param {FRange[]} ranges - The ranges to be protected.
* @returns {Promise<{ permissionId: string, ruleId: string } | undefined>} - Returns an object containing the `permissionId` and `ruleId` if the range protection is successfully added. If the operation fails or no result is returned, it resolves to `undefined`. permissionId is used to stitch permission point ID,ruleId is used to store permission rules
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* const worksheet = workbook.getActiveSheet();
* const subUnitId = worksheet.getSheetId();
* const range = worksheet.getRange('A1:B2');
* const ranges = [];
* ranges.push(range);
* // Note that there will be no permission changes after this step is completed. It only returns an ID for subsequent permission changes.
* // For details, please see the example of the **`setRangeProtectionPermissionPoint`** API.
* const res = await permission.addRangeBaseProtection(unitId, subUnitId, ranges);
* const {permissionId, ruleId} = res;
* console.log('debugger', permissionId, ruleId);
* ```
*/
async addRangeBaseProtection(e, t, n) {
const s = await this._authzIoService.create({
objectType: W.SelectRange,
selectRangeObject: {
collaborators: [],
unitID: e,
name: "",
scope: void 0
}
}), o = `ruleId_${Oe(6)}`;
if (this._worksheetProtectionRuleModel.getRule(e, t))
throw new Error("sheet protection cannot intersect with range protection");
if (this._rangeProtectionRuleModel.getSubunitRuleList(e, t).some((c) => c.ranges.some((C) => n.some((_) => M.intersects(_.getRange(), C)))))
throw new Error("range protection cannot intersect");
if (this._commandService.syncExecuteCommand(mt.id, {
unitId: e,
subUnitId: t,
rules: [{
permissionId: s,
unitType: W.SelectRange,
unitId: e,
subUnitId: t,
ranges: n.map((c) => c.getRange()),
id: o
}]
}))
return {
permissionId: s,
ruleId: o
};
}
/**
* Removes the range protection from the worksheet.
* @param {string} unitId - The unique identifier of the workbook.
* @param {string} subUnitId - The unique identifier of the worksheet.
* @param {string[]} ruleIds - The rule IDs of the range protection to be removed.
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* const worksheet = workbook.getActiveSheet();
* const subUnitId = worksheet.getSheetId();
* const range = worksheet.getRange('A1:B2');
* const ranges = [];
* ranges.push(range);
* const res = await permission.addRangeBaseProtection(unitId, subUnitId, ranges);
* const ruleId = res.ruleId;
* permission.removeRangeProtection(unitId, subUnitId, [ruleId]);
* ```
*/
removeRangeProtection(e, t, n) {
this._commandService.syncExecuteCommand(_t.id, {
unitId: e,
subUnitId: t,
ruleIds: n
}) && this._rangeProtectionRuleModel.getSubunitRuleList(e, t).length === 0 && (this._worksheetProtectionPointRuleModel.deleteRule(e, t), [...fe()].forEach((i) => {
const a = new i(e, t);
this._permissionService.updatePermissionPoint(a.id, a.value);
}));
}
/**
* Modify the permission points of a custom area
* @param {string} unitId - The unique identifier of the workbook.
* @param {string} subUnitId - The unique identifier of the worksheet within the workbook.
* @param {string} permissionId - The unique identifier of the permission that controls access to the range.
* @param {RangePermissionPointConstructor} FPointClass - The constructor for the range permission point class.
* See the [permission-point documentation](https://github.com/dream-num/univer/tree/dev/packages/sheets/src/services/permission/permission-point) for more details.
* @param {boolean} value - The new permission value to be set for the range (e.g., true for allowing access, false for restricting access).
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* const worksheet = workbook.getActiveSheet();
* const subUnitId = worksheet.getSheetId();
* const range = worksheet.getRange('A1:B2');
* const ranges = [];
* ranges.push(range);
* // Note that there will be no permission changes after this step is completed. It only returns an ID for subsequent permission changes.
* // For details, please see the example of the **`setRangeProtectionPermissionPoint`** API.
* const res = await permission.addRangeBaseProtection(unitId, subUnitId, ranges);
* const {permissionId, ruleId} = res;
* // After passing the following line of code, the range set above will become uneditable
* permission.setRangeProtectionPermissionPoint(unitId,subUnitId,permissionId, permission.permissionPointsDefinition.RangeProtectionPermissionEditPoint, false);
* ```
*/
setRangeProtectionPermissionPoint(e, t, n, s, o) {
const i = new s(e, t, n);
this._permissionService.getPermissionPoint(i.id) || this._permissionService.addPermissionPoint(i), this._permissionService.updatePermissionPoint(i.id, o);
}
/**
* Sets the ranges for range protection in a worksheet.
*
* This method finds the rule by unitId, subUnitId, and ruleId, and updates the rule with the provided ranges.
* It checks for overlaps with existing ranges in the same subunit and shows an error message if any overlap is detected.
* If no overlap is found, it executes the command to update the range protection with the new ranges.
* @param {string} unitId - The unique identifier of the workbook.
* @param {string} subUnitId - The unique identifier of the worksheet within the workbook.
* @param {string} ruleId - The ruleId of the range protection rule that is being updated.
* @param {FRange[]} ranges - The array of new ranges to be set for the range protection rule.
*
* @example
* ```typescript
* const workbook = univerAPI.getActiveWorkbook();
* const permission = workbook.getPermission();
* const unitId = workbook.getId();
* const worksheet = workbook.getActiveSheet();
* const subUnitId = worksheet.getSheetId();
* const range = worksheet.getRange('A1:B2');
* const ranges = [];
* ranges.push(range);
* const res = await permission.addRangeBaseProtection(unitId, subUnitId, ranges);
* const {permissionId, ruleId} = res;
* const newRange = worksheet.getRange('C1:D2');
* permission.setRangeProtectionRanges(unitId, subUnitId, ruleId, [newRange]);
* ```
*/
setRangeProtectionRanges(e, t, n, s) {
const o = this._rangeProtectionRuleModel.getRule(e, t, n);
if (o) {
if (this._rangeProtectionRuleModel.getSubunitRuleList(e, t).filter((h) => h.id !== n).some((h) => h.ranges.some((d) => s.some((c) => M.intersects(c.getRange(), d)))))
throw new Error("range protection cannot intersect");
this._commandService.syncExecuteCommand(wt.id, {
unitId: e,
subUnitId: t,
ruleId: n,
rule: {
...o,
ranges: s.map((h) => h.getRange())
}
});
}
}
};
q = un([
y(0, u(x)),
y(1, l),
y(2, ue),
y(3, u(Ge)),
y(4, u($e)),
y(5, u(qe)),
y(6, u(Fe))
], q);
function gn(r) {
switch (r) {
case "left":
return T.LEFT;
case "center":
return T.CENTER;
case "normal":
return T.RIGHT;
default:
throw new Error(`Invalid horizontal alignment: ${r}`);
}
}
function We(r) {
switch (r) {
case T.LEFT:
return "left";
case T.CENTER:
return "center";
case T.RIGHT:
return "normal";
default:
return "general";
}
}
function ln(r) {
switch (r) {
case "top":
return A.TOP;
case "middle":
return A.MIDDLE;
case "bottom":
return A.BOTTOM;
default:
throw new Error(`Invalid vertical alignment: ${r}`);
}
}
function Be(r) {
switch (r) {
case A.TOP:
return "top";
case A.MIDDLE:
return "middle";
case A.BOTTOM:
return "bottom";
default:
return "general";
}
}
function ee(r, e) {
return {
startRow: r.startRow,
endRow: r.endRow,
startColumn: 0,
endColumn: e.getColumnCount() - 1,
rangeType: w.ROW
};
}
function te(r, e) {
return {
startRow: 0,
endRow: e.getRowCount() - 1,
startColumn: r.startColumn,
endColumn: r.endColumn,
rangeType: w.COLUMN
};
}
var mn = Object.getOwnPropertyDescriptor, _n = (r, e, t, n) => {
for (var s = n > 1 ? void 0 : n ? mn(e, t) : e, o = r.length - 1, i; o >= 0; o--)
(i = r[o]) && (s = i(s) || s);
return s;
}, ne = (r, e) => (t, n) => e(t, n, r);
let m = class extends le {
constructor(r, e, t, n, s, o) {
super(n), this._workbook = r, this._worksheet = e, this._range = t, this._injector = n, this._commandService = s, this._formulaDataModel = o;
}
/**
* Get the unit ID of the current workbook
* @returns {string} The unit ID of the workbook
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getUnitId());
* ```
*/
getUnitId() {
return this._workbook.getUnitId();
}
/**
* Gets the name of the worksheet
* @returns {string} The name of the worksheet
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getSheetName());
* ```
*/
getSheetName() {
return this._worksheet.getName();
}
/**
* Gets the ID of the worksheet
* @returns {string} The ID of the worksheet
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getSheetId());
* ```
*/
getSheetId() {
return this._worksheet.getSheetId();
}
/**
* Gets the area where the statement is applied
* @returns {IRange} The area where the statement is applied
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* const range = fRange.getRange();
* const { startRow, startColumn, endRow, endColumn } = range;
* console.log(range);
* ```
*/
getRange() {
return this._range;
}
/**
* Gets the starting row index of the range. index starts at 0.
* @returns {number} The starting row index of the range.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getRow()); // 0
* ```
*/
getRow() {
return this._range.startRow;
}
/**
* Gets the ending row index of the range. index starts at 0.
* @returns {number} The ending row index of the range.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getLastRow()); // 1
* ```
*/
getLastRow() {
return this._range.endRow;
}
/**
* Gets the starting column index of the range. index starts at 0.
* @returns {number} The starting column index of the range.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getColumn()); // 0
* ```
*/
getColumn() {
return this._range.startColumn;
}
/**
* Gets the ending column index of the range. index starts at 0.
* @returns {number} The ending column index of the range.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getLastColumn()); // 1
* ```
*/
getLastColumn() {
return this._range.endColumn;
}
/**
* Gets the width of the applied area
* @returns {number} The width of the area
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getWidth());
* ```
*/
getWidth() {
return this._range.endColumn - this._range.startColumn + 1;
}
/**
* Gets the height of the applied area
* @returns {number} The height of the area
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getHeight());
* ```
*/
getHeight() {
return this._range.endRow - this._range.startRow + 1;
}
/**
* Return range whether this range is merged
* @returns {boolean} if true is merged
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.isMerged());
* // merge cells A1:B2
* fRange.merge();
* console.log(fRange.isMerged());
* ```
*/
isMerged() {
const { startColumn: r, startRow: e, endColumn: t, endRow: n } = this._range;
return this._worksheet.getMergedCellRange(e, r, n, t).some((o) => M.equals(o, this._range));
}
/**
* Return first cell style data in this range. Please note that if there are row styles, col styles and (or)
* worksheet style, they will be merged into the cell style. You can use `type` to specify the type of the style to get.
*
* @param {GetStyleType} type - The type of the style to get. 'row' means get the composed style of row, col and
* default worksheet style. 'col' means get the composed style of col, row and default worksheet style.
* 'cell' means get the style of cell without merging row style, col style and default worksheet style.
* Default is 'row'.
*
* @returns {IStyleData | null} The cell style data
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getCellStyleData());
* ```
*/
getCellStyleData(r = "row") {
return r !== "cell" ? this._worksheet.getComposedCellStyle(this._range.startRow, this._range.startColumn, r === "row") : this._worksheet.getCellStyle(this._range.startRow, this._range.startColumn);
}
/**
* Get the font family of the cell.
*
* @param {GetStyleType} type - The type of the style to get. 'row' means get the composed style of row, col and
* default worksheet style. 'col' means get the composed style of col, row and default worksheet style.
* 'cell' means get the style of cell without merging row style, col style and default worksheet style.
* Default is 'row'.
*
* @returns {string | null} The font family of the cell
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getFontFamily());
* ```
*/
getFontFamily(r = "row") {
var e, t;
return (t = (e = this.getCellStyleData(r)) == null ? void 0 : e.ff) != null ? t : null;
}
/**
* Get the font size of the cell.
*
* @param {GetStyleType} type - The type of the style to get. 'row' means get the composed style of row, col and
* default worksheet style. 'col' means get the composed style of col, row and default worksheet style.
* 'cell' means get the style of cell without merging row style, col style and default worksheet style.
* Default is 'row'.
*
* @returns {number | null} The font size of the cell
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getFontSize());
* ```
*/
getFontSize(r = "row") {
var e, t;
return (t = (e = this.getCellStyleData(r)) == null ? void 0 : e.fs) != null ? t : null;
}
/**
* Return first cell style in this range.
*
* @param {GetStyleType} type - The type of the style to get. 'row' means get the composed style of row, col and
* default worksheet style. 'col' means get the composed style of col, row and default worksheet style.
* 'cell' means get the style of cell without merging row style, col style and default worksheet style.
* Default is 'row'.
*
* @returns {TextStyleValue | null} The cell style
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getCellStyle());
* ```
*/
getCellStyle(r = "row") {
const e = this.getCellStyleData(r);
return e ? we.create(e) : null;
}
/**
* Returns the cell styles for the cells in the range.
*
* @param {GetStyleType} type - The type of the style to get. 'row' means get the composed style of row, col and
* default worksheet style. 'col' means get the composed style of col, row and default worksheet style.
* 'cell' means get the style of cell without merging row style, col style and default worksheet style.
* Default is 'row'.
*
* @returns {Array<Array<TextStyleValue | null>>} A two-dimensional array of cell styles.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* console.log(fRange.getCellStyles());
* ```
*/
getCellStyles(r = "row") {
return this.getCellDatas().map((t, n) => t.map((s, o) => {
if (!s) return null;
const i = r !== "cell" ? this._worksheet.getComposedCellStyle(n + this._range.startRow, o + this._range.startColumn, r === "row") : this._worksheet.getCellStyle(n + this._range.startRow, o + this._range.startColumn);
return i ? we.create(i) : null;
}));
}
getValue(r) {
var e, t;
return r ? this.getValueAndRichTextValue() : (t = (e = this._worksheet.getCell(this._range.startRow, this._range.startColumn)) == null ? void 0 : e.v) != null ? t : null;
}
/**
* Returns the raw value of the top-left cell in the range. Empty cells return `null`.
* @returns {Nullable<CellValue>} The raw value of the cell. Returns `null` if the cell is empty.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* fRange.setValueForCell({
* v: 0.2,
* s: {
* n: {
* pattern: '0%',
* },
* },
* });
* console.log(fRange.getRawValue()); // 0.2
* ```
*/
getRawValue() {
var e;
const r = this._worksheet.getCellMatrix().getValue(this._range.startRow, this._range.startColumn);
return r != null && r.p && ((e = r.p.body) != null && e.dataStream) ? r.p.body.dataStream : r != null && r.v ? r.v : null;
}
/**
* Returns the displayed value of the top-left cell in the range. The value is a String. Empty cells return an empty string.
* @returns {string} The displayed value of the cell. Returns an empty string if the cell is empty.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* fRange.setValueForCell({
* v: 0.2,
* s: {
* n: {
* pattern: '0%',
* },
* },
* });
* console.log(fRange.getDisplayValue()); // 20%
* ```
*/
getDisplayValue() {
var e;
const r = this._worksheet.getCell(this._range.startRow, this._range.startColumn);
return r != null && r.p && ((e = r.p.body) != null && e.dataStream) ? r.p.body.dataStream : r != null && r.v ? String(r.v) : "";
}
getValues(r) {
var i, a;
if (r)
return this.getValueAndRichTextValues();
const { startRow: e, endRow: t, startColumn: n, endColumn: s } = this._range, o = [];
for (let h = e; h <= t; h++) {
const d = [];
for (let c = n; c <= s; c++)
d.push((a = (i = this._worksheet.getCell(h, c)) == null ? void 0 : i.v) != null ? a : null);
o.push(d);
}
return o;
}
/**
* Returns a two-dimensional array of the range raw values. Empty cells return `null`.
* @returns {Array<Array<Nullable<CellValue>>>} The raw value of the cell. Returns `null` if the cell is empty.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:B2');
* fRange.setValues([
* [
* {