Author Topic: Import from Excel  (Read 4414 times)

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Import from Excel
« on: April 15, 2014, 02:35:24 am »
Hi there!

Eventually, a stupid question: is there any tool to import data from Excel?
Something like letting the user select certain columns from Excel into a paramquery table.

Thanks a lot for helping!


paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Import from Excel
« Reply #1 on: April 15, 2014, 05:04:51 pm »
your question makes sense. paste data (from Excel or another grid) API would be available in future versions.

nuno.nogueira

  • Pro Economy
  • Jr. Member
  • *
  • Posts: 95
    • View Profile
Re: Import from Excel
« Reply #2 on: April 15, 2014, 06:18:34 pm »
Whoa, that's really cool to know!

Leo F

  • Pro Enterprise
  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Import from Excel
« Reply #3 on: April 22, 2014, 04:36:47 am »
I don't know if it's appropriate to share code here, but I've created something for my use which does just that. The code below allows a user to paste a bunch of selected Excel cells into a ParamQuery grid. This is specific to my purposes, but it should be enough to get you going. This seems to work properly in IE8+, FF, Chrome. The relevant part starts at "$inp.on('paste', function (event) {", I provided the rest for context.


            obj.editor = {
                type: function (ui) {
                    var $cell = ui.$cell,
                    rowData = ui.rowData,
                    dataIndx = ui.dataIndx,
                    cls = ui.cls,
                    dc = $.trim(rowData[dataIndx]);
                    $cell.css('padding', '0');

                    var $inp = $('<input class="pq-grid-editor-default ' + cls + '" type="text" style="outline: none; width: 100%; height: 100%;" tabindx="0" value="' + dc + '"/>').appendTo($cell);
                    $inp.blur(function (event) {
                        grid.Container.pqGrid('saveEditCell');
                    });

                    $inp.on('paste', function (event) {
                        var data;
                        if (event.originalEvent && event.originalEvent.clipboardData && event.originalEvent.clipboardData.getData) {
                            data = event.originalEvent.clipboardData.getData('Text');
                        } else if (window.clipboardData && window.clipboardData.getData) {
                            data = window.clipboardData.getData('Text');
                        } else if (event.clipboardData && event.clipboardData.getData) {
                            data = event.clipboardData.getData('text/plain');
                        }

                        if (typeof data != "undefined") {
                            if (data.indexOf(String.fromCharCode(9)) == -1 && data.indexOf('\n') == -1)
                                return;

                            var cell = grid.Container.pqGrid("selection", { type: 'cell', method: 'getSelection' })[0];
                            var colIdx = grid.Container.pqGrid("getColIndx", { dataIndx: cell.dataIndx });
                            var rowIndx = cell.rowIndx;
                            var colModel = grid.Container.pqGrid("getColModel");
                            var pRows = data.split('\n');
                            for (i = 0; i < pRows.length; i++) {
                                pRows = pRows.split(String.fromCharCode(9));
                            }

                            for (i = 0; i < pRows.length; i++) {
                                if (grid.Container.pqGrid("hasClass", { rowIndx: rowIndx + i, cls: 'gridRowDivider' }) || grid.Container.pqGrid("hasClass", { rowIndx: rowIndx + i, cls: 'gridRowCalculated' }))
                                    continue;
                                var row = grid.Container.pqGrid("getRowData", { rowIndx: rowIndx + i });
                                if (typeof row == "undefined")
                                    break;
                                for (j = 0; j < pRows.length; j++) {
                                    var value = parseInt($.trim(pRows[j]));
                                    if (isNaN(value))
                                        value = null;
                                    var column = colModel[colIdx + j];
                                    if (typeof column == "undefined")
                                        break;
                                    if (pRows[j].length > 0) {
                                        SetValue(grid, column.dataIndx, row.rowId, value, true);
                                        if (j == 0 && i == 0)
                                            $(this).text(value);
                                    }
                                }
                            }
                            event.stopPropagation();
                            event.preventDefault();
                        }
                    });

                    $inp[0].select();
                },
                getData: function (ui) {
                    var $cell = ui.$cell;
                    var value = $(ui.$cell.children()[0]).val();
                    return value;
                }
            };
« Last Edit: April 22, 2014, 04:52:28 am by Leo F »