[{"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"}]}]
AsyncFunctionおよびAsyncEvaluateContextクラスを使用して、関数のサーバー側評価を行えます。関数の評価方法を指定するには、AsyncFunctionEvaluateModeクラスを使用します。
次のサンプルコードは、AsyncFunctionクラスを拡張するカスタム関数を定義し、その関数でタイムアウトを使用してサーバー側評価をシミュレーションします。
var asum = function () {}
//AsyncFunctionを拡張する"ASUM"クラスを定義します。
asum.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction("ASUM", 1, 255);
//デフォルト値として、"Loading..."を設定します。
asum.prototype.defaultValue = function () { return "Loading..."; };
//evaluateAsync関数をオーバーライドします。
asum.prototype.evaluateAsync = function (context) {
var args = arguments;
//サーバー側評価をシミュレートするには、タイムアウトを使用します。または、ajaxポストを使用します。
setTimeout(function () {
//評価ロジック
var result = 0;
for (var i = 0; i < args.length; i++) {
result += args[i];
}
result *= 2;
//非同期関数の評価結果をCalcEngineに設定します。
context.setAsyncResult(result);
}, 2000);
}
//または、Ajaxポストを使用します。
/$.ajax({
//url: '@Url.Action("ASUM", "Home")',
//type: "POST",
//data: JSON.stringify(args),
//contentType: "application/json,charset=UTF-8",
//success: function (data) {
//context.SetAsyncResult(data.result);
//}
//});
//ASUM関数をSpreadに追加して、数式を設定します。
activeSheet.addCustomFunction(new asum());
activeSheet.setValue(0, 0, 5);
activeSheet.setValue(0, 1, 15);
activeSheet.setFormula(1, 1, "ASUM(A1,B1)");
次のサンプルコードは、Refresh数式を使用して式を再計算します。
<!DOCTYPE html>
<html>
<head>
<title>SpreadJS</title>
<link href="./css/gc.spread.sheets.12.0.0.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./scripts/gc.spread.sheets.all.12.0.0.min.js"></script>
<!--jQuery References-->
<script src="http://code.jquery.com/jquery-2.0.2.js" type="text/javascript"></script> <script type="text/javascript">
window.onload = function(){
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{sheetCount:3});
var sheet = spread.getActiveSheet(); var GetNumberFromServer = function () {
}
GetNumberFromServer.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction("GETNUMBERFROMSERVER", 2, 2);
GetNumberFromServer.prototype.evaluate = function (context, arg1, arg2) {
var self = this;
$.get('http://xa-tools-shdev/asyncfunction/api/values/' + (arg1 || 1), function (value) {
context.setAsyncResult(value);
});
} GC.Spread.CalcEngine.Functions.defineGlobalCustomFunction("GETNUMBERFROMSERVER", new GetNumberFromServer());
sheet.setValue(0, 0, 'ChangeValue');
sheet.setValue(0, 1, 'Formula');
sheet.setValue(0, 2, 'Result');
sheet.setValue(0, 3, 'Comments');
sheet.setValue(1, 3, 'On A2 changed');
sheet.setValue(2, 3, 'On A2 changed');
sheet.setValue(3, 3, 'Evaluate once');
sheet.setValue(4, 3, 'Every 2 seconds');
sheet.setValue(1, 0, 1);
sheet.setValue(1, 1, '=GetNumberFromServer(A2)');
sheet.setValue(2, 1, '=Refresh(GetNumberFromServer(A2), 0)');
sheet.setValue(3, 1, '=Refresh(GetNumberFromServer(A2), 1)');
sheet.setValue(4, 1, '=Refresh(GetNumberFromServer(A2), 2, 2000)');
sheet.setFormula(1, 2, '=GetNumberFromServer(A2)');
sheet.setFormula(2, 2, '=Refresh(GetNumberFromServer(A2), 0)');
sheet.setFormula(3, 2, '=Refresh(GetNumberFromServer(A2), 1)');
sheet.setFormula(4, 2, '=Refresh(GetNumberFromServer(A2), 2, 2000)');
sheet.setColumnWidth(0, 100);
sheet.setColumnWidth(1, 300);
sheet.setColumnWidth(2, 200);
sheet.setColumnWidth(3, 200);
sheet.setValue(7, 1, "=Refresh(now(), 2, 1000)");
sheet.setValue(7, 3, "Every 1 seconds");
sheet.setFormula(7, 2, "=Refresh(now(), 2, 1000)"); var GetTimeFromServer = function () {
}
GetTimeFromServer.prototype = new GC.Spread.CalcEngine.Functions.AsyncFunction("GETTIMEFROMSERVER", 2, 2);
GetTimeFromServer.prototype.evaluate = function (context) {
$.get('http://xa-tools-shdev/asyncfunction/api/time/', function (value) {
context.setAsyncResult(value);
});
}
GetTimeFromServer.prototype.evaluateMode = function () {
return 2;
};
GetTimeFromServer.prototype.interval = function () {
return 1000;
}; GC.Spread.CalcEngine.Functions.defineGlobalCustomFunction("GETTIMEFROMSERVER", new GetTimeFromServer());
sheet.setValue(10, 1, "=GetTimeFromServer()");
sheet.setValue(10, 3, "Every 1 seconds");
sheet.setFormula(10, 2, "=GetTimeFromServer()");
sheet.getCell(10, 2).hAlign(GC.Spread.Sheets.HorizontalAlign.right);
}
</script>
</head>
<body>
<div id="ss" style="width:100%;height:500px;border:1px solid gray"></div>
</body>
</html>