'openxlsx: How to copy a workbook worksheet (or its formatted data) into a different worksheet?

I want to load a workbook/sheet from an excel file that has complex formatting that can't be applied using openxlsx (ie cells where some of the text is italic and some is not) and then append the formatted data from that worksheet at the bottom of an existing worksheet.

I'm aware that cloneWorksheet() will copy the ENTIRE sheet, but i specifically want to add data from one worksheet into another existing worksheet.

wb <- createWorkbook()
sheet <- addWorksheet(wb = wb, sheetName = "sheet")

writeData(wb, sheet, mydata) #assume mydata is a data frame

wb2 <- loadWorkbook("myfile.xlsx")

writeData(wb = wb, sheet = sheet, x = wb2, startRow = ncol(mydata) + 1) #I know this doesn't work

For that last line, I want to take the formatted data from the worksheet in wb2 and add it after the existing data in the sheet of wb.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source