Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
135
Custom Functions
posted

Is it possible to create custom functions that can be used as formulas?  For instance "=MyFunction(A1:A10)"?

Parents Reply
  • 1100
    Verified Answer
    Offline posted in reply to Brian Ploe

    Hello Brian,

    I am writing to add some information on your scenario that was provided by the development team.

    As a temporary workaround, while the feature is implemented, you can use this example: http://jsfiddle.net/fcj2dupv/

    The example demonstrates hot to write a helper function in that page that creates a new derived calc function. Then the usage is 2 parts. One is to define the function.

    e.g.

    createCustomFunction("SumTwo", 2, 2, function(numberStack, argumentCount) {
                    var second = numberStack.pop();
      var first = numberStack.pop();
      
      return new $.ig.excel.ExcelCalcValue(first.toDouble() + second.toDouble());
    });
    

    The other is to register an instance of it with the workbook:

    function setWorkbook() {
         if ($("#spreadsheet").igSpreadsheet !== undefined && workbook != null) {
           workbook.registerUserDefinedFunction(new $.ig.CustomFunctions.SumEx());
           workbook.registerUserDefinedFunction(new $.ig.CustomFunctions.SumTwo());
     
            //load specific workbook
           $("#spreadsheet").igSpreadsheet("option", "workbook", workbook);
        }
    }
    

    Note that CustomFunctions are prefixed to avoid any naming collisions with any of the predefined classes. Note also that writing a custom function can be rather involved since the arguments can be raw values, arrays or references and it’s really up to the function to properly handle all these things based on how it needs to function.You would likely need to look at the various members of the ExcelCalcValue class to see how to identify the value of the arg and how to extract information from it if it’s anything more involved than what I showed in the 2 examples in that fiddle.

    I hope this would meet your scenario requirements. However, once the feature is implemented in the future version, it is strongly recommended to switch to using it instead of this workaround.

    If you face any other issues - please let us know!

    Best regards,
    Alexander 

Children