Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: Cannot read property 'length' of undefined while exporting to Excel #1109

Closed
prnvlohkre opened this issue May 28, 2019 · 7 comments

Comments

@prnvlohkre
Copy link

While exporting the excel file i get the error saying "Uncaught TypeError: Cannot read property 'length' of undefined".
The scenario is : i have 6 different table which contains different data.When i am going to export it to excel sheet, it should create different 6 sheets in the Excel workbook.
When i have 1 empty table having no data, it creates an excel workbook with 1 empty sheet/tab but when i have more than 1 empty table, it fails to create an excel sheet and throws an error.

here is my query for the same.

var opts = [{sheetid:"Operations",header:true},{sheetid:"Operation Hooks",header:true},{sheetid:"Tool Groups",header:true},{sheetid:"BuyOffs",header:true},{sheetid:"Data Collection Groups",header:true},{sheetid:"DC Params",header:true}];
alasql('SELECT INTO XLSX(" ' + saveFile + '.xlsx", ?) FROM ?', [opts,[data,data1,data2,data3,data4,data5]]);

where data,data1,data2,data3,data4,data5 are the the data model(array) having the table data.

can someone please look into this. it needs to resolve in an urgency.

@prnvlohkre
Copy link
Author

can anyone help me out in this

@mathiasrw
Copy link
Member

Any chance you can create an example on jsfiddle that generates this error?

@artikhot97
Copy link

artikhot97 commented Aug 25, 2020

Getting Same error :face_with_rolling_eyes:
Any Solution

@mathiasrw
Copy link
Member

@artikhot97 Any chance you can create an example on jsfiddle that generates this error?

@gkkachi
Copy link
Contributor

gkkachi commented Feb 2, 2024

Hi @prnvlohkre @mathiasrw

Apply the patch file below and it works fine for me.

diff --git a/node_modules/alasql/dist/alasql.fs.js b/node_modules/alasql/dist/alasql.fs.js
index 0129398..b7fb1e4 100644
--- a/node_modules/alasql/dist/alasql.fs.js
+++ b/node_modules/alasql/dist/alasql.fs.js
@@ -16739,10 +16739,14 @@ alasql.into.XLSX = function (filename, opts, data, columns, cb) {
 		var dataLength = Object.keys(data).length;
 
 		// Generate columns if they are not defined
-		if ((!columns || columns.length == 0) && dataLength > 0) {
-			columns = Object.keys(data[0]).map(function (columnid) {
-				return {columnid: columnid};
-			});
+		if ((!columns || columns.length == 0)) {
+			if (dataLength > 0) {
+				columns = Object.keys(data[0]).map(function (columnid) {
+					return {columnid: columnid};
+				});
+			} else {
+				columns = [];
+			}
 		}
 
 		var cells = {};

@mathiasrw
Copy link
Member

Amazing. Thank you! Would you like to edit the file and make a pull request so your name is tracked as a contributer, or are you ok with me just applying the patch?

@gkkachi
Copy link
Contributor

gkkachi commented Feb 2, 2024

@mathiasrw
I have made a pull request #1872 , which contains the patch above and a test to reproduce the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants