Skip to content

Commit

Permalink
Trim trailing blank rows (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivansg44 authored Jun 11, 2020
1 parent f02aada commit 5b277ce
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<String>>} 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<Array<String>>} matrix Grid data.
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5b277ce

Please sign in to comment.