Skip to contents

Create / delete a named region

Usage

wb_add_named_region(
  wb,
  sheet = current_sheet(),
  dims = "A1",
  name,
  local_sheet = FALSE,
  overwrite = FALSE,
  comment = NULL,
  custom_menu = NULL,
  description = NULL,
  is_function = NULL,
  function_group_id = NULL,
  help = NULL,
  hidden = NULL,
  local_name = NULL,
  publish_to_server = NULL,
  status_bar = NULL,
  vb_procedure = NULL,
  workbook_parameter = NULL,
  xml = NULL,
  ...
)

wb_remove_named_region(wb, sheet = current_sheet(), name = NULL)

Arguments

wb

A workbook object

sheet

A name or index of a worksheet

dims

Worksheet dimension, single cell ("A1") or cell range ("A1:D4")

name

Name for region. A character vector of length 1. Note region names musts be case-insensitive unique.

local_sheet

If TRUE the named region will be local for this sheet

overwrite

Boolean. Overwrite if exists? Default to FALSE

comment

description text for named region

custom_menu

customMenu (unknown xml feature)

description

description (unknown xml feature)

is_function

function (unknown xml feature)

function_group_id

function group id (unknown xml feature)

help

help (unknown xml feature)

hidden

hidden if the named region should be hidden

local_name

localName (unknown xml feature)

publish_to_server

publish to server (unknown xml feature)

status_bar

status bar (unknown xml feature)

vb_procedure

wbProcedure (unknown xml feature)

workbook_parameter

workbookParameter (unknown xml feature)

xml

xml (unknown xml feature)

...

additional arguments

Details

Region is given by: min(cols):max(cols) X min(rows):max(rows)

Examples

## 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",
  dims = wb_dims(
    rows = seq_len(nrow(iris) + 1),
    cols = seq_along(iris)
  )
)


## using write_data 'name' argument
wb$add_data(sheet = 1, x = iris, name = "iris2", startCol = 10)

out_file <- temp_xlsx()
wb_save(wb, out_file, overwrite = TRUE)

## see named regions
wb_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
wb_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

## delete one
wb$remove_named_region(name = "iris2")
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

## 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