Skip to content

Instantly share code, notes, and snippets.

@fetwar
Last active October 31, 2023 06:38
Show Gist options
  • Save fetwar/0c509d84477b4f029f69dd4001e7e6a1 to your computer and use it in GitHub Desktop.
Save fetwar/0c509d84477b4f029f69dd4001e7e6a1 to your computer and use it in GitHub Desktop.
Google Apps Script - Create calendar with daily events for your target balance burndown to track spending
function getCalendar(config) {
var calendars = CalendarApp.getCalendarsByName(config.calendarName);
if (calendars.length === 0) {
var calendar = CalendarApp.createCalendar(config.calendarName, config.calendarConfig);
console.log('Created calendar: ' + calendar.getName());
} else {
if (calendars.length > 1) {
console.warn("Non critical error: multiple calendars with applicable name.", calendars)
}
var calendar = calendars[0];
}
return calendar
}
function createSpendingEvent(calendar, eventName, eventDate, reminders) {
var events = calendar.getEventsForDay(eventDate);
var eventExists = false;
for (var i = 0; i < events.length; i++) {
if (events[i].getTitle() === eventName) {
eventExists = true;
console.warn('Event with the same name already exists: ' + eventName);
return;
}
}
var event = calendar.createAllDayEvent(eventName, eventDate);
console.log('Event created: ' + eventName);
for (var i = 0; i < reminders.length; i++) {
if (reminders[i].method === "popup") {
event.addPopupReminder(reminders[i].minutes);
} else if (reminders[i].method === "email") {
event.addEmailReminder(reminders[i].minutes);
} else if (reminders[i].method === "sms") {
event.addSmsReminder(reminders[i].minutes);
}
console.log('\tReminder added: ' + JSON.stringify(reminders[i]));
}
}
function getDaysTillEOM(today) {
var monthEnd = new Date(today.getFullYear(), today.getMonth() + 1, 1);
return Math.floor((monthEnd - today) / (1000 * 60 * 60 * 24));
}
function main() {
var config = {
"calendarName": "Bank Balances",
"calendarConfig": {
"color": "#FA573C",
"timeZone": "Australia/Brisbane"
},
"dailySpend": 10,
"reminders": [{
"method": "popup",
"minutes": 420
}, {
"method": "sms",
"minutes": 420
}]
};
var cal = getCalendar(config);
var processingDate = new Date();
var currentMonth = processingDate.getMonth();
var daysRemaining = calcDaysTillEOM(processingDate);
while (processingDate.getMonth() === currentMonth) {
daysRemaining = getDaysTillEOM(processingDate);
var eventTitle = 'Balance: $' + (config.dailySpend * (daysRemaining + 1)).toFixed(2);
createSpendingEvent(cal, eventTitle, processingDate, config.reminders);
processingDate.setDate(processingDate.getDate() + 1);
}
}
@fetwar
Copy link
Author

fetwar commented Apr 11, 2023

Unsure if the SMS notifications work tbh, but I saw it in the reference docs and wanted to try.

Docs: https://developers.google.com/apps-script/reference/calendar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment