Skip to content

Instantly share code, notes, and snippets.

@snahor
Created June 16, 2016 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snahor/4386ead75f1f7cbbe7fbf273cc2f67eb to your computer and use it in GitHub Desktop.
Save snahor/4386ead75f1f7cbbe7fbf273cc2f67eb to your computer and use it in GitHub Desktop.
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