我有一个带有我公司徽标和格式的默认工作簿,以便快速为我的客户生成工作簿,而不必一直重新格式化所有内容。
我设法为一张纸做。我想在一个工作簿中根据需要处理尽可能多的工作表。
我现在这样做,如下
wb = loadWorkbook("O:/R/handle_wb.xlsx") # loads default workbook
sheets = getSheets(wb) # get pre-formatted sheet
# change styles
cs = CellStyle(wb) + ...
# add df of data to excel with the chosen styles
addDataFrame(data, sheets$Sheet1, startRow = 6, startColumn = 1,
colnamesStyle = cs, row.names = F)
然后,我想在同一个 wb 中生成另一张工作表,但是使用我已经拥有的预格式化工作表,通过编写一个类似的函数
add.sheet <- funtion(newdata, original.wb, default.wb){
wb = loadWorkbook(default.wb) # loads default workbook
sheets = getSheets(wb) # get pre-formatted sheet
ob = loadWorkbook(original.wb) # loads orginal workbook
# change styles
cs = CellStyle(wb) + ...
# add df of data to excel with the chosen styles
addDataFrame(newdata, sheets$Sheet1, startRow = 6, startColumn = 1, colnamesStyle = cs, row.names = F)
# create a new sheet in the original workbook to receive the newsheet created above
createSheet(ob, "sheet2")
she <- getSheets(ob)
she$sheet2 <- sheets$Sheet1 # designate to sheet2 the created default sheet
saveWorkbook(ob, original.wb)
}
问题是,如何将我在原始工作簿中创建的空白表替换为默认工作簿中的空白表,即she$sheet2 <- sheets$Sheet1
?