Learn R Programming

openxlsx (version 2.2.1)

setColWidths: Set worksheet column widths

Description

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

Usage

setColWidths(wb, sheet, cols, widths)

Arguments

wb
A workbook object
sheet
A name or index of a worksheet
cols
Indices of cols to set width
widths
widths to set rows to specified in Excel column width units or "auto" for automatic sizing. The widths argument is recycled to the length of cols.

Details

The global min and max column width for "auto" columns is set by (default values show):
  • options("openxlsx.minWidth" = 3)
options("openxlsx.maxWidth" = 250) ## This is the maximum width allowed in Excel

See Also

removeColWidths

Examples

Run this code
## Create a new workbook
wb <- createWorkbook()

## Add a worksheet
addWorksheet(wb, "Sheet 1")


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

## auto columns
addWorksheet(wb, "Sheet 2")
writeData(wb, sheet = 2, x = iris)
setColWidths(wb, sheet = 2, cols = 1:5, widths = "auto")

## Save workbook
saveWorkbook(wb, "setColWidthsExample.xlsx", overwrite = TRUE)

Run the code above in your browser using DataLab