Coleção e objeto HDI 4DVP em funções personalizadas
Funções personalizadas são uma caraterística poderosa de 4D View Pro que permite estender sua funcionalidade e realizar cálculos personalizados ou operações adaptadas às suas necessidades específicas. Agora pode aceitar colecções como parâmetros para funções personalizadas contendo valores de intervalos de células especificados pelo utilizador.
Gestão de colecções
Criação da função personalizada
Primeiro, vamos criar a função chamada pelo 4D View Pro. Essa função, chamada _averageNonZeroValues(), recebe um parâmetro de coleção e é definida assim:
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
Ativar a função Custom no 4D View Pro
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
Gestão de objectos
- Estabelecer uma função destinada a ser executada pelo 4D View Pro para realizar o cálculo.
- Formular um objeto para especificar as opções da função personalizada.
Para este exemplo, vamos completar a classe CustomFunctionCreator .
Criação da função personalizada
Primeiro, vamos criar a função chamada pelo 4D View Pro. Essa função, chamada _modifiedProperties(), recebe um parâmetro de objeto e é definida assim:
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 ""
Habilitar a função Custom no 4D View Pro
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
Declaração da função personalizada
O último passo é chamar VP SET CUSTOM FUNCTIONS no evento“on Load” do formulário usando as funções averageNonZeroValues() e modifiedProperties():
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
Agora, tem 2 novas funções utilizáveis na sua área 4D View Pro:
- “MY_AVERAGENONZEROVALUES”:
- “MY_MODIFICATIONS”:
Em conclusão, usar funções personalizadas em 4D View Pro eleva a funcionalidade da folha de cálculo a novas alturas, permitindo cálculos e operações personalizadas. Por favor veja nossa documentação para mais informação.