Learn R Programming

openxlsx2 (version 0.3.1)

ws_cell_merge: Worksheet cell merging

Description

Merge cells within a worksheet

Usage

wb_merge_cells(wb, sheet = current_sheet(), rows = NULL, cols = NULL)

wb_unmerge_cells(wb, sheet = current_sheet(), rows = NULL, cols = NULL)

Arguments

wb

A workbook object

sheet

A name or index of a worksheet

cols, rows

Column and row specifications to merge on. Note: min() and max() of each vector are provided for specs. Skipping rows or columns is not recognized.

Details

As merged region must be rectangular, only min and max of cols and rows are used.

See Also

Other workbook wrappers: wb_add_data_table(), wb_add_data(), wb_add_formula(), wb_add_worksheet(), wb_clone_worksheet(), 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

Examples

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

# Add a worksheets
wb$add_worksheet("Sheet 1")
wb$add_worksheet("Sheet 2")

# Merge cells: Row 2 column C to F (3:6)
wb <- wb_merge_cells(wb, "Sheet 1", cols = 2, rows = 3:6)

# Merge cells:Rows 10 to 20 columns A to J (1:10)
wb <- wb_merge_cells(wb, 1, cols = 1:10, rows = 10:20)

# Intersecting merges
wb <- wb_merge_cells(wb, 2, cols = 1:10, rows = 1)
wb <- wb_merge_cells(wb, 2, cols = 5:10, rows = 2)
wb <- wb_merge_cells(wb, 2, cols = c(1, 10), rows = 12) # equivalent to 1:10
try(wb_merge_cells(wb, 2, cols = 1, rows = c(1,10)))    # intersects existing merge

# remove merged cells
wb <- wb_unmerge_cells(wb, 2, cols = 1, rows = 1)  # removes any intersecting merges
wb <- wb_merge_cells(wb, 2, cols = 1, rows = 1:10) # Now this works

Run the code above in your browser using DataLab