HDI 4DVP カスタム関数内のコレクションとオブジェクト
カスタム関数は、4D View Proの強力な機能で、その機能を拡張し、特定のニーズに合わせたカスタムの計算や操作を実行することができます。カスタム関数の引数としてコレクションを渡すことができるようになり、これによってユーザー指定のセル範囲からの値を受け取ることができるようになります。
コレクション管理
カスタム関数の作成
まず、4D View Pro によって呼び出される関数を作成します。_averageNonZeroValues() と呼ばれるこの関数は、コレクションを1つ引数として受け取り、中身は以下のように書かれています:
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
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
オブジェクトの管理
- 4D View Pro が計算を実行するための関数を作成します。
- カスタム関数のオプションを指定するオブジェクトを作成します。
この例では、CustomFunctionCreator クラスを完成させます。
カスタム関数の作成
まず、4D View Pro から呼び出される関数を作成します。_modifiedProperties() と呼ばれるこの関数は、1つのオブジェクトを引数として受け取り、以下のように定義します:
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 ""
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
カスタム関数の宣言
最後のステップはフォームの”On Load”イベントにおいて、 averageNonZeroValues() とmodifiedProperties() 関数を使って、VP SET CUSTOM 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
これで、4D View Proエリアで使える2つの新しい関数ができました:
- “my_averagenonzerovalues”:
- “my_modifications”:
結論として、4D View Proでカスタム関数を使用することで、スプレッドシート機能を新たな高みへと引き上げ、オーダーメイドの計算や操作が可能になります。より詳細な情報についてはドキュメントをご覧ください。