Script Execution

ECMAScript-like script execution is support by ReoGrid full functionality set release. ReoGrid uses ReoScript, an ECMAScript-like script language engine to execute scripts.

Interaction between two modules


Learn more about ReoScript at github.com/unvell/reoscript.

API to run script

Call method RunScript of workbook to run script, for example:

grid.RunScript("alert('hello world');");

or pass the script to Script property of workbook, then call RunScript method to execute it:

grid.Script = "alert('hello world');";
grid.RunScript();

Property Script will be saved and loaded automatically during xml serialization.

Workbook global object in script

There is a predefined global object called ‘workbook’ in global context of script, it represents the root object, the workbook instance in the spreadsheet , it could be referenced inside any function scopes of script.

Note: from 0.8.8.2, the ‘grid’ global variable for control is changed to ‘workbook’. For back-compatible an alias variable ‘grid’ still exist but it will be removed from next version.

ReoScript:

// get current worksheet
var sheet = workbook.currentWorksheet;

Set cell data in script

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

It’s possible to test the script in ReoGridEditor, open the Editor, select menu: Tools->Script Editor to open script editor, input the script and press ‘Run’ button.

Bind event

onselectionchange event

grid.onselectionchange = function() {
  console.log('selection changed: ' + grid.selection);
};

ondatachange event

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.oncelledit = function(cell) {
    if(cell.row == 2 && cell.col == 3) {
        return false;
    }
};

Call script’s function from C# side

It is possible to customize an event in script, then raise it from C#. For example the event bound as below:

workbook.myevent = function() { ... };            // event bind in script 

To raise this script’s event from .NET:

sheet.RaiseScriptEvent("myevent");             // event raise in C#

The RaiseScriptEvent method returns an object value, that is the value returned from script side:

workbook.myevent = function() { return false; };

In C# side:

bool rs = ScriptRunningMachine.GetBoolValue(worksheet.RaiseScriptEvent("myevent"));
if (!rs) {
  ... 
}

Next: Settings