4D Blog

Home Product 4D View Pro: Enhancement of Custom Functions

4D View Pro: Enhancement of Custom Functions

May 8, 2024

Product

Sometimes, you may require specialized calculations on the contents of a column or row without knowing how many cells will be affected.

With 4D 20 R5, you can now pass multiple cells ranges to a custom formula within your spreadsheet formula, such as “A1:C10”, or an object such as “OBJECT(“Firstname”,B2,”Lastname”,C2)”,and retrieve a collection or an object containing all cell content in your 4D method.

HDI 4DVP Collection and Object in Custom Functions

Custom functions are a powerful feature of 4D View Pro that allows you to extend its functionality and perform custom calculations or operations tailored to your specific needs. You can now accept collections as parameters to custom functions containing values from user-specified cell ranges.

Collection management

An illustration of a custom function using collection entails calculating the average of non-zero values. This process comprises 2 fundamental steps:

  • Establish a function intended for execution by 4D View Pro to conduct the calculation.
  • Formulate an object to specify the custom function options.

For this example, we’ll create a class named CustomFunctionCreator.

Let’s see how to accomplish this calculation!

 

Custom function creation

First, we’ll create the function called by 4D View Pro. This function, called _averageNonZeroValues(), takes one collection parameter and is defined like this:

Function _averageNonZeroValues($values : Collection) : Real
    // Average of the non zero and non null values in the range
	
    // Sum of all the collection values
    var $total:=$values.flat().sum()
    // Calculates the number of cells containing a value other than 0 or NUll
    var $NonZeroValueNumber : Integer:=$values.flat().count()-$values.flat().countValues(0)
	
    // Returns the calculation result to be displayed in the cell
    return $NonZeroValueNumber>0 ? $total/$NonZeroValueNumber : 0

Enable the Custom function in 4D View Pro

Now that we’ve established the _averageNonZeroValues() function, we aim to enable its usage as a formula in 4D View Pro. To accomplish this, we employ the VP SET CUSTOM FUNCTIONS command, which needs an object encompassing essential details required for creating the custom function within 4D View Pro. For a comprehensive understanding of this process, you can refer to the 4D View Pro: Use 4D formulas in your spreadsheet blog post, which provides a comprehensive overview.

We implement the averageNonZeroValues() function to construct the object housing all parameters necessary for the VP SET CUSTOM FUNCTIONS command:

Function averageNonZeroValues()->$customFunction : Object
    var $this : Object
	
    $customFunction:={}
    $this:=This // Capture This for the formula
	
    // formula that will be called when the custom function is used in the spreadsheet
    $customFunction.formula:=Formula($this._averageNonZeroValues($1))
    // If a parameter is of collection type, the declaration of the custom function parameters is mandatory
    $customFunction.parameters:=[{name: "Values"; type: Is collection}]
    // Summary of the custom function using in the autocomplete popup 
    $customFunction.summary:="Returns the average of non zero values"
    // Expected number of parameters
    $customFunction.minParams:=1
    $customFunction.maxParams:=1

Object management

SpreadJS offers an OBJECT formula designed to generate objects, enabling users to consolidate content from multiple cells into a unified object. An illustration of a custom function using an object consists of defining which part of an object has been modified. As with the collections above, there are 2 basic steps to this process:

  • Establish a function intended for execution by 4D View Pro to conduct the calculation.
  • Formulate an object to specify the custom function options.

For this example, we’ll complete the CustomFunctionCreator class.

Custom function creation

First, we’ll create the function called by 4D View Pro. This function, called _modifiedProperties(), takes one object parameter and is defined like this:

Function _modifiedProperties($object: Object) : Text
    // Comparison between the properties in the Form.people object and the properties in the spreadsheet
    var $myObject : Object:=$object.value

     // Search the object attributes modified between the people data and the data in the spreadsheet
     For each ($property; $myObject)
          // comparison between the object returned by the spreadsheet and the Form.people object
          If ($myObject[$property]#Form.people[$property])
               return "Data modified."
          End if 
     End for each 
     return ""

Enable the Custom function in 4D View Pro

As for the averageNonZeroValues() example, we need to implement the modifiedProperties() function to construct the object housing all parameters necessary for the VP SET CUSTOM FUNCTIONS command:

Function modifiedProperties()->$customFunction : Object
    var $this : Object
	
    $customFunction:={}
    $this:=This // Capture This for the formula
	
    // formula that will be called when the custom function is used in the spreadsheet
    $customFunction.formula:=Formula($this._modifiedProperties($1))
    // If a parameter is of collection type, the declaration of the custom function parameters is mandatory
    $customFunction.parameters:=[{name: "Values"; type: Is object}]
    // Summary of the custom function using in the autocomplete popup 
    $customFunction.summary:="Returns a message when the object is modified by the user"
    // Expected number of parameters
    $customFunction.minParams:=1
    $customFunction.maxParams:=1

Custom function declaration

The last step is to call VP SET CUSTOM FUNCTIONS on the “on Load” event of the form using the averageNonZeroValues() and modifiedProperties() functions:

Case of 
		
  : (FORM Event.code=On Load)
    var $customFunctions:={}

    // Declaration of authorized custom functions in the 4D View Pro area
    var $creator:=cs.CustomFunctionsCreator.new()
    $customFunctions.MY_AVERAGENONZEROVALUES:=$creator.averageNonZeroValues()
    $customFunctions.MY_MODIFICATIONS:=$creator.modifiedProperties()

    VP SET CUSTOM FUNCTIONS("ViewProArea"; $customFunctions)
End case 

Now, you have 2 new functions usable in your 4D View Pro area:

  • “MY_AVERAGENONZEROVALUES:

  • “MY_MODIFICATIONS”:

blank

In conclusion, using custom functions in 4D View Pro elevates spreadsheet functionality to new heights, enabling tailored calculations and operations. Please see our documentation for further information.

Discuss

Tags 20 R5, 21, 4D View Pro, Spreadsheet

Latest related posts

  • December 3, 2025

    Give AI to a 30 years old 4D application

  • November 28, 2025

    ORDA – Handle an event-driven logic during database operations

  • November 27, 2025

    ORDA – Permissions – Restrict / allow web access to the resources in one click

Fabrice Mainguené
Fabrice Mainguené
• Product Owner •Fabrice Mainguené joined 4D Program team in November, 2016. As a Product Owner, he is in charge of writing the user stories then translating it to functional specifications. His role is also to make sure that the feature implementation delivered is meeting the customer need.After obtaining a Bachelor degree in Computer Science at CNAM, Fabrice joined a small software publishing company as a Windev developer. Then he worked for different companies in industry and trade areas as a Windev and web developer as well as technical advisor on new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • AI
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy