Skip to contents

Get named regions in a workbook

Usage

wb_get_named_regions(wb, tables = FALSE, x = NULL)

Arguments

wb

A wbWorkbook object

tables

Should data tables be included in the result?

x

deprecated. Use wb. For Excel input use wb_load() to first load the xlsx file as a workbook.

Value

A vector of named regions in x.

A data frame with the all named regions in wb. Or NULL, if none are found.

Examples

wb <- wb_workbook()
wb$add_worksheet("Sheet 1")

## specify region
wb$add_data(x = iris, start_col = 1, start_row = 1)
wb$add_named_region(
  name = "iris",
  dims = wb_dims(x = iris)
)$add_data(sheet = 1, x = iris, name = "iris2", start_col = 10)
## From Workbook object
wb_get_named_regions(wb)
#>    name                 value  sheets  coords id local sheet
#> 1  iris 'Sheet 1'!$A$1:$E$151 Sheet 1 A1:E151  1     0     1
#> 2 iris2     'Sheet 1'!J1:N151 Sheet 1 J1:N151  2     0     1
# Use this info to extract the data frame
df <- wb$to_df(named_region = "iris2")
head(df)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 2          5.1         3.5          1.4         0.2  setosa
#> 3          4.9         3.0          1.4         0.2  setosa
#> 4          4.7         3.2          1.3         0.2  setosa
#> 5          4.6         3.1          1.5         0.2  setosa
#> 6          5.0         3.6          1.4         0.2  setosa
#> 7          5.4         3.9          1.7         0.4  setosa

# Extract tables and named regions
wb$add_worksheet()$add_data_table(x = iris)

wb$get_named_regions(tables = TRUE)
#>     name                 value  sheets  coords id local sheet
#> 1   iris 'Sheet 1'!$A$1:$E$151 Sheet 1 A1:E151  1     0     1
#> 2  iris2     'Sheet 1'!J1:N151 Sheet 1 J1:N151  2     0     1
#> 3 Table1                 table Sheet 2 A1:E151 NA    NA     2

# Extract named regions from a file
out_file <- temp_xlsx()
wb_save(wb, out_file, overwrite = TRUE)

# Load the file as a workbook first, then get named regions.
wb1 <- wb_load(out_file)
wb1$get_named_regions()
#>    name                 value  sheets  coords id local sheet
#> 1  iris 'Sheet 1'!$A$1:$E$151 Sheet 1 A1:E151  1     0     1
#> 2 iris2     'Sheet 1'!J1:N151 Sheet 1 J1:N151  2     0     1