Using the FlexGrid getClipString causes precision to be lost in the data

Posted by: damon.carr on 26 May 2021, 2:03 am EST

    • Post Options:
    • Link

    Posted 26 May 2021, 2:03 am EST

    When we try to use the getClipString it seems to truncates the data. The underlying data in the grid goes to 8 decimal places but when pasting into Excel using getClipString we only get 2 (and I checked - the data is not there if I expand precision in Excel).

    Before we manually collected the data and exported, and had hoped to move to getClipString to reduce complexity.

    Here is the old code (which we need to enhance as it does not capture multi-row headers):

            const { gridData, columnDefinitions } = this.props;
            var headerRow = '';
            var columns = [];
    
            for (var columnIndex = 0; columnIndex < grid.columns.length; columnIndex++){
                var column = grid.columns[columnIndex];
                var actionColumns = columnDefinitions.filter(columnDef => columnDef.action).map(columnDef => columnDef.binding);
                if (!actionColumns || !actionColumns.includes(column.binding)) {
                    columns.push(column);
                    //Replace all new lines in the header with white space
                    headerRow += grid.columns[columnIndex].header.replace(/\n/g, ' ');
                    if (columnIndex !== grid.columns.length - 1) {
                        headerRow += '\t';
                    }
                }
            }
    
            //Handling for case where last column in grid is an action column
            if (headerRow.endsWith('\t')) {
                headerRow = headerRow.slice(0, -1);
            }
    
            var rows = [headerRow];
            if (gridData) {
                for (var rowIndex = 0; rowIndex < gridData.length; rowIndex++) {
                    var rowData = '';
                    var row = gridData[rowIndex];
                    for (var colIndex = 0; colIndex < columns.length; colIndex++) {
                        var value = row[columns[colIndex].binding];
                        if (value !== undefined && value !== null) {
                            rowData += value;
                        }
    
                        if (colIndex !== columns.length - 1){
                            rowData += '\t';
                        }
                    }
                    rows.push(rowData);
                }
            }
    
            Clipboard.copy(rows.join('\n'));
    

    This is the new implementation

                // Create a range including everything in the grid
                let rng = new  wjcGrid.CellRange(0, 0, grid.rows.length - 1, grid.columns.length - 1);
    
                // See https://www.grapecity.com/wijmo/api/classes/wijmo_grid.flexgrid.html#getclipstring
                // for parameters the range is passed, then false as we do not want CSV, then true as we 
                // do want headers
    
                // Get the clipboard string (tab delimited)
                let copyText = grid.getClipString(rng, false, true);     
                   
                Clipboard.copy(copyText);
    

    Is this expected behavior?

  • Posted 26 May 2021, 10:29 pm EST

    Hi,

    As the default behaviour, getClipString returns the formatted value. If you need to export the unformatted values, then you may pass the unformatted flag to the getClipString method like:

    grid.getClipString(rng, wjcGrid.ClipStringOptions.Unformatted, true);
    

    API references:

    • ClipStringOptions: https://www.grapecity.com/wijmo/api/enums/wijmo_grid.clipstringoptions.html

    • getClipString: https://www.grapecity.com/wijmo/api/classes/wijmo_grid.flexgrid.html#getclipstring

    Regards

  • Posted 27 May 2021, 12:09 am EST

    Thank you - indeed I found that exact solution shortly after posting this (as many probably do for their postings) - but so glad on the quality of this forum and much appreciate your response.

    One thing I did notice - I’m using few of the wjcGrid.ClipStringOptions as such:

    let myoptions - wjcGrid.ClipStringOptions.SkipMerged && wjcGrid.ClipStringOptions.Unformatted;
    

    When I also add CSV as such:

    let myoptions - wjcGrid.ClipStringOptions.SkipMerged && wjcGrid.ClipStringOptions.Unformatted && wjcGrid.ClipStringOptions.CSV;
    

    The SkipMerged no longer works - so probably an easy fix - for now I removed CSV and can parse cells based on tabs instead of commas.

    But thank you again!

    -Damon

  • Posted 27 May 2021, 12:12 am EST

    I just realized my bitwise operator is wrong - it should be & not && - let me try that.

  • Posted 27 May 2021, 12:25 am EST

    Ah - this is not a bitwise operation but a simple +:

                let copyOptions = wjcGrid.ClipStringOptions.Unformatted + wjcGrid.ClipStringOptions.SkipMerged;
    

    I had actually broken this and didn’t realize it until just now.

  • Posted 27 May 2021, 3:46 pm EST

    Since ClipStringOptions are just flags, you need to use the bitwise OR (|) which is basically the same as + operator. So you may use the + operator too.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels