Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Calculates the maximimun consecutive dates.
- *
- * @param {A1:A5} range Input range.
- * @return {number} result.
- * @customfunction
- */
- function CONSECUTIVE_DATES(range) {
- if (!Array.isArray(range)) {
- throw new Error('No range is givin');
- } else if (range[0].length > 1) {
- throw new Error('Only supports single columns')
- }
- const log = []
- let max = 1;
- let counter = 1;
- range.flat().forEach((date, i, array) => {
- const nextDate = array[i + 1]
- if (date != "" && nextDate != "") {
- if (isValid(date) == false || isValid(nextDate) == false) {
- throw new Error('Input is not of type date')
- };
- const difference = nextDate.getTime() - date.getTime()
- if (difference === 86400000) {
- counter++
- } else if (counter > max) {
- max = counter
- counter = 1
- } else {
- counter = 1
- }
- }
- })
- console.log(log)
- return (counter > max) ? counter : max
- }
- function isValid(date) {
- return date.getTime() === date.getTime()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement