[]
        
(Showing Draft Content)

データの連結

SpreadJS は、ワークシートを JavaScript オブジェクトの配列およびデータマネージャーのテーブルにバインドできます。SpreadJS はセルレベルのバインディングにも対応しており、個々のセルをオブジェクトのプロパティまたはデータマネージャーのテーブルのレコードにバインドできます。

SpreadJS は双方向データバインディングに対応しています。

セルバインディング

セルバインディングは、オブジェクトのプロパティをワークシート内の特定のセルにバインドします。セルバインディングを使用するには、GC.Spread.Sheets.Bindings.CellBindingSource でソースデータをラップし、対象セルにバインディングパスを設定してから、setDataSource でラップしたソースをシートにバインドします。

セルをオブジェクトにバインドする

次の例では、セルをオブジェクトのプロパティにバインドします。

var person = {
    name: "Wang feng",
    age: 25,
    address: {
        postcode: "710075"
    }
};
var source = new GC.Spread.Sheets.Bindings.CellBindingSource(person);
activeSheet.setBindingPath(0, 0, "name");
activeSheet.setBindingPath(1, 1, "age");
activeSheet.setBindingPath(3, 3, "address.postcode");
activeSheet.setDataSource(source);

SpreadJS worksheet displays object properties in individually bound cells, including a nested postcode resolved through the address.postcode binding path.

バインディングパスでは、ドット表記を使用してネストされたフィールドを参照できます。たとえば、address.postcode は、ネストされた address オブジェクト内の postcode フィールドを参照します。

セルをデータマネージャーのテーブルにバインドする

セルバインディングでは、データマネージャーのテーブルをバインディングソースとして使用することもできます。この場合、シートはテーブル内の 1 つのレコードにバインドされ、各セルのバインディングパスはそのレコードを基準に解決されます。

次の例では、データマネージャーのテーブルの最初のレコードのフィールドにセルをバインドします。

var dataManager = spread.dataManager();
var activeSheet = spread.getActiveSheet();
var ordersTable = dataManager.addTable("Orders", {
    data: [
        {
            orderId: 10248,
            customerId: "VINET",
            freight: 32.38
        },
        {
            orderId: 10249,
            customerId: "TOMSP",
            freight: 11.61
        }
    ]
});

var source = new GC.Spread.Sheets.Bindings.CellBindingSource(ordersTable, 0);

activeSheet.setBindingPath(0, 0, "orderId");
activeSheet.setBindingPath(1, 0, "customerId");
activeSheet.setBindingPath(2, 0, "freight");

activeSheet.setDataSource(source);

CellBindingSource の 2 番目の引数は、アクティブレコードのインデックスを指定します。たとえば、インデックス 0 は最初のレコードを使用し、インデックス 1 は 2 番目のレコードを使用します。

SpreadJS worksheet cells display order ID, customer ID, and freight fields bound to the first Data Manager table record.

アクティブレコードのインデックスを変更すると、同じセルをデータマネージャーのテーブル内の別のレコードにバインドできます。

var source = activeSheet.getDataSource();
source.activeRecordIndex(1);

SpreadJS worksheet updates the same bound cells with values from the second Data Manager record after changing activeRecordIndex.

シートバインディング

シートバインディングは、データソースをワークシートにバインドします。データソースがオブジェクトの配列の場合、各オブジェクトは行として表示され、オブジェクトのフィールドは列として表示されます。

シートを配列オブジェクトにバインドする

次の例では、ワークシートをオブジェクトの配列にバインドします。

var datasource = [
    { name: "Alice", age: 27, birthday: "1985/08/31", position: "PM" },
    { name: "Aimee", age: 28, birthday: "1984/07/31", position: "TL" },
    { name: "Charles", age: 29, birthday: "1983/03/31", position: "QC" },
    { name: "Fred", age: 30, birthday: "1982/02/20", position: "DL" },
    { name: "Angelia", age: 31, birthday: "1981/05/30", position: "QC" },
    { name: "Peter", age: 32, birthday: "1980/11/08", position: "QC" }
];

activeSheet.autoGenerateColumns = true;
activeSheet.setDataSource(datasource);

SpreadJS worksheet displays an array of person objects as generated rows and columns after calling the setDataSource method.

シートをデータマネージャーのテーブルにバインドする

データマネージャーのテーブルをワークシートに直接バインドできます。このバインディングモードはコラボレーションに対応しています。

次の例では、データマネージャーのテーブルを作成してアクティブシートにバインドします。

