Skip to contents

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.

Examples

# 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
#> Error : Merge intersects with existing merged cells: 
#> 		A1:J1.
#> Remove existing merge first.

# 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