Skip to content

Move (work)sheets around within a (spread)Sheet. The outcome is most predictable for these common and simple use cases:

  • Reorder and move one or more sheets to the front.

  • Move a single sheet to a specific (but arbitrary) location.

  • Move multiple sheets to the back with .after = 100 (.after can be any number greater than or equal to the number of sheets).

If your relocation task is more complicated and you are puzzled by the result, break it into a sequence of simpler calls to sheet_relocate().

Usage

sheet_relocate(ss, sheet, .before = if (is.null(.after)) 1, .after = NULL)

Arguments

ss

Something that identifies a Google Sheet:

  • its file id as a string or drive_id

  • a URL from which we can recover the id

  • a one-row dribble, which is how googledrive represents Drive files

  • an instance of googlesheets4_spreadsheet, which is what gs4_get() returns

Processed through as_sheets_id().

sheet

Sheet to relocate, in the sense of "worksheet" or "tab". You can identify a sheet by name, with a string, or by position, with a number. You can pass a vector to move multiple sheets at once or even a list, if you need to mix names and positions.

.before, .after

Specification of where to locate the sheets(s) identified by sheet. Exactly one of .before and .after must be specified. Refer to an existing sheet by name (via a string) or by position (via a number).

Value

The input ss, as an instance of sheets_id

See also

Examples

sheet_names <- c("alfa", "bravo", "charlie", "delta", "echo", "foxtrot")
ss <- gs4_create("sheet-relocate-demo", sheets = sheet_names)
#>  Creating new Sheet: sheet-relocate-demo.
sheet_names(ss)
#> [1] "alfa"    "bravo"   "charlie" "delta"   "echo"    "foxtrot"

# move one sheet, forwards then backwards
ss %>%
  sheet_relocate("echo", .before = "bravo") %>%
  sheet_names()
#>  Relocating sheets in sheet-relocate-demo.
#> [1] "alfa"    "echo"    "bravo"   "charlie" "delta"   "foxtrot"
ss %>%
  sheet_relocate("echo", .after = "delta") %>%
  sheet_names()
#>  Relocating sheets in sheet-relocate-demo.
#> [1] "alfa"    "bravo"   "charlie" "delta"   "echo"    "foxtrot"

# reorder and move multiple sheets to the front
ss %>%
  sheet_relocate(list("foxtrot", 4)) %>%
  sheet_names()
#>  Relocating sheets in sheet-relocate-demo.
#> [1] "foxtrot" "delta"   "alfa"    "bravo"   "charlie" "echo"   

# put the sheets back in the original order
ss %>%
  sheet_relocate(sheet_names) %>%
  sheet_names()
#>  Relocating sheets in sheet-relocate-demo.
#> [1] "alfa"    "bravo"   "charlie" "delta"   "echo"    "foxtrot"

# reorder and move multiple sheets to the back
ss %>%
  sheet_relocate(c("bravo", "alfa", "echo"), .after = 10) %>%
  sheet_names()
#>  Relocating sheets in sheet-relocate-demo.
#> [1] "charlie" "delta"   "foxtrot" "bravo"   "alfa"    "echo"   

# clean up
gs4_find("sheet-relocate-demo") %>%
  googledrive::drive_trash()
#> File trashed:
#>  sheet-relocate-demo <id: 10Q_239oz69VoGurYvqvN_BzNHZICuhKRMYXz7y4E-qo>