|
| 1 | +/** |
| 2 | + * @file setup.gs |
| 3 | + * @brief This run once script creates sample events for the next three days to help users understand the functionality. |
| 4 | + * @author Pedro Henrique Pinto de Oliveira |
| 5 | + * @date 2024-03-16 |
| 6 | + */ |
| 7 | + |
| 8 | +/* Constants */ |
| 9 | +// Global scope constants borrowed from main.gs |
| 10 | +const entry_firstEventColumn = 3; |
| 11 | +const tableHeader_rowValue_offset = 2; |
| 12 | + |
| 13 | +/* Functions */ |
| 14 | +function is_LeapYear(year) |
| 15 | +{ |
| 16 | + if(year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) |
| 17 | + { |
| 18 | + return true; |
| 19 | + } |
| 20 | + return false; |
| 21 | +} |
| 22 | + |
| 23 | +function find_AmountOfDaysLeftInMonth(date, month, year) |
| 24 | +{ |
| 25 | + switch (month){ |
| 26 | + case 1: |
| 27 | + case 3: |
| 28 | + case 5: |
| 29 | + case 7: |
| 30 | + case 8: |
| 31 | + case 10: |
| 32 | + case 12: |
| 33 | + return (31 - date); |
| 34 | + break; |
| 35 | + case 2: |
| 36 | + if(is_LeapYear(year)){ |
| 37 | + return (29 - date); |
| 38 | + } |
| 39 | + return (28 - date); |
| 40 | + break; |
| 41 | + case 4: |
| 42 | + case 6: |
| 43 | + case 9: |
| 44 | + case 11: |
| 45 | + return (30 - date); |
| 46 | + break; |
| 47 | + default: |
| 48 | + break; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function setup(mainFunctionCall) { |
| 53 | + // If sheet already contains values, must NOT setup |
| 54 | + if(!(mainSheet.getRange(3, 2).isBlank())) return false; |
| 55 | + |
| 56 | + const sampleEvents = ["Evento 1", "Evento 2", "Evento 3"]; |
| 57 | + |
| 58 | + let year = new Date().getFullYear(); |
| 59 | + let month = new Date().getMonth() + 1; // returns 0-11 months |
| 60 | + let nextMonth = month + 1; |
| 61 | + let date = new Date().getDate(); |
| 62 | + let amount = find_AmountOfDaysLeftInMonth(date, month, year); |
| 63 | + |
| 64 | + for(let i = date + tableHeader_rowValue_offset; i < (date+amount); i++){ |
| 65 | + if(sampleEvents.length === 0) break; |
| 66 | + sheet.getRange(i, entry_firstEventColumn).setValue(sampleEvents.shift()); // allows stack-based JavaScript array to behave like a queue |
| 67 | + } |
| 68 | + if(amount < 3 && sampleEvents.length === 0){ |
| 69 | + let i = 1 + tableHeader_rowValue_offset; // Begins at '@NextMonth'!C3's address |
| 70 | + while(sampleEvents.length() > 0){ |
| 71 | + ss.getSheets()[nextMonth].getRange(i, entry_firstEventColumn).setValue(sampleEvents.shift()); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if(!mainFunctionCall) main(); // If main() didn't call setup(), then main() must be run to findEvent_returnDate()s (might have been onOpen(e) trigger) |
| 76 | + |
| 77 | + return true; |
| 78 | +} |
0 commit comments