Learn R Programming

openxlsx2 (version 0.6.1)

named_region: Create / delete a named region

Description

Create / delete a named region

Return a vector of named regions in a xlsx file or Workbook object

Usage

wb_add_named_region(
  wb,
  sheet = current_sheet(),
  cols,
  rows,
  name,
  localSheet = FALSE,
  overwrite = FALSE,
  comment = NULL,
  customMenu = NULL,
  description = NULL,
  is_function = NULL,
  functionGroupId = NULL,
  help = NULL,
  hidden = NULL,
  localName = NULL,
  publishToServer = NULL,
  statusBar = NULL,
  vbProcedure = NULL,
  workbookParameter = NULL,
  xml = NULL
)

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

get_named_regions(x)

wb_get_named_regions(x)

Arguments

wb

A workbook object

sheet

A name or index of a worksheet

cols

Numeric vector specifying columns to include in region

rows

Numeric vector specifying rows to include in region

name

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

localSheet

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

customMenu

customMenu (unknown xml feature)

description

description (unknown xml feature)

is_function

function (unknown xml feature)

functionGroupId

function group id (unknown xml feature)

help

help (unknown xml feature)

hidden

hidden if the named region should be hidden

localName

localName (unknown xml feature)

publishToServer

publish to server (unknown xml feature)

statusBar

status bar (unknown xml feature)

vbProcedure

wbProcedure (unknown xml feature)

workbookParameter

workbookParameter (unknown xml feature)

xml

xml (unknown xml feature)

x

An xlsx file or Workbook object

Details

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

See Also

wb_add_named_region() wb_remove_named_region()

Examples

Run this code
## 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
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
wb_get_named_regions(out_file) ## From xlsx file

## delete one
wb$remove_named_region(name = "iris2")
wb_get_named_regions(wb)

## read named regions
df <- read_xlsx(wb, namedRegion = "iris")
head(df)

df <- read_xlsx(out_file, namedRegion = "iris2")
head(df)
## 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
wb_get_named_regions(wb) ## From Workbook object
wb_get_named_regions(out_file) ## From xlsx file

## read named regions
df <- read_xlsx(wb, namedRegion = "iris")
head(df)

df <- read_xlsx(out_file, namedRegion = "iris2")
head(df)

Run the code above in your browser using DataLab