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 Tools.set method (#2399)

siamea8f50cb6319
diff --git a/packages/core/src/shared/tools.ts b/packages/core/src/shared/tools.ts
index 5ab4695b73..939fe43072 100644
--- a/packages/core/src/shared/tools.ts
+++ b/packages/core/src/shared/tools.ts
@@ -671,4 +671,27 @@ export class Tools {
return !containsInvalidChars && isValidLength;
}
+
+ /**
+ * As lodash set, via a path string to set value to deep property
+ * set(obj, 'xx.yy', val)
+ * @param data
+ * @param propertyPath
+ * @param value
+ */
+ static set(data: Record<string, any>, propertyPath: string, value: any) {
+ const paths = propertyPath.split('.');
+ const key = paths.pop();
+
+ paths.forEach((prop) => {
+ if (!data[prop]) {
+ data[prop] = {};
+ }
+ data = data[prop];
+ });
+
+ if (key) {
+ data[key] = value;
+ }
+ }
}