From df396b671b4397079966bb23b585d54d4304776a Mon Sep 17 00:00:00 2001 From: Konosuke Kachi <18605580+gkkachi@users.noreply.github.com> Date: Sat, 3 Feb 2024 22:40:40 +0900 Subject: [PATCH] Fix #1109 (#1872) --- src/833xlsx.js | 12 ++++++++---- test/test1109.js | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 test/test1109.js diff --git a/src/833xlsx.js b/src/833xlsx.js index cf32933a26..71ce49c505 100755 --- a/src/833xlsx.js +++ b/src/833xlsx.js @@ -90,10 +90,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 = {}; diff --git a/test/test1109.js b/test/test1109.js new file mode 100644 index 0000000000..e19ad775e1 --- /dev/null +++ b/test/test1109.js @@ -0,0 +1,26 @@ +if (typeof exports === 'object') { + var assert = require('assert'); + var alasql = require('..'); +} + +describe('Test 1109 - Export empty tables to excel sheets', function () { + const test = '1109'; + + before(function () { + alasql('create database test' + test); + alasql('use test' + test); + }); + + after(function () { + alasql('drop database test' + test); + }); + + it('A) Export empty tables to excel sheets', function () { + var res = []; + var opts = [{sheetid: 'a'}, {sheetid: 'b'}]; + res.push( + alasql('SELECT INTO XLSX("' + __dirname + '/restest1109.xlsx",?) FROM ?', [opts, [[], []]]) + ); + assert.deepEqual(res, [1]); + }); +});