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: add facade api addStyles and removeStyles (#5690)

wpxp1234560f802211140d
diff --git a/packages/core/src/sheets/styles.ts b/packages/core/src/sheets/styles.ts
index 9a3fbd2692..54a4e42c73 100644
--- a/packages/core/src/sheets/styles.ts
+++ b/packages/core/src/sheets/styles.ts
@@ -86,6 +86,18 @@ export class Styles {
return this.add(data, styleObject);
}
+ addCustomStyle(id: string, data: IStyleData): void {
+ this._styles[id] = data;
+ this._cacheMap.set(JSON.stringify(data), id);
+ }
+
+ remove(id: string): void {
+ if (this._styles[id]) {
+ delete this._styles[id];
+ this._cacheMap.delete(JSON.stringify(this._styles[id]));
+ }
+ }
+
toJSON(): IKeyType<Nullable<IStyleData>> {
return this._styles;
}
diff --git a/packages/core/src/sheets/workbook.ts b/packages/core/src/sheets/workbook.ts
index d4c4b8d8cb..644465c980 100644
--- a/packages/core/src/sheets/workbook.ts
+++ b/packages/core/src/sheets/workbook.ts
@@ -16,6 +16,7 @@
import type { Observable } from 'rxjs';
import type { Nullable } from '../shared';
+import type { IStyleData } from '../types/interfaces';
import type { CustomData, IRangeType, IWorkbookData, IWorksheetData } from './typedef';
import { BehaviorSubject, Subject } from 'rxjs';
import { UnitModel, UniverInstanceType } from '../common/unit';
@@ -197,6 +198,18 @@ export class Workbook extends UnitModel<IWorkbookData, UniverInstanceType.UNIVER
return this._styles;
}
+ addStyles(styles: Record<string, IStyleData>): void {
+ Object.entries(styles).forEach(([id, data]) => {
+ this._styles.addCustomStyle(id, data);
+ });
+ }
+
+ removeStyles(ids: string[]) {
+ ids.forEach((id) => {
+ this._styles.remove(id);
+ });
+ }
+
getConfig(): IWorkbookData {
return this._snapshot;
}
diff --git a/packages/sheets/src/facade/f-workbook.ts b/packages/sheets/src/facade/f-workbook.ts
index 3ffd00fb56..0dbaa09f55 100644
--- a/packages/sheets/src/facade/f-workbook.ts
+++ b/packages/sheets/src/facade/f-workbook.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { CommandListener, CustomData, ICommandInfo, IDisposable, IRange, IWorkbookData, IWorksheetData, LocaleType, Workbook } from '@univerjs/core';
+import type { CommandListener, CustomData, ICommandInfo, IDisposable, IRange, IStyleData, IWorkbookData, IWorksheetData, LocaleType, Workbook } from '@univerjs/core';
import type { ISetDefinedNameMutationParam } from '@univerjs/engine-formula';
import type { IInsertSheetCommandParams, IRangeThemeStyleJSON, ISetSelectionsOperationParams, ISheetCommandSharedParams } from '@univerjs/sheets';
import type { FontLine as _FontLine } from './f-range';
@@ -1046,6 +1046,84 @@ export class FWorkbook extends FBaseInitialable {
getCustomMetadata(): CustomData | undefined {
return this._workbook.getCustomMetadata();
}
+
+ /**
+ * Add styles to the workbook styles.
+ * @param {Record<string, IStyleData>} styles Styles to add
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ *
+ * // Add styles to the workbook styles
+ * const styles = {
+ * 'custom-style-1': {
+ * bg: {
+ * rgb: 'rgb(255, 0, 0)',
+ * }
+ * },
+ * 'custom-style-2': {
+ * fs: 20,
+ * n: {
+ * pattern: '@'
+ * }
+ * }
+ * };
+ * fWorkbook.addStyles(styles);
+ *
+ * // Set values with the new styles
+ * const fWorksheet = fWorkbook.getActiveSheet();
+ * const fRange = fWorksheet.getRange('A1:B2');
+ * fRange.setValues([
+ * [{ v: 'Hello', s: 'custom-style-1' }, { v: 'Univer', s: 'custom-style-1' }],
+ * [{ v: 'To', s: 'custom-style-1' }, { v: '0001', s: 'custom-style-2' }],
+ * ]);
+ * ```
+ */
+ addStyles(styles: Record<string, IStyleData>): void {
+ this._workbook.addStyles(styles);
+ }
+
+ /**
+ * Remove styles from the workbook styles.
+ * @param {string[]} styleKeys Style keys to remove
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ *
+ * // Add styles to the workbook styles
+ * const styles = {
+ * 'custom-style-1': {
+ * bg: {
+ * rgb: 'rgb(255, 0, 0)',
+ * }
+ * },
+ * 'custom-style-2': {
+ * fs: 20,
+ * n: {
+ * pattern: '@'
+ * }
+ * }
+ * };
+ * fWorkbook.addStyles(styles);
+ *
+ * // Set values with the new styles
+ * const fWorksheet = fWorkbook.getActiveSheet();
+ * const fRange = fWorksheet.getRange('A1:B2');
+ * fRange.setValues([
+ * [{ v: 'Hello', s: 'custom-style-1' }, { v: 'Univer', s: 'custom-style-1' }],
+ * [{ v: 'To', s: 'custom-style-1' }, { v: '0001', s: 'custom-style-2' }],
+ * ]);
+ *
+ * // Remove the style `custom-style-1` after 2 seconds
+ * setTimeout(() => {
+ * fWorkbook.removeStyles(['custom-style-1']);
+ * fWorksheet.refreshCanvas();
+ * }, 2000);
+ * ```
+ */
+ removeStyles(styleKeys: string[]): void {
+ this._workbook.removeStyles(styleKeys);
+ }
}
/**