Skip to content

Instantly share code, notes, and snippets.

@snahor
Created Jun 16, 2016
Embed
What would you like to do?
const findLastRowWithValue = (sheet) => {
const { decode_cell } = XLSX.utils;
const { r } = decode_cell(sheet['!ref'].split(':')[1]);
const isEmpty = (x) => sheet[`A${ x }`].v === undefined;
let start = 1;
let end = r + 1;
let pivot = Math.round((end - start) / 2);
if (pivot === 0) {
log.error({start,end,pivot})
return !isEmpty(start) ? start : null;
//return start;
}
let iterations = 10;
while (true) {
if (iterations == 0) {
break;
}
log.debug({
start: [start,isEmpty(start)],
pivot: [pivot,isEmpty(pivot)],
end: [end,isEmpty(end)],
})
// base condition
if (pivot === end && !isEmpty(pivot - 1) && isEmpty(pivot)) {
return pivot - 1;
}
if (isEmpty(pivot)) {
end = pivot;
pivot = Math.round((pivot + start) / 2);
} else {
start = pivot;
pivot = Math.round((end + pivot) / 2);
}
iterations--;
}
return pivot;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment