Skip to content

Adds one or more (work)sheets to an existing (spread)Sheet. Note that sheet names must be unique.

Usage

sheet_add(ss, sheet = NULL, ..., .before = NULL, .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

One or more new sheet names. If unspecified, one new sheet is added and Sheets autogenerates a name of the form "SheetN".

...

Optional parameters to specify additional properties, common to all of the new sheet(s). Not relevant to most users. Specify fields of the SheetProperties schema in name = value form.

.before, .after

Optional specification of where to put the new sheet(s). Specify, at most, one of .before and .after. Refer to an existing sheet by name (via a string) or by position (via a number). If unspecified, Sheets puts the new sheet(s) at the end.

Value

The input ss, as an instance of sheets_id

Examples

ss <- gs4_create("add-sheets-to-me")
#>  Creating new Sheet: add-sheets-to-me.

# the only required argument is the target spreadsheet
ss %>% sheet_add()
#>  Adding 1 sheet to add-sheets-to-me:
#>Sheet2

# but you CAN specify sheet name and/or position
ss %>% sheet_add("apple", .after = 1)
#>  Adding 1 sheet to add-sheets-to-me:
#>apple
ss %>% sheet_add("banana", .after = "apple")
#>  Adding 1 sheet to add-sheets-to-me:
#>banana

# add multiple sheets at once
ss %>% sheet_add(c("coconut", "dragonfruit"))
#>  Adding 2 sheets to add-sheets-to-me:
#>coconut
#>dragonfruit

# keeners can even specify additional sheet properties
ss %>%
  sheet_add(
    sheet = "eggplant",
    .before = 1,
    gridProperties = list(
      rowCount = 3, columnCount = 6, frozenRowCount = 1
    )
  )
#>  Adding 1 sheet to add-sheets-to-me:
#>eggplant

# get an overview of the sheets
sheet_properties(ss)
#> # A tibble: 7 × 8
#>   name        index         id type  visible grid_rows grid_columns data  
#>   <chr>       <int>      <int> <chr> <lgl>       <int>        <int> <list>
#> 1 eggplant        0 1049711327 GRID  TRUE            3            6 <NULL>
#> 2 Sheet1          1          0 GRID  TRUE         1000           26 <NULL>
#> 3 apple           2 1229661602 GRID  TRUE         1000           26 <NULL>
#> 4 banana          3 1581642120 GRID  TRUE         1000           26 <NULL>
#> 5 Sheet2          4  824329517 GRID  TRUE         1000           26 <NULL>
#> 6 coconut         5  712450966 GRID  TRUE         1000           26 <NULL>
#> 7 dragonfruit     6  143983947 GRID  TRUE         1000           26 <NULL>

# clean up
gs4_find("add-sheets-to-me") %>%
  googledrive::drive_trash()
#> File trashed:
#>  add-sheets-to-me <id: 1G_lE2liN57vT4RpHe1UMNBoyRZPloFTksvTiWRRP4xE>