Skip to content

Commit 623c8f6

Browse files
author
Pedro H
committed
Initial commit (version 1.0)
1 parent f17a2bd commit 623c8f6

5 files changed

Lines changed: 144 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Unique and original calendar/task organizer spreadsheet template.
33

44
## To-do
55
- [x] Google Sheets compatible;
6-
- [ ] Apps Script autorun trigger;
6+
- [x] Apps Script autorun trigger;
77
- [ ] LibreOffice/OpenOffice Calc (.ods) compatible;
88
- [ ] Microsoft Excel (.xlsx) compatible;
99

sheets/Calendario-1.0.url

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[{000214A0-0000-0000-C000-000000000046}]
2+
Prop3=19,11
3+
[InternetShortcut]
4+
IDList=
5+
URL=https://docs.google.com/spreadsheets/d/1d1RFg44j5Ls4eoM9p6Ud4sZRzNaMkRAwsUDxzFm0vZM/edit?usp=sharing

src/sheets/appsscript.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"timeZone": "America/Sao_Paulo",
3+
"dependencies": {
4+
},
5+
"exceptionLogging": "STACKDRIVER",
6+
"runtimeVersion": "V8"
7+
}

src/sheets/main.gs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @file main.gs
3+
* @brief Describes the routines to follow in order to retrieve the upcoming events' respective dates.
4+
* @author Pedro Henrique Pinto de Oliveira
5+
* @date 2024-03-04
6+
*/
7+
8+
/* Constants */
9+
const ss = SpreadsheetApp.getActiveSpreadsheet(); // gets spreadsheet
10+
const mainSheet = ss.getSheets()[0]; // gets first sheet
11+
const globalUpcomingDatesRange = "B3:B1000"; // all the possible cells which can be possibly filled with an event's date
12+
const todayDay = new Date().getDate();
13+
const sheet = ss.getSheets()[new Date().getMonth() + 1]; // getMonth() returns 0-11 months, not 1-12
14+
15+
/* Functions */
16+
function findEvent_returnDate()
17+
{
18+
let data = sheet.getDataRange().getValues(); // macro to retrieve (int row, int col) cell data
19+
let nDatesWritten = 0;
20+
21+
for(let i = (1+todayDay); i <= 32; i++) {
22+
// Columns C:G
23+
24+
for(let j = 3; j <= 7; j++) {
25+
if(sheet.getRange(i, j).getValue() == '') {} else {
26+
mainSheet.getRange(3 + nDatesWritten, 2).setValue(sheet.getRange(i+1, 1).getValue());
27+
nDatesWritten++;
28+
}
29+
}
30+
}
31+
}
32+
33+
function clearOutdatedEvents()
34+
{
35+
mainSheet.getRange(globalUpcomingDatesRange).clear();
36+
}
37+
38+
function highlightToday_sEntry()
39+
{
40+
let sheet = ss.getSheets()[new Date().getMonth() + 1];
41+
let dd = 2 + new Date().getDate();
42+
43+
sheet.getRange(dd-1, 1).setBackgroundRGB(255, 255, 255)
44+
sheet.getRange(dd, 1).setBackgroundRGB(255, 255, 0);
45+
}
46+
47+
function main()
48+
{
49+
console.log(setup(true));
50+
highlightToday_sEntry();
51+
clearOutdatedEvents();
52+
findEvent_returnDate();
53+
}

src/sheets/setup.gs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)