Managing Sheet with APIs

SheetAPI.addCalculates



SheetAPI.addCalculates method is used to add customized calculate operation. It allows you define your calculation. These calculations will be passed to the sheet calculate system, so user can use these calculations in the cell. Online demo.

SHEET_API.addCalculates( calculates )

Parameters A list of calculate function need to be passed to the sheet calculate system.

Example code for a simple calculate function


SHEET_API = Ext.create('EnterpriseSheet.api.SheetAPI', {
    openFileByOnlyLoadDataFlag: true
});

SHEET_API.addCalculates({
     'getVersion': {
         fn: function(){
             return SCONST['version'];
         },
         hint: ['getVersion', 'getVersion()', 'Syntax: getVersion()<br><br>Returns the current version of this EnterpriseSheet App','info']
     }
});
 
After the above method is implemented, a new pre-defined formula "getVersion()" is inserted into the system. And user can call it in the cell to get the result. See the following image for detail.

sheet

Example code for a custom calculate function


SHEET_API = Ext.create('EnterpriseSheet.api.SheetAPI', {
    openFileByOnlyLoadDataFlag: true
});

SHEET_API.addCalculates({
    'customerCal1': {
        /*
         * define the function of this calculate, it's just for demo purpose, this function will checking all the calling 
         * parameters if it's a string or the value of the cell it's referenced is a string, then we will get the first 
         * letter of every those string to compose a new string.
         */
        fn: function(){
            /*
             * for every calculate function, the scope is an object contains:
             {
                me: me, //the calculate class instance itself
                store: store, //the store instance of the current sheet app
                sheet: sheetId, //the sheetId of the current cell contains this calculate
                row: rowIndex, //the row index of the current cell contains this calculate
                col: colIndex, //the column index of the current cell contains this calculate
                timestamp: timestamp //a timestamp created when this calculate is called
             }
             */
            var me = this.me;
            /*
             * get calculate instance and use it's checkCoordValid function to check whether the calling params is valid, 
             * if not then throw an exception
             * this function is only valid when the it returns true
             */
            var result = me.checkCoordValid(arguments);
            if(true !== result) throw {code: 'CAL_INCORRECT_COORD',span: result};
            var arr = [];
            /*
             * the each function is using to visit every item in the calling params, if it's a range of cells the function 
             * will split to single cells to let the callback visit
             * this function takes 6 params
                sheetId: the sheetId of the current cell contains this calculate
                rowIndex: the row index of the current cell contains this calculate
                colIndex: the column index of the current cell contains this calculate
                items: the items to iterate, the item element can be a single value or an coord object or an array
             * callback: the callback function will be invoked for every item and every cell referenced in the coord item
             * scope: the scope of the callback function
             */
            me.each(this.sheet, this.row, this.col, arguments, function(obj, itemType, index, insideIndex, item){
                var data;
                if('span' == itemType){
                    var sheet = obj[0], row = obj[1], col = obj[2];
                    var cell = me.getCellData(sheet, row, col, this);
                    data = cell.data;
                }else{
                    data = obj;
                }
                if(Ext.isString(data) && 0 < data.length){
                    var num = Number(data[0]);
                    if(!Ext.isNumber(num)){
                        arr.push(data[0].toUpperCase());
                    }
                }
            }, this);
            if(0 === arr.length){
                /*
                 * throw the NEW_EXCEPTION_1 if there is no string
                 */
                throw {code: 'NEW_EXCEPTION_1'};
            }

            return arr.join('');
        },
        hint: ['customerCal1', 'customerCal1(str1, str2, ...)', 'Syntax: customerCal1()<br><br>Compose a new string by take the first letter of the passed items','info']
    }
});
 
After the above method is implemented, a new pre-defined formula "customerCal1()" is inserted into the system. And you can call it in the cell to get result. See the following image for detail.

sheet

Click add Calculate exceptions method to see detail information about SheetAPI.addCalculateExceptions.

 

 


Copyright © FeyaSoft Inc. All rights reserved.