diff --git a/main.js b/main.js index c34e2c8e..fc42f026 100644 --- a/main.js +++ b/main.js @@ -289,6 +289,25 @@ const enableMultiSelection = (hot, data) => { return hot; }; +/** + * Get grid data without trailing blank rows. + * @param {Object} hot Handonstable grid instance. + * @return {Array>} Grid data without trailing blank rows. + */ +const getTrimmedData = (hot) => { + const gridData = hot.getData(); + let lastEmptyRow = -1; + for (let i=gridData.length; i>=0; i--) { + if (hot.isEmptyRow(i)) { + lastEmptyRow = i; + } else { + break; + } + } + + return lastEmptyRow === -1 ? gridData : gridData.slice(0, lastEmptyRow); +}; + /** * Download grid headers and data to file. * @param {Array>} matrix Grid data. @@ -677,7 +696,7 @@ $(document).ready(() => { try { const baseName = $('#base-name-save-as-input').val(); const ext = $('#file-ext-save-as-select').val(); - const matrix = [...getFlatHeaders(DATA), ...HOT.getData()]; + const matrix = [...getFlatHeaders(DATA), ...getTrimmedData(HOT)]; exportFile(matrix, baseName, ext, XLSX); $('#save-as-modal').modal('hide'); } catch (err) {