Learn R Programming

openxlsx2 (version 0.7)

wb_set_col_widths: Set worksheet column widths

Description

Set worksheet column widths to specific width or "auto".

Usage

wb_set_col_widths(
  wb,
  sheet = current_sheet(),
  cols,
  widths = 8.43,
  hidden = FALSE
)

Arguments

wb

A wbWorkbook object

sheet

A name or index of a worksheet

cols

Indices of cols to set width

widths

width to set cols to specified in Excel column width units or "auto" for automatic sizing. The widths argument is recycled to the length of cols. The default width is 8.43. Though there is no specific default width for Excel, it depends on Excel version, operating system and DPI settings used. Setting it to specific value also is no guarantee that the output will be of the selected width.

hidden

Logical vector. If TRUE the column is hidden.

Details

The global min and max column width for "auto" columns is set by (default values show):

  • options("openxlsx2.minWidth" = 3)

  • options("openxlsx2.maxWidth" = 250) ## This is the maximum width allowed in Excel

NOTE: The calculation of column widths can be slow for large worksheets.

NOTE: The hidden parameter may conflict with the one set in wb_group_cols; changing one will update the other.

See Also

wb_remove_col_widths()

Other workbook wrappers: wb_add_chartsheet(), wb_add_data_table(), wb_add_data(), wb_add_formula(), wb_add_pivot_table(), wb_add_worksheet(), wb_clone_worksheet(), wb_copy_cells(), wb_creators, wb_freeze_pane(), wb_get_base_font(), wb_save(), 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()

## Add a worksheet
wb$add_worksheet("Sheet 1")

## set col widths
wb$set_col_widths(1, cols = c(1, 4, 6, 7, 9), widths = c(16, 15, 12, 18, 33))

## auto columns
wb$add_worksheet("Sheet 2")
wb$add_data(sheet = 2, x = iris)
wb$set_col_widths(sheet = 2, cols = 1:5, widths = "auto")

Run the code above in your browser using DataLab