Script Execution

ECMAScript-like script execution is supported by ReoGrid control.
ReoGrid uses ReoScript, an ECMAScript-like script language engine to evaluate formula and run script.

Interaction between two modules

18.png
Learn more about ReoScript at reoscript.codeplex.com.

API to run script

Variable 'grid' is the global object that always point to the instance of control. e.g.:
grid.RunScript("alert('hello world');");
or
grid.Script = "alert('hello world');";
grid.RunScript();
Property Script will be saved and loaded automatically during xml serialization.

Set cell data by script

// get cell at pos(0,0) and set data of cell to 'hello world'
grid.getCell(0,0).data = 'hello world';
34.png

Bind event

selectionChanged event

26.png
grid.selectionChanged = function() {
  console.log('selection changed: ' + grid.selection.range);
};

cellDataChanged event

27.png
grid.cellDataChanged = function(cell) {
  console.log('cell at ' + cell.pos + ' changed data: ' + cell.data);
};

Available Event in Script

Event Timing Arguments
onmousedown when user presses down mouse buttons on any cells Position object
onmouseup when user release mouse buttons on any cells Position object
onkeydown when user presses keys on control KeyEventArg object
onkeyup when user release keys on control KeyEventArg object
oncelledit before starting to edit cell Cell object
ondatachange after cell's data has been changed Cell object
onselectionchange after selection range has been changed None
onnextfocus when focus cell will move to next cell after user press enter key None
onload after control has been initialized None
unload before control will be disposed None
oncopy before control does copy None
onpaste before control does paste None
oncut before control does cut None

All event names changed to lowercase since v0.8.5

Cancel operations by returning false

Event handled in script returns false to notify control cancelling current operations.
grid.cellBeforeEdit = function(cell) {
    if(cell.row == 2 && cell.col == 3) {
        return false;
    }
};

Customize Script Event

It is possible to customize an event in script, then raise it from C#. For example the event bound as below:
grid.myevent = function() { ... };            // event bind in script 
To raise this script's event:
grid.RaiseScriptEvent("myevent");             // event raise in C#
The RaiseScriptEvent method returns an object value, that is the value returned from script side:
grid.myevent = function() { return false; };
In C# side:
bool rs = ScriptRunningMachine.GetBoolValue(grid.RaiseScriptEvent("myevent"));
if (!rs) {
  ... 
}

Next: Customize Function

Last edited Feb 7, 2014 at 7:54 AM by unvell, version 18