4D View Pro:カスタム関数の強化

時には、いくつのセルが影響を受けるかわからないまま、カラムや行のコンテンツに基づいた特殊な計算が必要になることがあります。

4D 20 R5では、スプレッドシートのフォーミュラ内のカスタムフォーミュラに”A1:C10 “のような複数のセル範囲を渡したり、”OBJECT(“Firstname”,B2, “Lastname”,C2) “のようなオブジェクトを渡したりして、すべてのセルのコンテンツを格納したコレクションやオブジェクトを4Dメソッドに取得できるようになりました。

HDI 4DVP カスタム関数内のコレクションとオブジェクト

カスタム関数は、4D View Proの強力な機能で、その機能を拡張し、特定のニーズに合わせたカスタムの計算や操作を実行することができます。カスタム関数の引数としてコレクションを渡すことができるようになり、これによってユーザー指定のセル範囲からの値を受け取ることができるようになります。

コレクション管理

コレクションを使用したカスタム関数の例として、ここではゼロ以外の値の平均を計算します。このプロセスには2つの基本ステップがあります:

  • 4D View Proが計算を実行するための関数を作成します。
  • カスタム関数のオプションを指定するオブジェクトを作成します。

この例では、CustomFunctionCreator というクラスを作成します。

それではこの計算をどのように行うかを見てみましょう!

カスタム関数の作成

まず、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 でカスタム関数を有効にする

_averageNonZeroValues() 関数を作成したので、次に4D View Pro でフォーミュラとして使用できるようにします。このためには、VP SET CUSTOM FUNCTIONS コマンドを使用します。4D View Pro 内でカスタム関数を作成するために必要な詳細を格納したオブジェクトが必要です。このプロセスを包括的に理解するためには、4D View Pro: Use 4D formulas in your spreadsheet 参照してください。

VP SET CUSTOM FUNCTIONS コマンドに必要なすべてのパラメータを収容するオブジェクトを構築するために、averageNonZeroValues() 関数を実装します:

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

オブジェクトの管理

SpreadJSはオブジェクトを生成するために設計されたOBJECT フォーミュラを提供しており、これを使用することでユーザーは複数のセルの内容を単一のオブジェクトに統合することができます。オブジェクトを使用したカスタム関数の説明では、オブジェクトのどの部分が変更されたかを定義するフォーミュラを使用して説明します。上記のコレクションと同様に、このプロセスには2つの基本ステップがあります:

  • 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 でカスタム関数を有効にする

averageNonZeroValues() の例と同様に、modifiedProperties() 関数を実装して、VP SET CUSTOM FUNCTIONS コマンドに必要なすべてのパラメータを収容するオブジェクトを構築する必要があります:

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”:

blank

結論として、4D View Proでカスタム関数を使用することで、スプレッドシート機能を新たな高みへと引き上げ、オーダーメイドの計算や操作が可能になります。より詳細な情報についてはドキュメントをご覧ください。

Fabrice Mainguené
- Product Owner -Fabrice Mainguenéは、2016年11月に4D Programチームに参加しました。プロダクトオーナーとして、彼はユーザーストーリーを書き、それを機能仕様に変換する役割を担っています。CNAMでコンピュータサイエンスの学士号を取得した後、FabriceはWindev開発者として小さなソフトウェア出版社に入社しました。その後、彼は産業および貿易分野のさまざまな企業で、Windev および Web 開発者として、また新機能の技術アドバイザーとして働きました。