Created
November 9, 2023 21:04
-
-
Save allaway/e86402989a70ebde9efe8f181316fca6 to your computer and use it in GitHub Desktop.
convert column names in a google sheet to camelCase
This file contains hidden or 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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This might come in handy later...