Return a vector of named regions in a xlsx file or Workbook object
get_named_regions(x)
An xlsx file or Workbook object
## create named regions
wb <- wb_workbook()
wb$add_worksheet("Sheet 1")
## specify region
wb$add_data(sheet = 1, x = iris, startCol = 1, startRow = 1)
wb$add_named_region(
sheet = 1,
name = "iris",
rows = seq_len(nrow(iris) + 1),
cols = seq_along(iris)
)
## using write_data 'name' argument to create a named region
wb$add_data(sheet = 1, x = iris, name = "iris2", startCol = 10)
out_file <- temp_xlsx()
wb$save(out_file, overwrite = TRUE)
## see named regions
get_named_regions(wb) ## From Workbook object
#> 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
get_named_regions(out_file) ## From xlsx file
#> 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
## read named regions
df <- read_xlsx(wb, namedRegion = "iris")
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
df <- read_xlsx(out_file, namedRegion = "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