var dataManager = spread.dataManager();
var activeSheet = spread.getActiveSheet();
var personsTable = dataManager.addTable("Persons", {
    data: [
        { name: "Alice", age: 27, birthday: "1985/08/31", position: "PM" },
        { name: "Aimee", age: 28, birthday: "1984/07/31", position: "TL" },
        { name: "Charles", age: 29, birthday: "1983/03/31", position: "QC" },
        { name: "Fred", age: 30, birthday: "1982/02/20", position: "DL" },
        { name: "Angelia", age: 31, birthday: "1981/05/30", position: "QC" },
        { name: "Peter", age: 32, birthday: "1980/11/08", position: "QC" }
    ]
});

activeSheet.autoGenerateColumns = true;
activeSheet.setDataSource(personsTable);

バインドされたシートにレコードを追加すると、その変更がデータマネージャーのテーブルに同期されます。

シートに列を追加しても、データマネージャーのテーブルは変更されません。追加した列とそのセルの内容はシートに保存されます。

並べ替えとフィルタリングの機能があるため、現在サポートされているのはローカルデータソースのみで、リモートデータソースはサポートされていません。高度なバインディングが必要な場合は、テーブルシートの使用をお勧めします。

列バインディング

列バインディングを使用すると、データソース内の特定のフィールドをワークシートの指定した列に表示するように設定できます。

bindColumn を使用して列をフィールドにバインドし、ヘッダテキスト、幅、フォーマッター、サイズ変更の可否、表示状態などの列設定を定義します。

次の例では、bindColumn メソッドを使用して特定の列をバインドします。

var datasource = [
    { name: "Alice", age: 27, birthday: "1985/08/31", position: "PM" },
    { name: "Aimee", age: 28, birthday: "1984/07/31", position: "TL" },
    { name: "Charles", age: 29, birthday: "1983/03/31", position: "QC" },
    { name: "Fred", age: 30, birthday: "1982/02/20", position: "DL" },
    { name: "Angelia", age: 31, birthday: "1981/05/30", position: "QC" },
    { name: "Peter", age: 32, birthday: "1980/11/08", position: "QC" }
];

var nameColInfo = { name: "name", displayName: "Name", size: 70 };
var ageColInfo = { name: "age", displayName: "Age", size: 40, resizable: false };
var birthdayColInfo = { name: "birthday", displayName: "Birthday", formatter: "d/M/yy", size: 120 };
var positionColInfo = { name: "position", displayName: "Position", size: 50, visible: false };

activeSheet.autoGenerateColumns = true;
activeSheet.setDataSource(datasource);

activeSheet.bindColumn(0, nameColInfo);
activeSheet.bindColumn(1, ageColInfo);
activeSheet.bindColumn(2, birthdayColInfo);
activeSheet.bindColumn(3, positionColInfo);

SpreadJS worksheet shows explicitly bound Name, Age, and formatted Birthday columns, while the Position column remains hidden through bindColumn settings.

次の例では、ワークシートを JSON 配列オブジェクトにバインドします。

var jsonArray = '{"phoneNumbers": [{"type": "home","number": "212 555-1234"},{"type": "fax","number": "646 555-4567"}]}';
var arr = JSON.parse(jsonArray);

activeSheet.setDataSource(arr.phoneNumbers);

SpreadJS worksheet displays phone type and number columns populated from a parsed JSON array through the setDataSource method.

テーブルバインディング

テーブルを作成してデータソースにバインドできます。

テーブルをデータソースにバインドする

次の例では、データソースを手動で作成してアクティブシートにバインドします。

window.onload = function () {
    var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 3 });
    var activeSheet = spread.getActiveSheet();

    var sampleTable = [
        { "ID": 10, "Text": "Text-10", "Check": true },
        { "ID": 20, "Text": "Text-20", "Check": false },
        { "ID": 30, "Text": "Text-30", "Check": false },
        { "ID": 40, "Text": "Text-40", "Check": true },
        { "ID": 50, "Text": "Text-50", "Check": true }
    ];

    activeSheet.setDataSource(sampleTable);
};

SpreadJS worksheet displays bound ID, Text, and Check fields from a JavaScript object array, including Boolean cell values.

バインディングパスでテーブルをデータマネージャーのフィールドにバインドする

テーブルでは bindingPath を使用して、データマネージャーのテーブルのアクティブレコードにあるフィールドにバインドできます。アクティブレコード内のフィールドに、テーブルに表示するデータが含まれている場合に使用できます。

次の例では、シートをデータマネージャーのテーブルのレコードにバインドし、そのレコード内の project フィールドにテーブルをバインドします。

var persons = [
    {
        name: "Wang feng",
        age: 25,
        address: {
            postcode: "710075"
        },
        project: [
            { name: "project1", budget: 10000 },
            { name: "project2", budget: 20000 }
        ]
    },
    {
        name: "Li lei",
        age: 26,
        address: {
            postcode: "710076"
        },
        project: [
            { name: "project3", budget: 30000 },
            { name: "project4", budget: 40000 }
        ]
    }
];

