From 5b277ce08f64bc667cb9c85cd4e6f30df89b794d Mon Sep 17 00:00:00 2001 From: ivansg44 Date: Thu, 11 Jun 2020 14:23:46 -0700 Subject: [PATCH] Trim trailing blank rows (#57) --- main.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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) {