@univerjs/sheets-formula 0.1.9 → 0.1.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -768
- package/lib/cjs/index.js +8 -8
- package/lib/es/index.js +3432 -2216
- package/lib/types/commands/operations/__tests__/create-command-test-bed.d.ts +2 -2
- package/lib/types/commands/operations/editor-formula.operation.d.ts +3 -3
- package/lib/types/common/selection.d.ts +1 -1
- package/lib/types/controllers/__tests__/create-command-test-bed.d.ts +1 -1
- package/lib/types/controllers/active-dirty.controller.d.ts +1 -1
- package/lib/types/controllers/array-formula-display.controller.d.ts +2 -2
- package/lib/types/controllers/defined-name.controller.d.ts +2 -2
- package/lib/types/controllers/formula-alert.controller.d.ts +12 -0
- package/lib/types/controllers/formula-auto-fill.controller.d.ts +2 -2
- package/lib/types/controllers/formula-clipboard.controller.d.ts +3 -3
- package/lib/types/controllers/formula-editor-show.controller.d.ts +7 -6
- package/lib/types/controllers/formula-render.controller.d.ts +11 -0
- package/lib/types/controllers/formula-ui.controller.d.ts +6 -3
- package/lib/types/controllers/menu.d.ts +1 -1
- package/lib/types/controllers/prompt.controller.d.ts +8 -8
- package/lib/types/controllers/trigger-calculation.controller.d.ts +3 -3
- package/lib/types/controllers/update-formula.controller.d.ts +4 -3
- package/lib/types/controllers/utils/__tests__/utils.spec.d.ts +16 -0
- package/lib/types/controllers/utils/offset-formula-data.d.ts +4 -2
- package/lib/types/controllers/utils/ref-range-formula.d.ts +1 -1
- package/lib/types/controllers/utils/utils.d.ts +8 -1
- package/lib/types/formula-ui-plugin.d.ts +3 -3
- package/lib/types/index.d.ts +1 -1
- package/lib/types/locale/en-US.d.ts +15 -0
- package/lib/types/locale/function-list/array/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/compatibility/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/cube/ru-RU.d.ts +144 -0
- package/lib/types/locale/function-list/database/ru-RU.d.ts +234 -0
- package/lib/types/locale/function-list/date/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/engineering/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/financial/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/information/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/logical/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/lookup/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/math/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/statistical/ru-RU.d.ts +3 -0
- package/lib/types/locale/function-list/text/ru-RU.d.ts +852 -0
- package/lib/types/locale/function-list/univer/ru-RU.d.ts +17 -0
- package/lib/types/locale/function-list/web/ru-RU.d.ts +72 -0
- package/lib/types/locale/index.d.ts +1 -0
- package/lib/types/locale/ru-RU.d.ts +9276 -0
- package/lib/types/locale/zh-CN.d.ts +15 -0
- package/lib/types/services/description.service.d.ts +4 -4
- package/lib/types/services/formula-custom-function.service.d.ts +4 -5
- package/lib/types/services/formula-ref-range.service.d.ts +3 -3
- package/lib/types/services/prompt.service.d.ts +4 -4
- package/lib/types/services/register-function.service.d.ts +6 -7
- package/lib/types/services/register-other-formula.service.d.ts +2 -2
- package/lib/types/services/utils.d.ts +2 -1
- package/lib/types/views/more-functions/function-help/FunctionHelp.d.ts +15 -1
- package/lib/types/views/more-functions/input-params/InputParams.d.ts +1 -1
- package/lib/types/views/more-functions/select-function/SelectFunction.d.ts +1 -1
- package/lib/umd/index.js +7 -7
- package/package.json +27 -24
package/README.md
CHANGED
|
@@ -25,774 +25,9 @@ npm install @univerjs/sheets-formula
|
|
|
25
25
|
pnpm add @univerjs/sheets-formula
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
###
|
|
28
|
+
### How To Customize Formulas
|
|
29
29
|
|
|
30
30
|
If the officially provided formula does not meet your needs, you can expand the formula yourself. Depending on different needs, we provide multiple ways to support registering one or more custom formulas.
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
### How to add formulas using Uniscript
|
|
35
|
-
|
|
36
|
-
Using Uniscript scripts, you can quickly and easily register custom formulas in the current Univer instance.
|
|
37
|
-
|
|
38
|
-
As shown in the following case, use `registerFunction` to register the algorithm, name, and description required by a `CUSTOMSUM` formula into the formula plug-in at one time. After execution, the formula can be used. Enter `=CUSTOMSUM` in any blank cell to see the prompt.
|
|
39
|
-
|
|
40
|
-
```js
|
|
41
|
-
univerAPI.registerFunction({
|
|
42
|
-
calculate: [
|
|
43
|
-
[function (...variants) {
|
|
44
|
-
let sum = 0;
|
|
45
|
-
|
|
46
|
-
for(const variant of variants){
|
|
47
|
-
sum += Number(variant) || 0;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return sum;
|
|
51
|
-
}, 'CUSTOMSUM', 'Adds its arguments'],
|
|
52
|
-
// ... more formulas
|
|
53
|
-
]
|
|
54
|
-
})
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
Use the `unregisterFunction` method to quickly unregister custom formulas
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
univerAPI.unregisterFunction({
|
|
61
|
-
functionNames: ['CUSTOMSUM']
|
|
62
|
-
})
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
If you want to provide more complete international content and description, you can also configure the `locales` and `description` fields. As follows.
|
|
66
|
-
|
|
67
|
-
```js
|
|
68
|
-
const FUNCTION_NAMES_USER = {
|
|
69
|
-
CUSTOMSUM: 'CUSTOMSUM'
|
|
70
|
-
}
|
|
71
|
-
univerAPI.registerFunction({
|
|
72
|
-
locales:{
|
|
73
|
-
'zhCN': {
|
|
74
|
-
formulaCustom: {
|
|
75
|
-
CUSTOMSUM: {
|
|
76
|
-
description: '将单个值、单元格引用或是区域相加,或者将三者的组合相加。',
|
|
77
|
-
abstract: '求参数的和',
|
|
78
|
-
links: [
|
|
79
|
-
{
|
|
80
|
-
title: '教学',
|
|
81
|
-
url: 'https://support.microsoft.com/zh-cn/office/sum-%E5%87%BD%E6%95%B0-043e1c7d-7726-4e80-8f32-07b23e057f89',
|
|
82
|
-
},
|
|
83
|
-
],
|
|
84
|
-
functionParameter: {
|
|
85
|
-
number1: {
|
|
86
|
-
name: '数值1',
|
|
87
|
-
detail: '要相加的第一个数字。 该数字可以是 4 之类的数字,B6 之类的单元格引用或 B2:B8 之类的单元格范围。',
|
|
88
|
-
},
|
|
89
|
-
number2: {
|
|
90
|
-
name: '数值2',
|
|
91
|
-
detail: '这是要相加的第二个数字。 可以按照这种方式最多指定 255 个数字。',
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
// ... more formulas
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
'enUS':{
|
|
99
|
-
formulaCustom:{
|
|
100
|
-
CUSTOMSUM: {
|
|
101
|
-
description: `You can add individual values, cell references or ranges or a mix of all three.`,
|
|
102
|
-
abstract: `Adds its arguments`,
|
|
103
|
-
links: [
|
|
104
|
-
{
|
|
105
|
-
title: 'Instruction',
|
|
106
|
-
url: 'https://support.microsoft.com/en-us/office/sum-function-043e1c7d-7726-4e80-8f32-07b23e057f89',
|
|
107
|
-
},
|
|
108
|
-
],
|
|
109
|
-
functionParameter: {
|
|
110
|
-
number1: {
|
|
111
|
-
name: 'number1',
|
|
112
|
-
detail: 'The first number you want to add. The number can be like 4, a cell reference like B6, or a cell range like B2:B8.',
|
|
113
|
-
},
|
|
114
|
-
number2: {
|
|
115
|
-
name: 'number2',
|
|
116
|
-
detail: 'This is the second number you want to add. You can specify up to 255 numbers in this way.',
|
|
117
|
-
},
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
},
|
|
123
|
-
description:[
|
|
124
|
-
{
|
|
125
|
-
functionName: FUNCTION_NAMES_USER.CUSTOMSUM,
|
|
126
|
-
aliasFunctionName: 'formulaCustom.CUSTOMSUM.aliasFunctionName',
|
|
127
|
-
functionType: 15,
|
|
128
|
-
description: 'formulaCustom.CUSTOMSUM.description',
|
|
129
|
-
abstract: 'formulaCustom.CUSTOMSUM.abstract',
|
|
130
|
-
functionParameter: [
|
|
131
|
-
{
|
|
132
|
-
name: 'formulaCustom.CUSTOMSUM.functionParameter.number1.name',
|
|
133
|
-
detail: 'formulaCustom.CUSTOMSUM.functionParameter.number1.detail',
|
|
134
|
-
example: 'A1:A20',
|
|
135
|
-
require: 1,
|
|
136
|
-
repeat: 0,
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
name: 'formulaCustom.CUSTOMSUM.functionParameter.number2.name',
|
|
140
|
-
detail: 'formulaCustom.CUSTOMSUM.functionParameter.number2.detail',
|
|
141
|
-
example: 'B2:B10',
|
|
142
|
-
require: 0,
|
|
143
|
-
repeat: 1,
|
|
144
|
-
},
|
|
145
|
-
],
|
|
146
|
-
},
|
|
147
|
-
// ... more formulas
|
|
148
|
-
],
|
|
149
|
-
calculate: [
|
|
150
|
-
[function (...variants) {
|
|
151
|
-
let sum = 0;
|
|
152
|
-
|
|
153
|
-
for(const variant of variants){
|
|
154
|
-
sum += Number(variant) || 0;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return sum;
|
|
158
|
-
}, FUNCTION_NAMES_USER.CUSTOMSUM],
|
|
159
|
-
// ... more formulas
|
|
160
|
-
]
|
|
161
|
-
})
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
Note
|
|
165
|
-
|
|
166
|
-
- Multiple languages can be set under `locales`. For naming rules, please refer to [LocaleType](/api/core/enums/LocaleType.html). Translations for multiple formulas can be added under `functionList`. For detailed field descriptions, please refer to the [How to add formulas in UniverFormulaEnginePlugin](./#how-to-add-formulas-in-univerformulaengineplugin) section.
|
|
167
|
-
- `description` sets the description of the custom formula.
|
|
168
|
-
- `calculate` writes the specific algorithm and name mapping of the calculation formula. The input parameter is the content entered by the user when using the formula, which may be a number, a string, a Boolean value, or a range, and the same format is returned.
|
|
169
|
-
|
|
170
|
-
Likewise, if using the `unregisterFunction` method, it is recommended that you remove the internationalization files as well. The example below removes the Chinese and English `formulaCustom` nodes.
|
|
171
|
-
|
|
172
|
-
```ts
|
|
173
|
-
univerAPI.unregisterFunction({
|
|
174
|
-
localeKeys: {
|
|
175
|
-
'zhCN': ['formulaCustom'],
|
|
176
|
-
'enUS': ['formulaCustom'],
|
|
177
|
-
},
|
|
178
|
-
functionNames: ['CUSTOMSUM']
|
|
179
|
-
})
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
Uniscript uses `@univerjs/facade` under the hood. You can also use Uniscript-like APIs directly in your project. Please refer to [Registering Function](/en-us/guides/facade/register-function).
|
|
183
|
-
|
|
184
|
-
### How to add formulas when initializing Univer
|
|
185
|
-
|
|
186
|
-
Follow the steps below to implement a custom formula `CUSTOMSUM`.
|
|
187
|
-
|
|
188
|
-
You can create a new `custom-function.ts` file to specifically place custom formula-related modules, or write it directly before `univer` is initialized.
|
|
189
|
-
|
|
190
|
-
1. Define formula name
|
|
191
|
-
|
|
192
|
-
First, give the formula a name. We require that it cannot be repeated with the name of the existing formula. The existing formula is mainly from [Office Excel](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb) refer to.
|
|
193
|
-
|
|
194
|
-
We collect multiple custom formulas in an enumeration.
|
|
195
|
-
|
|
196
|
-
```ts
|
|
197
|
-
/**
|
|
198
|
-
* function name
|
|
199
|
-
*/
|
|
200
|
-
export enum FUNCTION_NAMES_USER {
|
|
201
|
-
CUSTOMSUM = "CUSTOMSUM",
|
|
202
|
-
}
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
2. Define internationalization
|
|
206
|
-
|
|
207
|
-
Define the international content you need. For detailed field descriptions, please refer to the [How to add formulas in UniverFormulaEnginePlugin](./#how-to-add-formulas-in-univerformulaengineplugin) section. Similarly, multiple formulas are distinguished by using the formula name as the `key` value.
|
|
208
|
-
|
|
209
|
-
```ts
|
|
210
|
-
/**
|
|
211
|
-
*i18n
|
|
212
|
-
*/
|
|
213
|
-
export const functionEnUS = {
|
|
214
|
-
formulaCustom: {
|
|
215
|
-
CUSTOMSUM: {
|
|
216
|
-
description: `You can add individual values, cell references or ranges or a mix of all three.`,
|
|
217
|
-
abstract: `Adds its arguments`,
|
|
218
|
-
links: [
|
|
219
|
-
{
|
|
220
|
-
title: 'Instruction',
|
|
221
|
-
url: 'https://support.microsoft.com/en-us/office/sum-function-043e1c7d-7726-4e80-8f32-07b23e057f89',
|
|
222
|
-
},
|
|
223
|
-
],
|
|
224
|
-
functionParameter: {
|
|
225
|
-
number1: {
|
|
226
|
-
name: 'number1',
|
|
227
|
-
detail: 'The first number you want to add. The number can be like 4, a cell reference like B6, or a cell range like B2:B8.',
|
|
228
|
-
},
|
|
229
|
-
number2: {
|
|
230
|
-
name: 'number2',
|
|
231
|
-
detail: 'This is the second number you want to add. You can specify up to 255 numbers in this way.',
|
|
232
|
-
},
|
|
233
|
-
},
|
|
234
|
-
},
|
|
235
|
-
},
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
export const functionZhCN = {
|
|
239
|
-
formulaCustom: {
|
|
240
|
-
CUSTOMSUM: {
|
|
241
|
-
description: '将单个值、单元格引用或是区域相加,或者将三者的组合相加。',
|
|
242
|
-
abstract: '求参数的和',
|
|
243
|
-
links: [
|
|
244
|
-
{
|
|
245
|
-
title: '教学',
|
|
246
|
-
url: 'https://support.microsoft.com/zh-cn/office/sum-%E5%87%BD%E6%95%B0-043e1c7d-7726-4e80-8f32-07b23e057f89',
|
|
247
|
-
},
|
|
248
|
-
],
|
|
249
|
-
functionParameter: {
|
|
250
|
-
number1: {
|
|
251
|
-
name: '数值1',
|
|
252
|
-
detail: '要相加的第一个数字。 该数字可以是 4 之类的数字,B6 之类的单元格引用或 B2:B8 之类的单元格范围。',
|
|
253
|
-
},
|
|
254
|
-
number2: {
|
|
255
|
-
name: '数值2',
|
|
256
|
-
detail: '这是要相加的第二个数字。 可以按照这种方式最多指定 255 个数字。',
|
|
257
|
-
},
|
|
258
|
-
},
|
|
259
|
-
},
|
|
260
|
-
},
|
|
261
|
-
};
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
3. Registration internationalization
|
|
265
|
-
|
|
266
|
-
Expand the internationalization content you defined in the original internationalization object.
|
|
267
|
-
|
|
268
|
-
```ts
|
|
269
|
-
export const locales = {
|
|
270
|
-
[LocaleType.EN_US]: {
|
|
271
|
-
...UniverSheetsEnUS,
|
|
272
|
-
...UniverDocsUIEnUS,
|
|
273
|
-
...UniverSheetsUIEnUS,
|
|
274
|
-
...UniverUiEnUS,
|
|
275
|
-
...UniverDesignEnUS,
|
|
276
|
-
...functionEnUS,
|
|
277
|
-
},
|
|
278
|
-
[LocaleType.ZH_CN]: {
|
|
279
|
-
...functionZhCN,
|
|
280
|
-
},
|
|
281
|
-
};
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
4. Definition description
|
|
285
|
-
|
|
286
|
-
The description of the formula mainly configures internationalized fields, which are used for formula search prompts, details panels, etc.
|
|
287
|
-
|
|
288
|
-
```ts
|
|
289
|
-
import type { IFunctionInfo } from '@univerjs/engine-formula';
|
|
290
|
-
import { FunctionType } from '@univerjs/engine-formula';
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* description
|
|
294
|
-
*/
|
|
295
|
-
export const FUNCTION_LIST_USER: IFunctionInfo[] = [
|
|
296
|
-
{
|
|
297
|
-
functionName: FUNCTION_NAMES_USER.CUSTOMSUM,
|
|
298
|
-
aliasFunctionName: 'formulaCustom.CUSTOMSUM.aliasFunctionName',
|
|
299
|
-
functionType: FunctionType.User,
|
|
300
|
-
description: 'formulaCustom.CUSTOMSUM.description',
|
|
301
|
-
abstract: 'formulaCustom.CUSTOMSUM.abstract',
|
|
302
|
-
functionParameter: [
|
|
303
|
-
{
|
|
304
|
-
name: 'formulaCustom.CUSTOMSUM.functionParameter.number1.name',
|
|
305
|
-
detail: 'formulaCustom.CUSTOMSUM.functionParameter.number1.detail',
|
|
306
|
-
example: 'A1:A20',
|
|
307
|
-
require: 1,
|
|
308
|
-
repeat: 0,
|
|
309
|
-
},
|
|
310
|
-
{
|
|
311
|
-
name: 'formulaCustom.CUSTOMSUM.functionParameter.number2.name',
|
|
312
|
-
detail: 'formulaCustom.CUSTOMSUM.functionParameter.number2.detail',
|
|
313
|
-
example: 'B2:B10',
|
|
314
|
-
require: 0,
|
|
315
|
-
repeat: 1,
|
|
316
|
-
},
|
|
317
|
-
],
|
|
318
|
-
},
|
|
319
|
-
];
|
|
320
|
-
```
|
|
321
|
-
|
|
322
|
-
5. Registration description
|
|
323
|
-
|
|
324
|
-
Pass in the description object you defined when registering the formula plug-in.
|
|
325
|
-
|
|
326
|
-
```ts
|
|
327
|
-
// universal
|
|
328
|
-
univer.registerPlugin(UniverSheetsFormulaPlugin, {
|
|
329
|
-
description: FUNCTION_LIST_USER,
|
|
330
|
-
});
|
|
331
|
-
```
|
|
332
|
-
|
|
333
|
-
6. Define formula algorithm
|
|
334
|
-
|
|
335
|
-
Write specific formula calculation logic, map algorithms and formula names.
|
|
336
|
-
|
|
337
|
-
```ts
|
|
338
|
-
import type { ArrayValueObject, BaseValueObject, IFunctionInfo } from '@univerjs/engine-formula';
|
|
339
|
-
import { BaseFunction, FunctionType, NumberValueObject } from '@univerjs/engine-formula';
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* Function algorithm
|
|
343
|
-
*/
|
|
344
|
-
export class Customsum extends BaseFunction {
|
|
345
|
-
override calculate(...variants: BaseValueObject[]) {
|
|
346
|
-
let accumulatorAll: BaseValueObject = NumberValueObject.create(0);
|
|
347
|
-
for (let i = 0; i < variants.length; i++) {
|
|
348
|
-
let variant = variants[i];
|
|
349
|
-
|
|
350
|
-
if (variant.isError()) {
|
|
351
|
-
return variant;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
if (accumulatorAll.isError()) {
|
|
355
|
-
return accumulatorAll;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
if (variant.isArray()) {
|
|
359
|
-
variant = (variant as ArrayValueObject).sum();
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
accumulatorAll = accumulatorAll.plus(variant as BaseValueObject);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
return accumulatorAll;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// Mapping of algorithms and names
|
|
370
|
-
export const functionUser = [[Customsum, FUNCTION_NAMES_USER.CUSTOMSUM]];
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
7. Registration formula algorithm
|
|
374
|
-
|
|
375
|
-
Pass in the formula algorithm object you defined in `UniverFormulaEnginePlugin`.
|
|
376
|
-
|
|
377
|
-
```ts
|
|
378
|
-
univer.registerPlugin(UniverFormulaEnginePlugin, {
|
|
379
|
-
function: functionUser,
|
|
380
|
-
});
|
|
381
|
-
```
|
|
382
|
-
|
|
383
|
-
Please note: If `UniverFormulaEnginePlugin` is instantiated in `worker`, you need to register the formula algorithm in `UniverFormulaEnginePlugin` in `worker`, otherwise the custom formula cannot be executed.
|
|
384
|
-
|
|
385
|
-
8. Test
|
|
386
|
-
|
|
387
|
-
At this point, the development of the custom formula is completed, and now it is time to test it. Enter `=CUSTOMSUM` in any blank cell and expect to get a formula prompt. Here is a [Custom Formula Demo](/playground?title=Custom%20Function) for reference.
|
|
388
|
-
|
|
389
|
-
### How to add formulas in third-party plug-ins
|
|
390
|
-
|
|
391
|
-
If you are developing a Univer plug-in, you can add custom formulas directly to the plug-in to facilitate code management in a plug-in repository.
|
|
392
|
-
|
|
393
|
-
Our internal `UniverFormulaEnginePlugin` plug-in provides a `function.service` specifically for registering formula descriptions and algorithms.
|
|
394
|
-
|
|
395
|
-
First refer to [Custom Plugin](https://univer.ai/guides/customization/write-a-plugin/) to create a new plug-in, and then you can start adding custom formulas.
|
|
396
|
-
|
|
397
|
-
1. Create a new `custom-function.ts` file in the `common` file and write all the basic modules required for the formula.
|
|
398
|
-
|
|
399
|
-
```ts
|
|
400
|
-
import type { ArrayValueObject, BaseValueObject, IFunctionInfo } from '@univerjs/engine-formula';
|
|
401
|
-
import { BaseFunction, FunctionType, NumberValueObject } from '@univerjs/engine-formula';
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* function name
|
|
405
|
-
*/
|
|
406
|
-
export enum FUNCTION_NAMES_USER {
|
|
407
|
-
CUSTOMSUM = 'CUSTOMSUM',
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
*i18n
|
|
412
|
-
*/
|
|
413
|
-
export const functionEnUS = {
|
|
414
|
-
formulaCustom: {
|
|
415
|
-
CUSTOMSUM: {
|
|
416
|
-
description: `You can add individual values, cell references or ranges or a mix of all three.`,
|
|
417
|
-
abstract: `Adds its arguments`,
|
|
418
|
-
links: [
|
|
419
|
-
{
|
|
420
|
-
title: 'Instruction',
|
|
421
|
-
url: 'https://support.microsoft.com/en-us/office/sum-function-043e1c7d-7726-4e80-8f32-07b23e057f89',
|
|
422
|
-
},
|
|
423
|
-
],
|
|
424
|
-
functionParameter: {
|
|
425
|
-
number1: {
|
|
426
|
-
name: 'number1',
|
|
427
|
-
detail: 'The first number you want to add. The number can be like 4, a cell reference like B6, or a cell range like B2:B8.',
|
|
428
|
-
},
|
|
429
|
-
number2: {
|
|
430
|
-
name: 'number2',
|
|
431
|
-
detail: 'This is the second number you want to add. You can specify up to 255 numbers in this way.',
|
|
432
|
-
},
|
|
433
|
-
},
|
|
434
|
-
},
|
|
435
|
-
},
|
|
436
|
-
};
|
|
437
|
-
|
|
438
|
-
export const functionZhCN = {
|
|
439
|
-
formulaCustom: {
|
|
440
|
-
CUSTOMSUM: {
|
|
441
|
-
description: '将单个值、单元格引用或是区域相加,或者将三者的组合相加。',
|
|
442
|
-
abstract: '求参数的和',
|
|
443
|
-
links: [
|
|
444
|
-
{
|
|
445
|
-
title: '教学',
|
|
446
|
-
url: 'https://support.microsoft.com/zh-cn/office/sum-%E5%87%BD%E6%95%B0-043e1c7d-7726-4e80-8f32-07b23e057f89',
|
|
447
|
-
},
|
|
448
|
-
],
|
|
449
|
-
functionParameter: {
|
|
450
|
-
number1: {
|
|
451
|
-
name: '数值1',
|
|
452
|
-
detail: '要相加的第一个数字。 该数字可以是 4 之类的数字,B6 之类的单元格引用或 B2:B8 之类的单元格范围。',
|
|
453
|
-
},
|
|
454
|
-
number2: {
|
|
455
|
-
name: '数值2',
|
|
456
|
-
detail: '这是要相加的第二个数字。 可以按照这种方式最多指定 255 个数字。',
|
|
457
|
-
},
|
|
458
|
-
},
|
|
459
|
-
},
|
|
460
|
-
},
|
|
461
|
-
};
|
|
462
|
-
|
|
463
|
-
/**
|
|
464
|
-
* description
|
|
465
|
-
*/
|
|
466
|
-
export const FUNCTION_LIST_USER: IFunctionInfo[] = [
|
|
467
|
-
{
|
|
468
|
-
functionName: FUNCTION_NAMES_USER.CUSTOMSUM,
|
|
469
|
-
aliasFunctionName: 'formulaCustom.CUSTOMSUM.aliasFunctionName',
|
|
470
|
-
functionType: FunctionType.User,
|
|
471
|
-
description: 'formulaCustom.CUSTOMSUM.description',
|
|
472
|
-
abstract: 'formulaCustom.CUSTOMSUM.abstract',
|
|
473
|
-
functionParameter: [
|
|
474
|
-
{
|
|
475
|
-
name: 'formulaCustom.CUSTOMSUM.functionParameter.number1.name',
|
|
476
|
-
detail: 'formulaCustom.CUSTOMSUM.functionParameter.number1.detail',
|
|
477
|
-
example: 'A1:A20',
|
|
478
|
-
require: 1,
|
|
479
|
-
repeat: 0,
|
|
480
|
-
},
|
|
481
|
-
{
|
|
482
|
-
name: 'formulaCustom.CUSTOMSUM.functionParameter.number2.name',
|
|
483
|
-
detail: 'formulaCustom.CUSTOMSUM.functionParameter.number2.detail',
|
|
484
|
-
example: 'B2:B10',
|
|
485
|
-
require: 0,
|
|
486
|
-
repeat: 1,
|
|
487
|
-
},
|
|
488
|
-
],
|
|
489
|
-
},
|
|
490
|
-
];
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Function algorithm
|
|
494
|
-
*/
|
|
495
|
-
export class Customsum extends BaseFunction {
|
|
496
|
-
override calculate(...variants: BaseValueObject[]) {
|
|
497
|
-
let accumulatorAll: BaseValueObject = NumberValueObject.create(0);
|
|
498
|
-
for (let i = 0; i < variants.length; i++) {
|
|
499
|
-
let variant = variants[i];
|
|
500
|
-
|
|
501
|
-
if (variant.isError()) {
|
|
502
|
-
return variant;
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
if (accumulatorAll.isError()) {
|
|
506
|
-
return accumulatorAll;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
if (variant.isArray()) {
|
|
510
|
-
variant = (variant as ArrayValueObject).sum();
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
accumulatorAll = accumulatorAll.plus(variant as BaseValueObject);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
return accumulatorAll;
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
export const functionUser = [[Customsum, FUNCTION_NAMES_USER.CUSTOMSUM]];
|
|
521
|
-
```
|
|
522
|
-
|
|
523
|
-
2. Create a new `custom-description.controller.ts` under the `controllers` folder to register formula internationalization content and description.
|
|
524
|
-
|
|
525
|
-
```ts
|
|
526
|
-
import { Disposable, LifecycleStages, LocaleService, OnLifecycle } from '@univerjs/core';
|
|
527
|
-
import { Inject } from '@wendellhu/redi';
|
|
528
|
-
|
|
529
|
-
import { FUNCTION_LIST_USER, functionEnUS, functionZhCN } from '../common/custom-function';
|
|
530
|
-
import { IDescriptionService } from '../services/description.service';
|
|
531
|
-
|
|
532
|
-
@OnLifecycle(LifecycleStages.Ready, CustomDescriptionController)
|
|
533
|
-
export class CustomDescriptionController extends Disposable {
|
|
534
|
-
constructor(
|
|
535
|
-
@IDescriptionService private readonly _descriptionService: IDescriptionService,
|
|
536
|
-
@Inject(LocaleService) private readonly _localeService: LocaleService
|
|
537
|
-
) {
|
|
538
|
-
super();
|
|
539
|
-
|
|
540
|
-
this._initialize();
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
private _initialize(): void {
|
|
544
|
-
this._registerLocales();
|
|
545
|
-
this._registerCustomDescriptions();
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
private _registerLocales() {
|
|
549
|
-
this._localeService.load({
|
|
550
|
-
zhCN: functionZhCN,
|
|
551
|
-
enUS: functionEnUS,
|
|
552
|
-
});
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
private _registerCustomDescriptions() {
|
|
556
|
-
this._descriptionService.registerDescription(FUNCTION_LIST_USER);
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
```
|
|
560
|
-
|
|
561
|
-
3. Create a new `custom-function.controller.ts` under the `controllers` folder to register formula algorithms.
|
|
562
|
-
|
|
563
|
-
```ts
|
|
564
|
-
import { Disposable, LifecycleStages, OnLifecycle } from '@univerjs/core';
|
|
565
|
-
import type { BaseFunction, IFunctionNames } from '@univerjs/engine-formula';
|
|
566
|
-
import { IFunctionService } from '@univerjs/engine-formula';
|
|
567
|
-
import { type Ctor } from '@wendellhu/redi';
|
|
568
|
-
|
|
569
|
-
import { functionUser } from '../common/custom-function';
|
|
570
|
-
|
|
571
|
-
@OnLifecycle(LifecycleStages.Ready, CustomFunctionController)
|
|
572
|
-
export class CustomFunctionController extends Disposable {
|
|
573
|
-
constructor(@IFunctionService private readonly _functionService: IFunctionService) {
|
|
574
|
-
super();
|
|
575
|
-
|
|
576
|
-
this._initialize();
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
private _initialize(): void {
|
|
580
|
-
this._registerCustomFunctions();
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
private _registerCustomFunctions() {
|
|
584
|
-
const functions: BaseFunction[] = [...functionUser].map((registerObject) => {
|
|
585
|
-
const Func = registerObject[0] as Ctor<BaseFunction>;
|
|
586
|
-
const name = registerObject[1] as IFunctionNames;
|
|
587
|
-
|
|
588
|
-
return new Func(name);
|
|
589
|
-
});
|
|
590
|
-
|
|
591
|
-
this._functionService.registerExecutors(...functions);
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
```
|
|
595
|
-
|
|
596
|
-
4. In the plug-in entry file `plugin.ts`, register `custom-description.controller.ts` and `custom-function.controller.ts` into the DI system.
|
|
597
|
-
|
|
598
|
-
```ts
|
|
599
|
-
initialize(): void {
|
|
600
|
-
// ... other logic
|
|
601
|
-
|
|
602
|
-
const dependencies: Dependency[] = [
|
|
603
|
-
// ... other modules
|
|
604
|
-
[CustomFunctionController],
|
|
605
|
-
[CustomDescriptionController],
|
|
606
|
-
];
|
|
607
|
-
|
|
608
|
-
dependencies.forEach((dependency) => this._injector.add(dependency));
|
|
609
|
-
}
|
|
610
|
-
```
|
|
611
|
-
|
|
612
|
-
Start Univer and enter `=CUSTOMSUM` in any blank cell to test this newly added formula.
|
|
613
|
-
|
|
614
|
-
Please note: If `UniverFormulaEnginePlugin` is instantiated in `worker`, you need to register the formula algorithm in `worker`, otherwise the custom formula cannot be executed.
|
|
615
|
-
In addition to registering through the `UniverFormulaEnginePlugin` configuration, the formula algorithm module can also be separately packaged as a plug-in for registration.
|
|
616
|
-
|
|
617
|
-
First of all, there is no need to register `CustomFunctionController` in `plugin.ts`. Create a new `custom-function-plugin.ts` in the same directory, specifically for registering `CustomFunctionController`.
|
|
618
|
-
|
|
619
|
-
```ts
|
|
620
|
-
import { Plugin, UniverInstanceType } from '@univerjs/core';
|
|
621
|
-
import type { Dependency } from '@wendellhu/redi';
|
|
622
|
-
import { Inject, Injector } from '@wendellhu/redi';
|
|
623
|
-
|
|
624
|
-
import { FORMULA_UI_PLUGIN_NAME } from './common/plugin-name';
|
|
625
|
-
import { CustomFunctionController } from './controllers/custom-function.controller';
|
|
626
|
-
|
|
627
|
-
export class UniverSheetsCustomFunctionPlugin extends Plugin {
|
|
628
|
-
static override pluginName = FORMULA_UI_PLUGIN_NAME
|
|
629
|
-
static override type = UniverInstanceType.UNIVER_SHEET;
|
|
630
|
-
|
|
631
|
-
constructor(@Inject(Injector) override readonly _injector: Injector) {
|
|
632
|
-
super();
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
initialize(): void {
|
|
636
|
-
const dependencies: Dependency[] = [[CustomFunctionController]];
|
|
637
|
-
|
|
638
|
-
dependencies.forEach((dependency) => this._injector.add(dependency));
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
override onReady(): void {
|
|
642
|
-
this.initialize();
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
```
|
|
646
|
-
|
|
647
|
-
Then export it in `index.ts`
|
|
648
|
-
|
|
649
|
-
```ts
|
|
650
|
-
export { UniverSheetsFormulaPlugin } from './formula-ui-plugin';
|
|
651
|
-
```
|
|
652
|
-
|
|
653
|
-
Finally, your `worker` entry initializes the plugin.
|
|
654
|
-
|
|
655
|
-
```ts
|
|
656
|
-
import { UniverSheetsCustomFunctionPlugin } from '@univerjs/sheets-formula';
|
|
657
|
-
|
|
658
|
-
// ...initialize other plugins
|
|
659
|
-
univer.registerPlugin(UniverSheetsCustomFunctionPlugin);
|
|
660
|
-
```
|
|
661
|
-
|
|
662
|
-
This way you can register the formula in `worker`.
|
|
663
|
-
|
|
664
|
-
### How to add formulas in UniverFormulaEnginePlugin
|
|
665
|
-
|
|
666
|
-
#### Reference Documentation
|
|
667
|
-
|
|
668
|
-
[Office Excel functions (by category)](https://support.microsoft.com/en-us/office/excel-functions-by-category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb)
|
|
669
|
-
|
|
670
|
-
#### Categories
|
|
671
|
-
|
|
672
|
-
Detailed API reference [FunctionType](/api/engine-formula/enums/FunctionType.html)
|
|
673
|
-
|
|
674
|
-
- Financial
|
|
675
|
-
- Date
|
|
676
|
-
- Math
|
|
677
|
-
- Statistical
|
|
678
|
-
- Lookup
|
|
679
|
-
- Database
|
|
680
|
-
- Text
|
|
681
|
-
- Logical
|
|
682
|
-
- Information
|
|
683
|
-
- Engineering
|
|
684
|
-
- Cube
|
|
685
|
-
- Compatibility
|
|
686
|
-
- Web
|
|
687
|
-
- Array
|
|
688
|
-
- Univer
|
|
689
|
-
- User
|
|
690
|
-
|
|
691
|
-
#### Requirements
|
|
692
|
-
|
|
693
|
-
To implement a formula, you need to add formula description, internationalization, and formula algorithm. Take the `SUMIF` function as an example for reference.
|
|
694
|
-
|
|
695
|
-
1. Add Function Name
|
|
696
|
-
|
|
697
|
-
Location: [packages/engine-formula/src/functions/math/function-names.ts](https://github.com/dream-num/univer/blob/dev/packages/engine-formula/src/functions/math/function-names.ts).
|
|
698
|
-
|
|
699
|
-
Each category has a folder containing a `function-names` file to manage all function names in that category. Add the function name, which will be used in the `sheets-formula` plugin.
|
|
700
|
-
|
|
701
|
-
Note that a function in Excel may belong to multiple categories. For example, `FLOOR` appears in Compatibility and Math Functions, and we classify it under the Math category. Other functions are treated similarly, based on the exact classification.
|
|
702
|
-
|
|
703
|
-
> Most Excel functions have already written function names. New functions can be added at the end
|
|
704
|
-
|
|
705
|
-
2. Internationalization Files
|
|
706
|
-
|
|
707
|
-
Location: [packages/sheets-formula/src/locale/function-list/math/en-US.ts](https://github.com/dream-num/univer/blob/dev/packages/sheets-formula/src/locale/function-list/math/en-US.ts).
|
|
708
|
-
|
|
709
|
-
Internationalization is organized by category, with a file for each category. Refer to the Office function category page for a brief overview.
|
|
710
|
-

|
|
711
|
-
|
|
712
|
-
Refer to the Office function details page for function descriptions and parameter descriptions.
|
|
713
|
-

|
|
714
|
-
|
|
715
|
-
Most function names already have basic description, abstract, links, and parameter structures. It is recommended to modify them based on this foundation. If a function is not present, add it to the end.
|
|
716
|
-
|
|
717
|
-
Requirements:
|
|
718
|
-
|
|
719
|
-
- Use the English names of function parameters as the `key` for translation, e.g., `SUMIF`. Generally, do not modify unless there is an error.
|
|
720
|
-
- Extract the `description` from the content, as some Excel descriptions are lengthy and need simplification.
|
|
721
|
-
- `abstract` and `links` generally do not need modification.
|
|
722
|
-
- `aliasFunctionName` is optional; most formulas do not need to be filled (or can be set for aliases in specific countries). Currently, there is no documentation for formula aliases. Currently I have found a function translation plug-in that may provide similar functions [Excel Functions Translator](https://support.microsoft.com/en-us/office/excel-functions-translator-f262d0c0-991c-485b-89b6-32cc8d326889)
|
|
723
|
-
- `functionParameter` needs a name for each parameter. We recommend varying names based on the parameter's meaning, e.g., use `number` for a numeric parameter (if there is only one) or `number1`, `number2` for multiple numeric parameters. Use `range` for a range, `criteria` for conditions, and `sumRange` for the sum range, use `camelCase`. For specific parameter content, the English format of `name` uses the underlined format `sum_range`, other languages use the translated text, and `detail` uses all translations.
|
|
724
|
-
- Some Chinese translations in the Office function documentation are machine-translated and may be unclear. Modify as needed. For example, `单元格参考` (Cell Reference) should be translated as `单元格引用`. Numeric type parameters are uniformly translated as: `数值`.
|
|
725
|
-
- Do not end `abstract` with a period (used in the search list when users input cells), but end `description` and `detail` with a period (used in descriptions).
|
|
726
|
-
- Capitalize the first letter of English sentences.
|
|
727
|
-
- Ensure that all existing internationalization files are filled. Currently, there are only Chinese, English, and Japanese translations (languages can be switched at the bottom of the Excel introduction page).
|
|
728
|
-
|
|
729
|
-
3. Formula Descriptions
|
|
730
|
-
|
|
731
|
-
`SUMIF` belongs to the `math` category, and the description is in [packages/sheets-formula/src/services/function-list/math.ts](https://github.com/dream-num/univer/blob/dev/packages/sheets-formula/src/services/function-list/math.ts), which manages all functions in the `math` category.
|
|
732
|
-
|
|
733
|
-
Most function names already have basic description structure. It is recommended to modify them based on this foundation. If a function is not present, add it to the end.
|
|
734
|
-
|
|
735
|
-
Requirements:
|
|
736
|
-
|
|
737
|
-
- Add the formula to the `FUNCTION_LIST_MATH` array. It is recommended to keep the order consistent with the internationalization file for easy management and retrieval.
|
|
738
|
-
- Reference the previously defined `FUNCTION_NAMES_MATH` enum for the `functionName`.
|
|
739
|
-
- `aliasFunctionName` is also optional; if there are no aliases in the internationalization file, you do not need to add them here.
|
|
740
|
-
- Pay attention to the correspondence between internationalization fields and function and parameter names.
|
|
741
|
-
- Modify function parameter information, including the `example` parameter example (e.g., for a range, use `"A1:A20"`; for conditions, use `">5"`), the `require` parameter (1 for required, 0 for optional), and the `repeat` parameter (1 for allowed, 0 for not allowed). For detailed information, refer to the interface [IFunctionParam](https://github.com/dream-num/univer/blob/dev/packages/engine-formula/src/basics/function.ts).
|
|
742
|
-
|
|
743
|
-
4. Formula Algorithm
|
|
744
|
-
|
|
745
|
-
Location: [packages/engine-formula/src/functions/math/sumif/index.ts](https://github.com/dream-num/univer/blob/dev/packages/engine-formula/src/functions/math/sumif/index.ts).
|
|
746
|
-
|
|
747
|
-
Create a new formula folder under the classification folder of the current formula. The folder name is the same as the formula, named with `kebab-case`, one folder for each formula. Then create a new `index.ts` file to write the formula algorithm. The name of the formula `class` adopts `PascalCase`. The formula is considered to be one word, and the formula with `_` or `.` is considered to be two words such as
|
|
748
|
-
|
|
749
|
-
- `SUMIF` => folder `sumif`, class `Sumif`
|
|
750
|
-
- `NETWORKDAYS.INTL` => folder `networkdays-intl`, class `NetworkdaysIntl`
|
|
751
|
-
- `ARRAY_CONSTRAIN` => folder `array-constrain`, class `ArrayConstrain`
|
|
752
|
-
|
|
753
|
-
Create a `__tests__` folder at the same level to write unit tests. After writing, remember to add the formula algorithm and function name mapping in the `function-map` file in the category directory to register the formula algorithm.
|
|
754
|
-
|
|
755
|
-
Location: [packages/engine-formula/src/functions/math/function-map.ts](https://github.com/dream-num/univer/blob/dev/packages/engine-formula/src/functions/math/function-map.ts).
|
|
756
|
-
|
|
757
|
-
5. Unit Tests
|
|
758
|
-
|
|
759
|
-
Location: [packages/engine-formula/src/functions/math/sumif/\_\_tests\_\_/index.spec.ts](https://github.com/dream-num/univer/blob/dev/packages/engine-formula/src/functions/math/sumif/__tests__/index.spec.ts)
|
|
760
|
-
|
|
761
|
-
Note:
|
|
762
|
-
|
|
763
|
-
- Supplement `sheetData` according to the formula's calculation needs, construct `cellData` based on the calculated data, and determine `rowCount` and `columnCount`.
|
|
764
|
-
- Manually initialize the formula with `new Sumif(FUNCTION_NAMES_MATH.SUMIF)`.
|
|
765
|
-
- Manually build the formula parameters for each test, and execute `calculate` at the end.
|
|
766
|
-
- Single formula tests are generally used for testing the algorithm of the current formula. If testing nested formulas with multiple formulas is needed, manually nest them or go to the `/packages/engine-formula/src/functions/__tests__` directory to execute complex nested formulas.
|
|
767
|
-
|
|
768
|
-
6. Functional Tests
|
|
769
|
-
|
|
770
|
-
Start Univer in development mode, test formulas on the interface, and preconstruct data.
|
|
771
|
-
|
|
772
|
-
- In any blank cell, enter `=sumif`. Expect a search prompt list to appear.
|
|
773
|
-
- After selecting `SUMIF` or entering `=sumif(`, trigger the formula details popup and carefully check the contents.
|
|
774
|
-
- Select the data range, trigger the calculation, and check if the formula calculation result is correct.
|
|
775
|
-
|
|
776
|
-
### Considerations for Formula Implementation
|
|
777
|
-
|
|
778
|
-
- For most formula rules, please refer to the latest version of Excel. If there are any unreasonable rules, please refer to Google Sheets.
|
|
779
|
-
- The input and output parameters of any formula can be `A1`, `A1:B10`, and the cell content may also be numbers, strings, Boolean values, empty cells, error values, arrays, etc., although the formula tutorial explains In order to identify fixed data types, the program implementation needs to be compatible. When researching Excel, consider all cases, such as `=SIN(A1:B10)`, which expands to the calculated range.
|
|
780
|
-
- For example, the `XLOOKUP` function requires at least one of the rows or columns of its two inputs to be of equal size for matrix calculation.
|
|
781
|
-
- For example, the `SUMIF` function, although commonly used for summation, can expand based on the second parameter.
|
|
782
|
-

|
|
783
|
-

|
|
784
|
-
- Excel formula calculation is becoming more like numpy, for example:
|
|
785
|
-

|
|
786
|
-
- For numerical calculations in formulas, use built-in methods and try to avoid obtaining values for manual calculation. Because formula parameters can be values, arrays, or references. You can refer to existing `sum` and `minus` functions.
|
|
787
|
-
- Precision issues: The formula introduces `big.js`, and using built-in methods will call this library. However, it is nearly 100 times slower than native calculations. Therefore, for methods like `sin`, it is advisable to use native implementations.
|
|
788
|
-
- For custom calculations, use the `product` function, suitable for calculating two input parameters. Call `map` to iterate over the values for changes to a parameter's own values.
|
|
789
|
-
- Formula algorithm supports two configurations `needsExpandParams` and `needsReferenceObject`
|
|
790
|
-
- `needsExpandParams`: Whether the function needs to expand parameters, mainly handles situations where the `LOOKUP` function needs to handle vectors of different sizes
|
|
791
|
-
- `needsReferenceObject`: Whether the function needs to pass in a reference object. After setting, `BaseReferenceObject` will not be converted into `ArrayValueObject` but will be passed directly into the formula algorithm, such as the `OFFSET` function
|
|
792
|
-
- Formula calculation errors will return fixed types of errors, such as `#NAME?`, `#VALUE!`, which need to be aligned with Excel, because there are functions `ISERR`, `ISNA`, etc. that determine the error type. If the type is not specified correctly, the result will be It may be different.
|
|
793
|
-
- In the formula algorithm, even if it is a required parameter, it is necessary to intercept the case of `null` and return the error `#N/A`, because the user may not enter any parameters. This behavior will be intercepted in Excel and `#N/A` will be returned in Google Sheets. We refer to Google Sheets.
|
|
794
|
-
|
|
795
|
-
### Formula Basic Tools
|
|
796
|
-
|
|
797
|
-
1. `ValueObjectFactory` is used to automatically recognize parameter formats and create a parameter instance. Use `RangeReferenceObject` to create parameter instances for range-type data.
|
|
798
|
-
2. The array `toArrayValueObject` can be operated directly with values to get a new array.
|
|
32
|
+
- [Register formula using Facade API](https://univer.ai/guides/sheet/facade/general/#register-formula)
|
|
33
|
+
- [Custom Formula](https://univer.ai/guides/sheet/customization/formula/)
|