Created
June 16, 2016 19:22
-
-
Save snahor/4386ead75f1f7cbbe7fbf273cc2f67eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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