Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
255
Recursive export of multiple grids to Excel
posted

I'm attempting to export multiple grids to an Excel workbook, with each grid on a different tab. I went through the posting 4 years ago with a similar title and implemented (with some modifications) the code. It worked for the first two grids, but it did not hit the exportEnding callback on the 2nd grid, so it could not recurse into the other grids on the page.  I'll paste the two functions in question here so you'll have a better idea.

        function ExportGridsToExcel() {

            var columnsToSkip = ["RowType", "RowFormat", "RowDateFormat"];

            var gridIndex = 0;

            var lengthColumns = gridColumnsArray[gridIndex]; // This will get us to the array of columns for this grid

            // Loop through each of the column names and skip any columns that start with the name Oversold
            $.each(lengthColumns,
                function(index, item) {
                    if (item.key.startsWith("Oversold") && item.key !== "Oversold") {
                        columnsToSkip.push(item.key);
                    }
                });

            var nameToUse = descriptionArray[gridIndex].replace(/ +/g, ' '); // Get the description for this grid and remove extra spaces
            var workSheetName = nameToUse.substring(0, 31);

            $.ig.GridExcelExporter.exportGrid($("#mainGrid_" + gridIndex),
                {
                    fileName: "ForecastExport",
                    worksheetName: workSheetName,
                    columnsToSkip: columnsToSkip
                },
                {
                    exportEnding: function (sender, args) {
                        if (numberOfLayouts > 1) {
                            ExportNextGrid(gridIndex, args.workbook);
                            return false;
                        } else {
                            return true;
                        }
                    }
                },
                {
                    error: function (error) {
                        console.log("Error on export of worksheet " + workSheetName + "Error: "  + error);
                    }
                });
        }

        function ExportNextGrid(gridIndex, workbook) {

            gridIndex++; // Get to the next grid

            var columnsToSkip = ["RowType", "RowFormat", "RowDateFormat"];

            var lengthColumns = gridColumnsArray[gridIndex]; // This will get us to the array of columns for this grid

            // Loop through each of the column names and skip any columns that start with the name Oversold
            $.each(lengthColumns,
                function(index, item) {
                    if (item.key.startsWith("Oversold") && item.key !== "Oversold") {
                        columnsToSkip.push(item.key);
                    }
                });

            var nameToUse = descriptionArray[gridIndex].replace(/ +/g, ' '); // Get the description for this grid and remove extra spaces

            var workSheetName = nameToUse.substring(0, 31);

            var headerArr = [];

            $.ig.GridExcelExporter.exportGrid($("#mainGrid_" + gridIndex),
                {
                    fileName: "ForecastExport",
                    worksheetName: workSheetName,
                    columnsToSkip: columnsToSkip
                },
                {
                    headerCellExporting: function (sender, args) {
                        headerArr.push(args.headerText); // Save the names of the headers to assign to the Excel header row
                        if (args.columnIndex === 0) {
                            sender._workbook = workbook;
                            sender._workbook.worksheets().add(sender._worksheet.name());
                            sender._worksheet = sender._workbook.worksheets(gridIndex);
                        }
                    }
                },
                {
                    exportEnding: function (sender, args) {
                        var row = sender._worksheet.rows(0); // this is the header row

                        // Set the column names for the header row we saved earlier
                        for (var ind = 0; ind < headerArr.length; ind++) {
                            row.setCellValue(ind, headerArr[ind]);
                        }

                        if (gridIndex < numberOfLayouts - 1) {
                            ExportNextGrid(gridIndex, args.workbook);
                            return false;
                        } else {
                            return true;
                        }

                    }
                },
                {
                    error: function (error) {
                        console.log("Error on export of worksheet " + workSheetName + "Error: " + error);
                    }
                });
        }
 

As you can see I've set it up to recurse into ExportNextGrid, but I've set a breakpoint in the callback for exportEnding and it never gets hit. I can save the spreadsheet and it contains the two grids in different worksheets, but the header row for the 2nd worksheet doesn't have the correct names and it will not recurse to get the other grids. 

What am I doing wrong?

Parents Reply Children
No Data