SpreadJS 选择数据统计

发布时间:2016/06/27 00:06 发布者:dexteryao

返回博客中心

Excel中用户选择区域时可以在下方看到选择数据的统计,本示例演示如何在SpreadJS中实现选择区域改变时实时数据统计。

485F.tmp

实现这个功能的核心就是在SelectionChanging事件中,获取选中区域的数据,进行统计,下面直接看下代码。

activeSheet.bind(GcSpread.Sheets.Events.SelectionChanging, function (e, info) {    
    var sheet = info.sheet;
    var selections = info.newSelections; //得到新选择区域
    var totalCount = 0, numCount = 0, total = 0; //统计数据初始值
    if(selections && selections.length){
        for(var i = 0; i < selections.length; i++){
            var selection = selections[i];
            for(var row = selection.row; row < selection.row + selection.rowCount; row++){
                for(var col = selection.col; col < selection.col + selection.colCount; col++){
                    var value = sheet.getValue(row, col);
                    if(value !== null && value !== undefined && value !== ""){
                        if(isFinite(value)){
                            numCount++; //统计Number数量
                            total += value; //求和
                        }
                        totalCount ++; //统计非空数据数量
                    }
                }
            }
        }
        if(totalCount>0){
            var text = "计数:"+totalCount +  (numCount > 0 ? "  平均值:" + total/numCount +"  求和:" + total : "");
            $("#sumInfo").html(text);
        }
        else{
            $("#sumInfo").html("");
        }
    }
    else{
        $("#sumInfo").html("")
    }
});

 

在SelectionChanging事件中我们可以从info中获取到sheet,sheetName,oldSelections以及newSelections四个参数。newSelections就是当前最新的选择区域,我们循环获取该区域内的数据,就可以到我们想要的数据统计了。

改示例的在现在演示可以在RunJS看到,地址:http://runjs.cn/detail/uxlmtnos

 

更多资源

SpreadJS中文学习指南:http://demo.grapecity.com.cn/SpreadJS/TutorialSample/#/samples

SpreadJS在线英文产品文档:http://sphelp.grapecity.com/webhelp/SpreadJSWeb/webframe.html#welcome.html

如果您对SpreadJS产品感兴趣,可以到官方网站下载试用:/developer/spreadjs

如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn

 
 

关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网