Bensstats
Bensstats

Reputation: 1056

How to color a Google Sheets tab in R with googlesheets4

I am working the googlesheets4 package in R and I'm wondering if there is a function that exists for changing the color of a tab?

For example, I am looking to make color a tab brown.

Before:

enter image description here

After:

enter image description here

Upvotes: 1

Views: 149

Answers (1)

Paul
Paul

Reputation: 9097

googlesheets4 does not support setting tab color. However, the Sheets v4 API does.

The function, sheet_set_tab_color, can be created like other functions in googlesheets4 which update sheet properties.

library(googlesheets4)


sheet_set_tab_color <- function(ss, sheet = NULL, r, g, b) {
  ssid <- as_sheets_id(ss)
  sp <- list(
    sheetId = googlesheets4:::lookup_sheet(sheet, sheets_df = gs4_get(ssid)$sheets)$id,
    tabColorStyle = list(rgbColor = list(red = r, green = g, blue = b))
  )
  
  update_req <- list(
    properties = sp,
    fields = gargle::field_mask(sp)
  )
  
  req <- request_generate(
    "sheets.spreadsheets.batchUpdate",
    params = list(
      spreadsheetId = ssid,
      requests = list(updateSheetProperties = update_req)
    )
  )
  
  resp_raw <- request_make(req)
  gargle::response_process(resp_raw)
  
  invisible(ssid)
}

This sets the tab color as expected

ss <- sheet_write(iris)
sheet_set_tab_color(ss, sheet = NULL, r = 1.0, g = 0.3, b = 0.4)

Colored tab

Upvotes: 3

Related Questions