var dataManager = spread.dataManager();

var personsTable = dataManager.addTable("Persons", {
    data: persons
});

var source = new GC.Spread.Sheets.Bindings.CellBindingSource(personsTable, 0);

activeSheet.setDataSource(source);

var table = activeSheet.tables.add("table1", 1, 1, 4, 4);
table.bindingPath("project");

SpreadJS worksheet table displays project names and budgets from the active Data Manager record through the bindingPath("project") property.

この例では、CellBindingSource によって Persons テーブルの最初のレコードがアクティブレコードに設定されます。次に、テーブルは bindingPath("project") を使用して、そのレコードの project フィールドにバインドされます。

バインドされたデータの更新

ワークシートをデータソースにバインドした後も、ワークシートに行や列を追加できます。

新しい行を追加してデータソースを更新する

次の例では、バインドされたシートの最終行の後に行を追加します。新しい行はデータソースにも追加されます。

window.onload = function () {
    var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 3 });
    var activeSheet = spread.getActiveSheet();

    var sampleTable = [
        { "ID": 10, "Text": "Text-10", "Check": true },
        { "ID": 20, "Text": "Text-20", "Check": false },
        { "ID": 30, "Text": "Text-30", "Check": false },
        { "ID": 40, "Text": "Text-40", "Check": true },
        { "ID": 50, "Text": "Text-50", "Check": true }
    ];

    activeSheet.setDataSource(sampleTable);

    document.getElementById("button1").addEventListener("click", function () {
        console.log("The number of all rows in the datasource before addition: " + sampleTable.length);

        var row = activeSheet.getRowCount();

        activeSheet.addRows(row, 1);

        activeSheet.setValue(row, 0, 100);
        activeSheet.setValue(row, 1, "Text-New");
        activeSheet.setValue(row, 2, true);
        activeSheet.getCell(row, -1).backColor("pink");

        console.log("The number of all rows in the datasource after addition: " + sampleTable.length);
        console.log("The value of the Text field in the last row of the datasource: " + sampleTable[sampleTable.length - 1].Text);
    });
};

SpreadJS worksheet shows a newly added pink row whose ID, text, and Boolean values synchronize with the bound data source.

非バインド列を追加する

バインドされたシートに非バインド列を追加できます。非バインド列はシートに保存され、元のデータソースには追加されません。

次の例では、非バインド列を追加し、数式を使用して小計と合計の値を計算します。

window.addEventListener("DOMContentLoaded", function () {
    var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 3 });
    var activeSheet = spread.getActiveSheet();
    var rowCount, colCount;

    var sampleTable = [
        { "Num1": 10, "Num2": 100, "Num3": 1000 },
        { "Num1": 20, "Num2": 200, "Num3": 2000 },
        { "Num1": 30, "Num2": 300, "Num3": 3000 },
        { "Num1": 40, "Num2": 400, "Num3": 4000 },
        { "Num1": 50, "Num2": 500, "Num3": 5000 }
    ];

    activeSheet.setDataSource(sampleTable);

    activeSheet.addColumns(2, 1);
    activeSheet.addColumns(activeSheet.getColumnCount(), 1);

    rowCount = activeSheet.getRowCount();
    spread.options.referenceStyle = GC.Spread.Sheets.ReferenceStyle.R1C1;

    activeSheet.setValue(0, 2, "Subtotal", GC.Spread.Sheets.SheetArea.colHeader);

    for (var i = 0; i < rowCount; i++) {
        activeSheet.setFormula(i, 2, "=SUBTOTAL(9, RC[-2]:RC[-1])");
    }

    activeSheet.getRange(-1, 2, -1, 1).backColor("LightCyan");
    activeSheet.getCell(0, 2, GC.Spread.Sheets.SheetArea.colHeader).backColor("LightCyan");
    activeSheet.setColumnWidth(2, 60);

    colCount = activeSheet.getColumnCount();

    activeSheet.setValue(0, colCount - 1, "Total", GC.Spread.Sheets.SheetArea.colHeader);

    for (var i = 0; i < rowCount; i++) {
        activeSheet.setFormula(i, colCount - 1, "SUBTOTAL(9, RC[-4]:RC[-1])");
    }

    activeSheet.getRange(-1, colCount - 1, -1, 1).backColor("LightPink");
    activeSheet.getCell(0, colCount - 1, GC.Spread.Sheets.SheetArea.colHeader).backColor("LightPink");
    activeSheet.setColumnWidth(colCount - 1, 60);
});

SpreadJS bound worksheet includes unbound Subtotal and Total columns that calculate row values with SUBTOTAL formulas using R1C1 references.

関連トピック

SpreadJSデザイナコンポーネントでのバインディングの設定については、SpreadJSデザイナでのシートバインディングを参照してください。