Search code examples
handsontable

handsontable's subproject rulejs: how to get the calculated value of a formula cell?


rulejs subproject of handsontable is great. But I can't find anything that emulates the "paste special" of spreadsheets. So, if I write

afterOnCellMouseDown: function(r, c) {
    var x = hotdata[c['row']][c['col']];
    document.getElementById("valorCelCorrente").innerHTML = x;}

I get the formula, not the calculated value that the cell is displaying. Same happens with a manual copy & paste. A fiddle with example: https://jsfiddle.net/zota/j2a04w83/3/ Any clue? Thanks a lot Julio


Solution

  • I had the same issue, but solved it with the help of this question.

    Firstly you can use hot.plugin.helper.cellValue('CELL REF') to get the actual value, but when using getData() you're looking at the whole set. To solve this problem I basically iterated through the array replacing any formula cells with the value (I only needed columns up until 'K'):

    var aCols = ['A','B','C','D','E','F','G','H','I','J','K'];                   
    var data = hot.getData();
    
    for (i = 0; i < data.length; i++){
        for(j = 0; j < data[i].length; j++){
            if(data[i][j].toString().indexOf('=') > -1){
                data[i][j] = hot.plugin.helper.cellValue (aCols[j] + (i+1));
            }
        }
    }