[{"id":"7720ca38-fc98-4fd8-a3a5-3c0e730c3c72","tags":[{"product":null,"links":null,"id":"2f522515-8c88-4017-855d-905bc4b0ea3c","name":"Upd","color":"Blue","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"247ded67-b050-4436-be40-fe7b0eeabef7","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"9a8c377f-1f1e-476e-b487-c2339da9bed3","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"5e8e6db2-89b9-4184-bbdc-301ee676869e","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"e04b598e-b0d8-4054-bdac-5143d90b3d51","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"edb028d0-a8cc-42b2-acda-42a5515729f0","tags":[{"product":null,"links":null,"id":"2f522515-8c88-4017-855d-905bc4b0ea3c","name":"Upd","color":"Blue","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"2adde444-20a6-43d7-8896-2b445877327a","tags":[{"product":null,"links":null,"id":"2f522515-8c88-4017-855d-905bc4b0ea3c","name":"Upd","color":"Blue","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"6ab8ced2-6c7a-41b6-b861-ba066bd40f40","tags":[{"product":null,"links":null,"id":"2f522515-8c88-4017-855d-905bc4b0ea3c","name":"Upd","color":"Blue","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"0c34aedd-80a3-470b-a0f6-c327a4346be7","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"717a5892-fa23-499b-a61d-cff228943aaf","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"bd9be971-69fc-4793-ad6d-234be7a93048","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"a1e8d38f-43d8-4b3e-b747-3fbf7656fcd9","tags":[{"product":null,"links":null,"id":"2f522515-8c88-4017-855d-905bc4b0ea3c","name":"Upd","color":"Blue","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"d6995fdf-76ee-421f-9e7a-3fb324d14bbb","tags":[{"product":null,"links":null,"id":"2f522515-8c88-4017-855d-905bc4b0ea3c","name":"Upd","color":"Blue","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"64fa3155-4543-4acd-83b7-f9860fd46673","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"698413bd-0b16-4287-baec-644d98a411d1","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]},{"id":"7e4393a0-21ef-438b-981b-325b64db5baa","tags":[{"product":null,"links":null,"id":"6c1a6899-1b88-404c-9a73-4a0c6c9090a3","name":"New","color":"Red","productId":"66a69675-7df9-45b2-867a-4ab2613535fa"}]}]
        
(Showing Draft Content)

カスタム数式の作成

組み込み関数ではない関数を定期的に使用する場合や、いくつかの組み込み関数を1つの関数として統合したい場合は、独自のカスタム関数を定義できます。カスタム関数は、組み込み関数とまったく同様に呼び出すことができます。カスタム関数は、指定された値または配列のいずれかを返すことができます。

カスタム関数で単一の値を返す

次のサンプルコードは、単一の値を返すカスタム関数を作成する方法を示します。

var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{sheetCount:3});
var activeSheet = spread.getActiveSheet();
// カスタム関数をセルに追加して、結果を確認します。
// Type =myfunc(1)
function myfunc() {}
myfunc.prototype = new GC.Spread.CalcEngine.Functions.Function("myfunc", 0, 0, {name: "myfunc",description: "This is my first function"});
myfunc.prototype.evaluate = function (args) {
    return 100;}
spread.addCustomFunction(new myfunc());

カスタム関数で配列を返す

次のサンプルコードは、配列を返すカスタム関数を作成する方法を示します。

<script>
        $(document).ready(function () {
            // スプレッドシートを初期化します。
            var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 1 });
            // アクティブシートを取得します。
            var sheet = spread.getSheet(0);
            // 動的な配列を許可します。
            spread.options.allowDynamicArray = true;
            // 階乗関数を作成します。
            function FactorialFunction() {
                this.name = "FACTORIAL";
                this.maxArgs = 1;
                this.minArgs = 1;
            }
            FactorialFunction.prototype = new GC.Spread.CalcEngine.Functions.Function();
            // 数式の結果を評価します。
            FactorialFunction.prototype.evaluate = function (arg) {
                var t = 1;
                var result = [];
                if (arguments.length === 1 && !isNaN(parseInt(arg))) {
                    for (var i = 1; i <= arg; i++) {
                        t = i * t;
                        result.push(t);
                    }
                    // 関数の結果としてCALCArrayオブジェクトを返します。
                    return new GC.Spread.CalcEngine.CalcArray([result]);
                }
                return "#VALUE!";
            };
            // カスタム関数に説明を追加します。
            FactorialFunction.prototype.description = function () {
                return {
                    name: "FACTORIAL",
                    description:
                        "This is function which calculates the factorial from 1 and show in different cells of a row",
                };
            };
            var factorial = new FactorialFunction();
            sheet.addCustomFunction(factorial);
            // セル(0,0)を原点とする3つの列をマージします。
            sheet.addSpan(0, 0, 1, 7, GC.Spread.Sheets.SheetArea.viewport);
            sheet.setValue(0, 0, "This custom factorial function calculates the factorial from 1 to 7 ");
            for (var i = 1; i < 8; i++) {
                sheet.setText(2, i - 1, i + "!");
            }
            // カスタム数式を設定します。
            sheet.setFormula(3, 0, "=Factorial(7)");
            var style = new GC.Spread.Sheets.Style();
            style.font = "bold 12px Arial";
            style.foreColor = "white";
            style.backColor = "#5B9BD5";
            style.hAlign = GC.Spread.Sheets.HorizontalAlign.center;
            style.vAlign = GC.Spread.Sheets.VerticalAlign.center;
            sheet.setStyle(0, 0, style, GC.Spread.Sheets.SheetArea.viewport);
        });
    </script>