R/update_google_sheet.R

#' Function To Update Google Sheet
#' 
#' This function updates the Google sheet with the latest stats
#' @keywords update google sheet
#' @export
#' @examples
#' update_google_sheet()

update_google_sheet <- function(AllMatchInfo,Leaders){

new_sheet_contents <- c(list(AllMatchInfo = AllMatchInfo),Leaders)

wc2018stats_sheet <- googlesheets::gs_title("wc2018stats")

wc2018stats_current_worksheets <- googlesheets::gs_ws_ls(wc2018stats_sheet)

# add temporary placeholder to ensure sheet never has zero worksheets
googlesheets::gs_ws_new(wc2018stats_sheet,"placeholder")
wc2018stats_sheet <- googlesheets::gs_title("wc2018stats")

# delete existing sheets
for (i in c(1:length(wc2018stats_current_worksheets))){
  googlesheets::gs_ws_delete(wc2018stats_sheet,wc2018stats_current_worksheets[i])
  wc2018stats_sheet <- googlesheets::gs_title("wc2018stats")
}

# create new sheets
for (i in c(1:length(new_sheet_contents))){
  googlesheets::gs_ws_new(wc2018stats_sheet,names(new_sheet_contents)[i])
  wc2018stats_sheet <- googlesheets::gs_title("wc2018stats")
}

# populate new sheets with updated data
for (i in c(1:length(new_sheet_contents))){
  googlesheets::gs_edit_cells(wc2018stats_sheet, ws = names(new_sheet_contents)[i], anchor = "A1", input = new_sheet_contents[[i]], byrow=FALSE)
  wc2018stats_sheet <- googlesheets::gs_title("wc2018stats")
}

# delete temporary sheet
googlesheets::gs_ws_delete(wc2018stats_sheet, ws = "placeholder")

}
JimmyClowes/wc2018SweepstakeStats documentation built on May 30, 2019, 4:05 a.m.