Skip to content

Commit

Permalink
#37: Pad sample rows to the full width of the sheet. (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaric-rd authored Sep 4, 2024
1 parent 3a0b103 commit b9c9696
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/importer/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,22 @@ exports.SessionSuggestDataRange = (sid, headerRange, footerRange) => {
}
}

// Take a slice from an array, but if the array isn't long enough to reach to
// end, append nulls to make the result the correct length.
function sliceAndPad(row, start, end) {
// Slice row; but this may result in fewer elements than we want if the row
// wasn't that long to begin with
const sliced = row.slice(start, end);

if (sliced.length < (end-start)) {
let padding = new Array(end-sliced.length);
padding.fill(null);
return sliced.concat(padding);
} else {
return sliced;
}
}

// Returns a sample of rows in a range. range is of the form {sheet: 'Foo', start:{row: X, column: Y}, end:{row: X, column: Y}}.

// Returns three arrays - one with startCount rows from the top of the range,
Expand Down Expand Up @@ -224,9 +240,9 @@ exports.SessionGetInputSampleRows = (sid, range, startCount, middleCount, endCou
// data.

// Slice out only desired columns from these rows, and return the results
return [startRows.map((row) => row.slice(range.start.column, range.end.column+1)),
middleRows.map((row) => row.slice(range.start.column, range.end.column+1)),
endRows.map((row) => row.slice(range.start.column, range.end.column+1))];
return [startRows.map((row) => sliceAndPad(row, range.start.column, range.end.column+1)),
middleRows.map((row) => sliceAndPad(row, range.start.column, range.end.column+1)),
endRows.map((row) => sliceAndPad(row, range.start.column, range.end.column+1))];
};

// Return the unique values in each column in the range. Return no more than
Expand Down

0 comments on commit b9c9696

Please sign in to comment.