jspreadsheet / ce

Jspreadsheet is a lightweight vanilla javascript plugin to create amazing web-based interactive tables and spreadsheets compatible with other spreadsheet software.
https://bossanova.uk/jspreadsheet/v4
MIT License
6.66k stars 818 forks source link

Get both value and formula #1664

Closed shihlun1208 closed 7 months ago

shihlun1208 commented 7 months ago

I have tried to set copyCompatibility:true, to get the value executed by formula. But what I probably need is formula as well.

I need to get data and send it to my python backend, and store all the table data. But if I send with formula, I need to convert the formula myself in the backend, if I send with value, I cannot render with formula data in frontend next time. So I'm thinking that maybe I need both formula data and value data. Is it possible to get both types of data?

shihlun1208 commented 7 months ago

I think I found the way to get the formula data, if there is another easy way to do this, please let me know, thanks a lot!

let myTable = jspreadsheet(document.getElementById('spreadsheet'), {
    data: data2,
    copyCompatibility:true,
    search:true,
});

To get formula data: Call getFormulaData()

function getFormulaData(highlighted) {
    // Control vars
    var dataset = [];
    var px = 0;
    var py = 0;

    // Data type
    // var dataType = dataOnly == true || obj.options.copyCompatibility == false ? true : false;
    var dataType = true;

    // Column and row length
    var x = myTable.options.columns.length
    var y = myTable.options.data.length

    // Go through the columns to get the data
    for (var j = 0; j < y; j++) {
        px = 0;
        for (var i = 0; i < x; i++) {
            // Cell selected or fullset
            if (! highlighted || myTable.records[j][i].classList.contains('highlight')) {
                // Get value
                if (! dataset[py]) {
                    dataset[py] = [];
                }
                if (! dataType) {
                    dataset[py][px] = myTable.records[j][i].innerHTML;
                } else {
                    dataset[py][px] = myTable.options.data[j][i];
                }
                px++;
            }
        }
        if (px > 0) {
            py++;
        }
   }

   return dataset;
}

To get value Data: Just call myTable.getData()