Skip to content

Instantly share code, notes, and snippets.

@JBGruber
Last active October 4, 2022 10:55
Show Gist options
  • Save JBGruber/ac2cb82091006e19f08c2e4c22a089bb to your computer and use it in GitHub Desktop.
Save JBGruber/ac2cb82091006e19f08c2e4c22a089bb to your computer and use it in GitHub Desktop.
#' Make ICS calendar file
#'
#' Make an ICS calendar file from the text-as-data reading group Google sheet
#' for importing events into your calendar.
#'
#' @param sheet URL of the Google sheet (current URL in example).
#' @param location currently the link to register on Zoom.
#' @param outfile file name of the output ics file.
#'
#' @return
#' @export
#'
#' @examples
#' make_ics("https://docs.google.com/spreadsheets/d/1FJlqAPeJJbRTYy-ao21f_MgVSipoBTVNMNhYg0TQcqY/edit?pli=1#gid=196289460")
make_ics <- function(sheet,
outfile = "tada_reading_group.ics") {
sheet <- dplyr::mutate(
googlesheets4::read_sheet(sheet),
start_time = paste(Date, `TIME (CET)`),
start_time = stringr::str_replace(start_time, "(am|pm)", ":00 \\1"),
start_time = lubridate::with_tz(lubridate::ymd_hm(start_time, tz = "CET"), "UTC"),
end_time = start_time + 60 * 60
)
events <- unlist(lapply(seq_len(nrow(sheet)), function(i) {
c("BEGIN:VEVENT",
"SUMMARY:TADA Polsci Reading Group",
paste0("UID:tada_polsci", as.integer(sheet$start_time[i])),
"SEQUENCE:0",
"STATUS:CONFIRMED",
"TRANSP:TRANSPARENT",
paste0("DTSTART;VALUE=DATE-TIME:", format(sheet$start_time[i], "%Y%m%dT%H%M00Z")),
paste0("DTEND;VALUE=DATE-TIME:", format(sheet$end_time[i], "%Y%m%dT%H%M00Z")),
paste0("LOCATION:", sheet$Location[i]),
paste0("SUMMARY: TADA Polsci Reading Group: ", sheet$`Presenter/Chair`[i], ": ", sheet$Title[i]),
paste0("DESCRIPTION: Text-as-Data Reading Group ", sheet$`Session type`[i], "\\n\\nRegister via ", sheet$Location[i]),
"END:VEVENT")
}))
out <- c(
"BEGIN:VCALENDAR",
"VERSION:2.0",
"PRODID:-//tada_polsci//NONSGML v1.0//EN",
"CALSCALE:GREGORIAN",
events,
"END:VCALENDAR")
outfile <- ifelse(tools::file_ext(outfile) != "ics", paste0(outfile, ".ics"), outfile)
message("Writing to file: ", outfile, appendLF = F)
writeLines(out, outfile, useBytes = TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment