[]
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);
バインディングパスでは、ドット表記を使用してネストされたフィールドを参照できます。たとえば、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 番目のレコードを使用します。

アクティブレコードのインデックスを変更すると、同じセルをデータマネージャーのテーブル内の別のレコードにバインドできます。
var source = activeSheet.getDataSource();
source.activeRecordIndex(1);
シートバインディングは、データソースをワークシートにバインドします。データソースがオブジェクトの配列の場合、各オブジェクトは行として表示され、オブジェクトのフィールドは列として表示されます。
次の例では、ワークシートをオブジェクトの配列にバインドします。
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);
データマネージャーのテーブルをワークシートに直接バインドできます。このバインディングモードはコラボレーションに対応しています。
次の例では、データマネージャーのテーブルを作成してアクティブシートにバインドします。
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);
次の例では、ワークシートを 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);
テーブルを作成してデータソースにバインドできます。
次の例では、データソースを手動で作成してアクティブシートにバインドします。
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);
};
テーブルでは 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");
この例では、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);
});
};
バインドされたシートに非バインド列を追加できます。非バインド列はシートに保存され、元のデータソースには追加されません。
次の例では、非バインド列を追加し、数式を使用して小計と合計の値を計算します。
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デザイナコンポーネントでのバインディングの設定については、SpreadJSデザイナでのシートバインディングを参照してください。