Skip to content

Instantly share code, notes, and snippets.

@allaway
Created November 9, 2023 21:04
Show Gist options
  • Save allaway/e86402989a70ebde9efe8f181316fca6 to your computer and use it in GitHub Desktop.
Save allaway/e86402989a70ebde9efe8f181316fca6 to your computer and use it in GitHub Desktop.
convert column names in a google sheet to camelCase
library(googlesheets4)
gs4_auth()
convert_colname_to_camelcase <- function(ss_url) {
# Read the sheet
ss_data <- range_read(ss_url, n_max = 0)
colnames(ss_data)[colnames(ss_data)!="Component"] <- sapply(colnames(ss_data)[colnames(ss_data)!="Component"], stringr::str_replace, "_", " ")
# Convert column names to camelCase
colnames(ss_data)[colnames(ss_data)!="Component"] <- sapply(colnames(ss_data)[colnames(ss_data)!="Component"], R.utils::toCamelCase)
# Update the Google Sheet with the new column names
range_write(ss_url, ss_data, range = "A1", col_names = TRUE, reformat = FALSE)
}
foo <- googledrive::drive_ls("https://drive.google.com/drive/u/1/folders/1NJcXcGEDWmlTsRtyLtuwgH_5pbX0Dd9C")
sapply(foo$id, convert_colname_to_camelcase)
check_colnames <- function(ss_url) {
# Read the sheet
ss_data <- range_read(ss_url, n_max = 0)
foo <- all(colnames(ss_data) %in% schema)
if(!foo){
message(glue::glue('error in {ss_url}'))
message(glue::glue('issue with {colnames(ss_data)[!colnames(ss_data) %in% schema]} '))
}
}
bar <- googledrive::drive_ls("https://drive.google.com/drive/u/1/folders/1c6FbMJgcpNMwjHwtu-ikqftzIio3yPJ9")
res <- sapply(bar$id, check_colnames)
@allaway
Copy link
Author

allaway commented Nov 9, 2023

This might come in handy later...

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