jSpreadsheet plugin summary :




Source code of example


// Property plugin in JExcel
plugins: [
    { name:'summary', plugin:jexcel_summary, options:{
            columns: [
                { title: "0 - 19", width: 80 },
                { title: "20 - 29", width: 80 },
                { title: "30 - 39", width: 80 },
                { title: "40 - 49", width: 80 },
                { title: "50 - 59", width: 80 },
                { title: "+ 60", width: 80 },
            ],
            widthRowIndex: 200,
            formulas: {
                "Number of persons" : "=COUNT_AGE(TABLEPARENT(), COLUMN())",
                "Average of salaries" : "=AVG_SUMMARY_SALARIES_BY_AGE(TABLEPARENT(), COLUMN())",
            },
    }},
],

// Load library NumberJS in Head : <script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>

// functions
var COUNT_AGE = function(instance, column) {
    switch(column) {
        case 1 :
            var ageStart = 0, ageEnd = 19;
            break;
        case 2 :
            var ageStart = 20, ageEnd = 29;
            break;
        case 3 :
            var ageStart = 30, ageEnd = 39;
            break;
        case 4 :
            var ageStart = 40, ageEnd = 49;
            break;
        case 5 :
            var ageStart = 50, ageEnd = 59;
            break;
        case 6 :
            var ageStart = 60, ageEnd = 1000;
            break;
    }
    var data = instance.getColumnData(3);
    var count = 0;
    for(var ite_data in data) {
        if(data[ite_data] >= ageStart && data[ite_data] <= ageEnd) {
            count++;
        }
    }
    return count;
}

var AVG_SUMMARY_SALARIES_BY_AGE = function(instance, column) {
    switch(column) {
        case 1 :
            var ageStart = 0, ageEnd = 19;
            break;
        case 2 :
            var ageStart = 20, ageEnd = 29;
            break;
        case 3 :
            var ageStart = 30, ageEnd = 39;
            break;
        case 4 :
            var ageStart = 40, ageEnd = 49;
            break;
        case 5 :
            var ageStart = 50, ageEnd = 59;
            break;
        case 6 :
            var ageStart = 60, ageEnd = 1000;
            break;
    }
    var data = instance.getColumnData(3);
    var dataSalary = instance.getColumnData(5);
    var salary = [];
    for(var ite_data in data) {
        if(data[ite_data] >= ageStart && data[ite_data] <= ageEnd) {
            salary.push(dataSalary[ite_data]);
        }
    }
    return numeral(AVGSALARY(salary)).format("$#,##0");
}

function AVGSALARY(data) {
    var values = [];
    for(var ite_data in data) {
        var value = data[ite_data];
        value = value.replace(/[^0-9\.]/gm, "");
        values.push(Number(value));
    }
    return AVERAGE(values);
}

This plugin is a premium plugin available on Repo of JSpreadsheet plugins

Download full version Documentation

you can try this plugin in limited version in your jspreadsheet in localhost :


<script src="https://demo.gbonnaire.fr/dist/jexcel.summary.js"></script>
<script src="https://cdn.jsdelivr.net/gh/GBonnaire/jspreadsheet-plugins-and-editors@latest/plugins/src/rowHeaderRename/index.js"></script>