1

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

Bensstats
  • 988
  • 5
  • 17
  • 1
    Is there a way in the native Sheets interface? What have you searched for in the API docs and teh package docs? – IRTFM Jul 28 '23 at 17:25
  • @IRTFM I'm sure there is but I'm developing with the `googlesheets4` package and would prefer to use that directly. – Bensstats Jul 28 '23 at 17:34
  • 3
    I’m just saying you should give more background and make more effort at investigating if you want help from a group of people who don’t use both platforms. Just an attempt at formative criticism. – IRTFM Jul 29 '23 at 01:46

1 Answers1

3

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

Paul
  • 8,734
  • 1
  • 26
  • 36