Learn R Programming

openxlsx2 (version 0.6.1)

wb_add_worksheet: Add a worksheet to a workbook

Description

Add a worksheet to a workbook

Usage

wb_add_worksheet(
  wb,
  sheet = next_sheet(),
  gridLines = TRUE,
  rowColHeaders = TRUE,
  tabColor = NULL,
  zoom = 100,
  header = NULL,
  footer = NULL,
  oddHeader = header,
  oddFooter = footer,
  evenHeader = header,
  evenFooter = footer,
  firstHeader = header,
  firstFooter = footer,
  visible = c("true", "false", "hidden", "visible", "veryhidden"),
  hasDrawing = FALSE,
  paperSize = getOption("openxlsx2.paperSize", default = 9),
  orientation = getOption("openxlsx2.orientation", default = "portrait"),
  hdpi = getOption("openxlsx2.hdpi", default = getOption("openxlsx2.dpi", default = 300)),
  vdpi = getOption("openxlsx2.vdpi", default = getOption("openxlsx2.dpi", default = 300)),
  ...
)

Value

The wbWorkbook object wb

Arguments

wb

A Workbook object to attach the new worksheet

sheet

A name for the new worksheet

gridLines

A logical. If FALSE, the worksheet grid lines will be hidden.

rowColHeaders

A logical. If FALSE, the worksheet colname and rowname will be hidden.

tabColor

Color of the worksheet tab. A valid color (belonging to colors()) or a valid hex color beginning with "#"

zoom

A numeric between 10 and 400. Worksheet zoom level as a percentage.

header, oddHeader, evenHeader, firstHeader, footer, oddFooter, evenFooter, firstFooter

Character vector of length 3 corresponding to positions left, center, right. header and footer are used to default additional arguments. Setting even, odd, or first, overrides header/footer. Use NA to skip a position.

visible

If FALSE, sheet is hidden else visible.

hasDrawing

If TRUE prepare a drawing output (TODO does this work?)

paperSize

An integer corresponding to a paper size. See ?ws_page_setup for details.

orientation

One of "portrait" or "landscape"

hdpi

Horizontal DPI. Can be set with options("openxlsx2.dpi" = X) or options("openxlsx2.hdpi" = X)

vdpi

Vertical DPI. Can be set with options("openxlsx2.dpi" = X) or options("openxlsx2.vdpi" = X)

...

...

Details

Headers and footers can contain special tags

  • &[Page] Page number

  • &[Pages] Number of pages

  • &[Date] Current date

  • &[Time] Current time

  • &[Path] File path

  • &[File] File name

  • &[Tab] Worksheet name

See Also

Other workbook wrappers: wb_add_chartsheet(), wb_add_data_table(), wb_add_data(), wb_add_formula(), wb_add_pivot_table(), wb_clone_worksheet(), wb_copy_cells(), wb_creators, wb_freeze_pane(), wb_get_base_font(), wb_save(), wb_set_col_widths(), wb_set_last_modified_by(), wb_set_row_heights(), wb_workbook(), workbook_grouping, ws_cell_merge

Examples

Run this code
## Create a new workbook
wb <- wb_workbook("Fred")

## Add 3 worksheets
wb$add_worksheet("Sheet 1")
wb$add_worksheet("Sheet 2", gridLines = FALSE)
wb$add_worksheet("Sheet 3", tabColor = "red")
wb$add_worksheet("Sheet 4", gridLines = FALSE, tabColor = "#4F81BD")

## Headers and Footers
wb$add_worksheet("Sheet 5",
  header = c("ODD HEAD LEFT", "ODD HEAD CENTER", "ODD HEAD RIGHT"),
  footer = c("ODD FOOT RIGHT", "ODD FOOT CENTER", "ODD FOOT RIGHT"),
  evenHeader = c("EVEN HEAD LEFT", "EVEN HEAD CENTER", "EVEN HEAD RIGHT"),
  evenFooter = c("EVEN FOOT RIGHT", "EVEN FOOT CENTER", "EVEN FOOT RIGHT"),
  firstHeader = c("TOP", "OF FIRST", "PAGE"),
  firstFooter = c("BOTTOM", "OF FIRST", "PAGE")
)

wb$add_worksheet("Sheet 6",
  header = c("&[Date]", "ALL HEAD CENTER 2", "&[Page] / &[Pages]"),
  footer = c("&[Path]&[File]", NA, "&[Tab]"),
  firstHeader = c(NA, "Center Header of First Page", NA),
  firstFooter = c(NA, "Center Footer of First Page", NA)
)

wb$add_worksheet("Sheet 7",
  header = c("ALL HEAD LEFT 2", "ALL HEAD CENTER 2", "ALL HEAD RIGHT 2"),
  footer = c("ALL FOOT RIGHT 2", "ALL FOOT CENTER 2", "ALL FOOT RIGHT 2")
)

wb$add_worksheet("Sheet 8",
  firstHeader = c("FIRST ONLY L", NA, "FIRST ONLY R"),
  firstFooter = c("FIRST ONLY L", NA, "FIRST ONLY R")
)

## Need data on worksheet to see all headers and footers
wb$add_data(sheet = 5, 1:400)
wb$add_data(sheet = 6, 1:400)
wb$add_data(sheet = 7, 1:400)
wb$add_data(sheet = 8, 1:400)

Run the code above in your browser using DataLab