Docs: expanding the Custom Functions topic - #758
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #758 +/- ##
========================================
Coverage 92.35% 92.35%
========================================
Files 166 166
Lines 40311 40330 +19
Branches 5569 5569
========================================
+ Hits 37229 37248 +19
Misses 3039 3039
Partials 43 43
|
Co-authored-by: Wojciech Czerniak <wojciech.czerniak@gmail.com>
Co-authored-by: Wojciech Czerniak <wojciech.czerniak@gmail.com>
| public hyper({ args }) { | ||
| if (!args.length) { | ||
| // create a `CellError` instance with an `ErrorType` of `DIV_BY_ZERO` | ||
| // with your custom error message (optional) | ||
| return new CellError(ErrorType.DIV_BY_ZERO, 'Sorry, cannot divide by zero!'); | ||
| } | ||
|
|
||
| else { | ||
| return 'Hyperformula'.length; | ||
| } | ||
| }; |
There was a problem hiding this comment.
Wrong formatting and I think we're missing plugin class here
| public hyper({ args }) { | |
| if (!args.length) { | |
| // create a `CellError` instance with an `ErrorType` of `DIV_BY_ZERO` | |
| // with your custom error message (optional) | |
| return new CellError(ErrorType.DIV_BY_ZERO, 'Sorry, cannot divide by zero!'); | |
| } | |
| else { | |
| return 'Hyperformula'.length; | |
| } | |
| }; | |
| export class CountHF extends FunctionPlugin { | |
| public hyper({ args }) { | |
| if (!args.length) { | |
| // create a `CellError` instance with an `ErrorType` of `DIV_BY_ZERO` | |
| // with your custom error message (optional) | |
| return new CellError(ErrorType.DIV_BY_ZERO, 'Sorry, cannot divide by zero!'); | |
| } | |
| else { | |
| return 'Hyperformula'.length; | |
| } | |
| }; | |
| } |
All imports are already in place
There was a problem hiding this comment.
Or make it more obvious that we're focusing on the body of our method because complete functions will be added below. Then we should remove the imports, fix indentation, add a comment that this is part of the CountHF class etc
There was a problem hiding this comment.
@wojciechczerniak - sure. I think I'd rather display the whole example (except optional parameters) in each step, WDYT? I also checked the indendation in all the code samples: 5b0f149
There was a problem hiding this comment.
🤔 With translations and aliases it's a little bit harder to find what has changed between steps. Also the "complete" examples is a copy of last step without any changes and this sections makes little sense. But I don't have a better idea right now
|
|
||
| ## A complete example of the class definition | ||
|
|
||
| To sum up, here is a complete example of a custom `CountHF` class: |
There was a problem hiding this comment.
The complete examples is now shorter than each step 😄 We should update the example below
There was a problem hiding this comment.
Ale at the bottom of the page we have CodeSandbox with working demo. We should make sure it's up do date with this tutorial
There was a problem hiding this comment.
@wojciechczerniak - done: 5b0f149
@jansiegel - can we update the demo to include:
- Optional parameters
- Argument validation options
- Aliases
- Translations
- The
runFunction()wrapper - Returning errors
Thanks for your help!
|
With translations added as static key on the plugin class the "using plugin" section have to change: import Hyperformula, { plPL } from 'hyperformula';
import { CountHF } from './file_with_your_custom_function';
- import { myTranslations } from '/myTranslationFile';
// register the language
HyperFormula.registerLanguage('plPL', plPL);
// register your custom plugin and the translation
- HyperFormula.registerFunctionPlugin(CountHF, myTranslations);
+ HyperFormula.registerFunctionPlugin(CountHF, CountHF.translations);
// build HF instance where you can use the function directly
const hfInstance = HyperFormula.buildFromArray([['=HAJPER()']]);
// read the value of cell A1
const A1Value = hfInstance.getCellValue({ sheet: 0, col: 0, row: 0 });
// open the browser's console to see the results
console.log(A1Value); |
|
@wojciechczerniak - translation updated: a1f532b, d786b8a |
#557
In Custom Functions: