
Create copies of a worksheet within a workbook
Source:R/class-workbook-wrappers.R
wb_clone_worksheet.RdCreate a copy of a worksheet in the same wbWorkbook object.
Data, styles, tables, comments, images and charts are cloned. Cloning sheets with pivot tables, slicers, timelines, named regions, chart extensions, form controls, embedded objects, threaded comments, query tables or rich values (e.g. pictures in cells) is experimental and throws a warning naming these elements. Check the output carefully.
Cloning is possible only to a limited extent. References to sheet names in formulas, charts, pivot tables, etc. may not be updated. Some elements like slicers and timelines cannot be cloned yet.
Cloning from another workbook is still an experimental feature and might not work reliably. Cloning data, media, charts, tables and conditional formatting should work. Slicers, timelines, pivot tables, custom table styles and cell metadata (e.g. dynamic arrays) are currently not implemented. Formula references are not updated to reflect interactions between workbooks.
Usage
wb_clone_worksheet(wb, old = current_sheet(), new = next_sheet(), from = NULL)See also
Other workbook wrappers:
base_font-wb,
col_widths-wb,
creators-wb,
grouping-wb,
row_heights-wb,
wb_add_chartsheet(),
wb_add_data(),
wb_add_data_table(),
wb_add_formula(),
wb_add_hyperlink(),
wb_add_pivot_table(),
wb_add_slicer(),
wb_add_worksheet(),
wb_base_colors,
wb_copy_cells(),
wb_freeze_pane(),
wb_merge_cells(),
wb_save(),
wb_set_last_modified_by(),
wb_workbook()
Examples
# Create a new workbook
wb <- wb_workbook()
# Add worksheets
wb$add_worksheet("Sheet 1")
wb$clone_worksheet("Sheet 1", new = "Sheet 2")
# Take advantage of waiver functions
wb$clone_worksheet(old = "Sheet 1")
## cloning from another workbook
# create a workbook
wb <- wb_workbook()$
add_worksheet("NOT_SUM")$
add_data(x = head(iris))$
add_fill(dims = "A1:B2", color = wb_color("yellow"))$
add_border(dims = "B2:C3")
# we will clone this styled chart into another workbook
fl <- system.file("extdata", "oxlsx2_sheet.xlsx", package = "openxlsx2")
wb_from <- wb_load(fl)
# clone styles and shared strings
wb$clone_worksheet(old = "SUM", new = "SUM", from = wb_from)