Created
January 1, 2019 02:18
-
-
Save winterlightning/76d935b623bcb43bf031e7f7fb6bd7dc to your computer and use it in GitHub Desktop.
google calendar script that I wrote to write calendar for accounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function write_calendar_from_events() { | |
var firebaseUrl = "https://officeiq-87f37.firebaseio.com/"; | |
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl); | |
var data = base.getData("/users"); | |
var account = base.getData("/reverse"); | |
//Create a tech's previous events via calendar | |
Logger.clear() | |
var all_account = base.getData("/accounts"); | |
var reports = base.getData("/reportsheet"); | |
var all_jobs_by_account_key = {} | |
for (index in data) { | |
Logger.log("tech_name: "+index) | |
techone = data[index] | |
techone["id"]= index | |
//write_tech_calendar(techone, account) | |
//write_master_calendar(techone, account) | |
all_jobs_by_account_key = write_all_account_array(techone, all_jobs_by_account_key, account) | |
} | |
//Logger.log("all_jobs_by_account_key: "+JSON.stringify(all_jobs_by_account_key)) | |
//write to the | |
for (x in all_jobs_by_account_key) { | |
write_account_calendar(x, all_jobs_by_account_key[x]) | |
} | |
} | |
function calculate_hours(min) { | |
var hour; | |
if ((min < 60) && (min > 0)) { | |
hour = 1; | |
} else { | |
hour = Math.floor(min / 30); | |
hour /= 2; | |
if (hour === min / 60) { | |
console.log("hour is the same without processing"); | |
} else { | |
hour += 0.5; | |
} | |
} | |
return hour; | |
}; | |
//write array of jobs by accounts into a dictionary where the key | |
//is their name | |
function write_all_account_array(tech_data, all_jobs_by_account_key, account) { | |
//Logger.log("Tech one: ") | |
//Logger.log(tech_data) | |
for (x in techone["jobs"]) { | |
Logger.log(x) | |
job = tech_data["jobs"][x] | |
client = account[job["client"]] | |
job["tech"] = tech_data.username | |
//Logger.log(client+" "+JSON.stringify(tech_data["jobs"][x])) | |
if (all_jobs_by_account_key[client]) { | |
all_jobs_by_account_key[client].push(tech_data["jobs"][x]) | |
} | |
else { | |
all_jobs_by_account_key[client]=[] | |
all_jobs_by_account_key[client].push(tech_data["jobs"][x]) | |
} | |
} | |
return all_jobs_by_account_key | |
} | |
//write an attribute to a firebase account | |
function write_account_attribute(account_name, key, value) { | |
var firebaseUrl = "https://officeiq-87f37.firebaseio.com/"; | |
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl); | |
var data = {}; | |
data[key] = value; | |
path = "accounts/"+account_name | |
Logger.log(base.updateData(path, data)); | |
} | |
function write_user_attribute(user_id, key, value) { | |
var firebaseUrl = "https://officeiq-87f37.firebaseio.com/"; | |
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl); | |
var data = {}; | |
data[key] = value; | |
path = "users/"+user_id | |
Logger.log(base.updateData(path, data)); | |
} | |
function test_write_account() { | |
write_user_attribute("U3JETU3T3", "calendar", "test") | |
} | |
function write_tech_calendar(tech_data, account) { | |
//check if calendar exists, if not create one | |
var calendar = null | |
var search = CalendarApp.getCalendarsByName(tech_data.username) | |
if (search.length == 0) { | |
calendar = CalendarApp.createCalendar(tech_data.username) | |
Logger.log("CREATED CALENDAR: "+tech_data.username) | |
} | |
else { | |
calendar= search[0] | |
Logger.log("FOUND CALENDAR: "+tech_data.username) | |
write_user_attribute(tech_data["id"], "calendar", calendar.getId()) | |
} | |
//write tech events | |
Logger.log("Tech one: ") | |
Logger.log(tech_data) | |
for (x in techone["jobs"]) { | |
Logger.log(x) | |
job = tech_data["jobs"][x] | |
var start = new Date(job["checkin"]) | |
var end = new Date(job["checkout"]) | |
title = account[job["client"]] | |
write_event(calendar, start, end, title, job["summary"]) | |
} | |
} | |
function write_master_calendar(tech_data, account) { | |
var search = CalendarApp.getCalendarsByName("Master Tech Calendar") | |
if (search.length == 0) { | |
calendar = CalendarApp.createCalendar("Master Tech Calendar") | |
Logger.log("CREATED CALENDAR: Master Tech Calendar") | |
} | |
else { | |
calendar= search[0] | |
Logger.log("FOUND CALENDAR: "+"Master Tech Calendar") | |
} | |
//write tech events | |
Logger.log("Tech one: ") | |
Logger.log(tech_data) | |
try { | |
for (x in techone["jobs"]) { | |
Logger.log(x) | |
job = tech_data["jobs"][x] | |
var start = new Date(job["checkin"]) | |
var end = new Date(job["checkout"]) | |
title = account[job["client"]] | |
write_event(calendar, start, end, tech_data.username+":"+title, job["summary"]) | |
} | |
}catch (e) | |
{ | |
Logger.log("Exception: "+e) | |
} | |
} | |
//This is the writer function for the account's calendar | |
function write_account_calendar(account_name, data) { | |
Logger.log("Account name: "+account_name+"data: "+data) | |
//check if calendar exists, if not create one | |
var calendar = null | |
var search = CalendarApp.getCalendarsByName(account_name) | |
if (search.length == 0) { | |
calendar = CalendarApp.createCalendar(account_name) | |
Logger.log("CREATED CALENDAR: "+account_name) | |
} | |
else { | |
calendar= search[0] | |
Logger.log("FOUND CALENDAR: "+account_name) | |
write_account_attribute(account_name, "calendar", calendar.getId()) | |
} | |
for (x in data) { | |
Logger.log(x) | |
job = data[x] | |
diff = calculate_hours(parseInt(job["minutes"], 10)) * 60 | |
var start = new Date(job["checkin"]) | |
var end = new Date(start.getTime() + diff*60000); | |
title = job["summary"] | |
if ("tech" in job) { | |
summary = job["tech"]+": "+job["summary"] | |
} | |
else { | |
summary = job["summary"] | |
} | |
write_event(calendar, start, end, title, summary) | |
} | |
} | |
function write_event(calendar, start, end, title, summary) { | |
Logger.log("calendar "+calendar.getName()) | |
var start_date = new Date(start) | |
var end_date = new Date(end) | |
find_event = calendar.getEvents(start_date, end_date, {}) | |
Logger.log(find_event) | |
if (find_event.length == 0) { | |
var event = calendar.createEvent(title, | |
start_date, | |
end_date, | |
{description: summary }) | |
Logger.log(event) | |
} | |
else { | |
for (e in find_event) { | |
Logger.log("e: "+find_event[e].getTitle()) | |
if (title == find_event[e].getTitle()) { | |
Logger.log("matched "+find_event[e].getTitle()) | |
return | |
} | |
} | |
var event = calendar.createEvent(title, | |
start_date, | |
end_date, | |
{description: summary }) | |
Logger.log(event) | |
} | |
} | |
function test_event() { | |
var calendar = null | |
var search = CalendarApp.getCalendarsByName("Master Tech Calendar") | |
if (search.length == 0) { | |
calendar = CalendarApp.createCalendar("Master Tech Calendar") | |
Logger.log("CREATED CALENDAR: Master Tech Calendar") | |
} | |
else { | |
calendar= search[0] | |
Logger.log("FOUND CALENDAR: "+"Master Tech Calendar "+calendar) | |
} | |
write_event(calendar, "Sun Aug 27 2017 15:09:35 GMT-0700 (PDT)", "Sun Aug 27 2017 17:09:35 GMT-0700 (PDT)", "Test TWO", "This is my summary") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment