Advertisement
RemcoE33

CONSECUTIVE_DATES

Mar 12th, 2022
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Calculates the maximimun consecutive dates.
  3. *
  4. * @param {A1:A5} range Input range.
  5. * @return {number} result.
  6. * @customfunction
  7. */
  8. function CONSECUTIVE_DATES(range) {
  9.   if (!Array.isArray(range)) {
  10.     throw new Error('No range is givin');
  11.   } else if (range[0].length > 1) {
  12.     throw new Error('Only supports single columns')
  13.   }
  14.  
  15.   const log = []
  16.   let max = 1;
  17.   let counter = 1;
  18.  
  19.   range.flat().forEach((date, i, array) => {
  20.     const nextDate = array[i + 1]
  21.     if (date != "" && nextDate != "") {
  22.  
  23.       if (isValid(date) == false || isValid(nextDate) == false) {
  24.         throw new Error('Input is not of type date')
  25.       };
  26.  
  27.       const difference = nextDate.getTime() - date.getTime()
  28.      
  29.  
  30.       if (difference === 86400000) {
  31.         counter++
  32.       } else if (counter > max) {
  33.         max = counter
  34.         counter = 1
  35.       } else {
  36.         counter = 1
  37.       }
  38.     }
  39.   })
  40.  
  41.   console.log(log)
  42.   return (counter > max) ? counter : max
  43. }
  44.  
  45. function isValid(date) {
  46.   return date.getTime() === date.getTime()
  47. }
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement