Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More advanced custom function tutorial #1108

Merged
merged 32 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2889c31
Start writing a custom function example for GREET()
sequba Nov 23, 2022
0f4bcdf
Export FunctionArgumentType enum
sequba Nov 23, 2022
7b3c860
Move custom function tests from square-plugin-example.spec.ts to cust…
sequba Nov 23, 2022
6d0af2e
Add unit tests for custom functions implemented with runFunction
sequba Nov 23, 2022
70c1b49
WIP
sequba Nov 24, 2022
9e67e69
Update the code snippets in the custom functions guide to match the n…
sequba Dec 2, 2022
9e3dbb3
Add info about unit tests to the custom functions guide
sequba Dec 2, 2022
0f25ddf
Merge branch 'develop' into feature/issue-779
sequba Dec 6, 2022
82d0e8b
Rewrite custom function options section in custom functions guide
sequba Dec 6, 2022
fbc1ca9
Export ArraySize class
sequba Dec 10, 2022
60c0ce5
Update custom-functions guide after changes in the demo
sequba Dec 10, 2022
01144a9
Structural changes in the custom-functions guide
sequba Dec 10, 2022
f1615da
Move SimpleRangeValue from ./src/interpreter to ./src so that the typ…
sequba Dec 13, 2022
8d4fca3
Describe DOUBLE_RANGE function in the custom-functions guide
sequba Dec 13, 2022
6b5992d
Use ArraySize in the example of arraySizeMethod in the custom-functio…
sequba Dec 13, 2022
ee375f7
Add changelog entry
sequba Dec 13, 2022
0ab7465
Add the description for the arraySizeMethod option
sequba Dec 13, 2022
b9c6868
Rephrase a few sentences in the custom-functions guide
sequba Dec 14, 2022
fb5e5aa
Improve the description of returnNumberType option in the custom-func…
sequba Dec 14, 2022
befa807
Add default values to the custom function options description
sequba Dec 14, 2022
4f62223
Add possible values to the argumentType description in the custom-fuc…
sequba Dec 14, 2022
ea0f41b
Reduce content duplication on custom function name translations
sequba Dec 14, 2022
682f5d5
Minor improvements to custom-functions guide
sequba Dec 14, 2022
3fa9d33
Merge branch 'develop' into feature/issue-779
sequba Dec 14, 2022
bfbb0f0
grammarly fixes
sequba Dec 14, 2022
56fe511
Reformat table
sequba Dec 14, 2022
32556e4
Improve the description of the vectorization feature
sequba Dec 15, 2022
a6b1e57
Merge branch 'develop' into feature/issue-779
sequba Dec 20, 2022
aef856b
code review fixes
sequba Dec 20, 2022
f629630
Change translations object to be standalone instead of a static prope…
sequba Dec 20, 2022
efe66ef
Use FunctionArgumentType enum in custom functions guide
sequba Dec 20, 2022
318d094
Fix typo
sequba Dec 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Expanded the HyperFormula public API by adding the code entities that are necessary to implement a complex custom function: `ArraySize`, `ImplementedFunctions`, `FunctionMetadata`, `FunctionArgumentType`. [#779](https://github.com/handsontable/hyperformula/issues/779)

## [2.2.0] - 2022-11-17

### Added
Expand Down
33 changes: 17 additions & 16 deletions docs/guide/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ISEVEN(A2:A5*10)

Thanks to HyperFormula's built-in array features, you can:
* [Operate on arrays](#operating-on-arrays) just like on [scalars](#about-arrays)
* [Pass arrays to functions](#passing-arrays-to-scalar-functions) that accept [scalars](#about-arrays)
* [Pass arrays to functions](#passing-arrays-to-scalar-functions-vectorization) that accept [scalars](#about-arrays)
* [Broadcast](#broadcasting) smaller input arrays across larger output areas

You can also:
Expand All @@ -83,7 +83,7 @@ You can also:

You can operate on arrays just like on single values.

When the [array arithmetic mode](#enabling-the-array-arithmetic-mode) is enabled, each output array value is the result of your operation on the corresponding input array value.
When the [array arithmetic mode](#array-arithmetic-mode) is enabled, each output array value is the result of your operation on the corresponding input array value.

```
=ARRAYFORMULA(A2:A5*B2:B5)
Expand All @@ -95,22 +95,23 @@ When the [array arithmetic mode](#enabling-the-array-arithmetic-mode) is enabled
// =A5*B5
```

### Passing arrays to scalar functions
You can pass arrays to functions that would normally accept [scalars](#about-arrays) (thanks to HyperFormula's **vectorization** feature).
### Passing arrays to scalar functions (vectorization)

When the [array arithmetic mode](#enabling-the-array-arithmetic-mode) is enabled, and you pass an array to a [scalar](#about-arrays) function, that function produces an array on the output as well.
When the [array arithmetic mode](#array-arithmetic-mode) is enabled, HyperFormula automatically _vectorizes_ most functions.

As a consequence of that, you can pass arrays to functions that would normally accept [scalars](#about-arrays). The result would also be an array.

```
=ARRAYFORMULA(ISEVEN(A2:A5*10))
=ARRAYFORMULA(ISEVEN(A2:A5))

// calculates:
// =ISEVEN(A2*10)
// =ISEVEN(A3*10)
// =ISEVEN(A4*10)
// =ISEVEN(A5*10)
// =ISEVEN(A2)
// =ISEVEN(A3)
// =ISEVEN(A4)
// =ISEVEN(A5)
```

#### Broadcasting
### Broadcasting

If an input array has a dimension of `1`, it's automatically repeated ("broadcast") on that dimension to match the size of the output.

Expand All @@ -126,15 +127,15 @@ If an input array has a dimension of `1`, it's automatically repeated ("broadcas

### Filtering an array

When the [array arithmetic mode](#enabling-the-array-arithmetic-mode) is enabled, you can filter an array, based on boolean arrays, using the `FILTER` function:
When the [array arithmetic mode](#array-arithmetic-mode) is enabled, you can filter an array, based on boolean arrays, using the `FILTER` function:

| Syntax | Example |
|:-----------------------------------------------------|:------------------------------------------------|
| `FILTER(your_array, BoolArray1[, BoolArray2[, ...]]` | `=ARRAYFORMULA(FILTER(A2:A5*10), {1, 0, 0, 1})` |

### Constraining an array's size

When the [array arithmetic mode](#enabling-the-array-arithmetic-mode) is enabled, you can constrain the size of the output array, using the `ARRAY_CONSTRAIN` function:
When the [array arithmetic mode](#array-arithmetic-mode) is enabled, you can constrain the size of the output array, using the `ARRAY_CONSTRAIN` function:

| Syntax | Example |
|:-------------------------------------------|:----------------------------------------------|
Expand All @@ -148,17 +149,17 @@ If your specified output array size is larger or equal to the input array size,

### With the array arithmetic mode enabled

When the [array arithmetic mode](#enabling-the-array-arithmetic-mode) is enabled, and you pass an array to a [scalar](#about-arrays) function, the following rules apply:
When the [array arithmetic mode](#array-arithmetic-mode) is enabled, and you pass an array to a [scalar](#about-arrays) function, the following rules apply:
* Array dimensions need to be consistent (e.g. every row needs to be of the same length).
* If an input array value is missing (due to a difference in dimensions), the corresponding output array value is `#N/A`.
* If a cell evaluates to an array, the array values are spilled into neighboring cells (unless the neighboring cells are already filled).<br>This behavior doesn't apply to ranges, which return the `#VALUE!` error in this case.
* If one of input array dimensions is `1` (`1`x`n` or `n`x`1`), the array is repeated, to match the output array dimensions.

### With the array arithmetic mode disabled

When the [array arithmetic mode](#enabling-the-array-arithmetic-mode) is disabled, and you pass an array to a [scalar](#about-arrays) function, the array is reduced to 1 element (usually the array's top-left value).
When the [array arithmetic mode](#array-arithmetic-mode) is disabled, and you pass an array to a [scalar](#about-arrays) function, the array is reduced to 1 element (usually the array's top-left value).

When the [array arithmetic mode](#enabling-the-array-arithmetic-mode) is disabled, and you operate on a range of width/height equal to `1`, the behavior depends on your array formula's location:
When the [array arithmetic mode](#array-arithmetic-mode) is disabled, and you operate on a range of width/height equal to `1`, the behavior depends on your array formula's location:

| Your array formula's location | Behavior |
|:--------------------------------------------------|:---------------------------------------|
Expand Down
Loading