Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Clone Worksheet #255

Open
ksak3 opened this issue Feb 12, 2017 · 5 comments
Open

Feature Request: Clone Worksheet #255

ksak3 opened this issue Feb 12, 2017 · 5 comments

Comments

@ksak3
Copy link

ksak3 commented Feb 12, 2017

This would be awesome to have for copying of template sheets before writing. Thank you!

@debarros
Copy link
Contributor

debarros commented May 1, 2017

I was playing around with this, and hit an interesting error. I have a workbook that I read in
wb1 = loadWorkbook("scores10.xlsx")
I then extracted the sheet I want
x = wb1$worksheets[[8]]
I then tried to make a copy of it
y = x$copy()
That generated the error

Error in lapply(x, function(x) { : 
  argument "oddHeader" is missing, with no default

I am going to try writing something that (1) pulls the formatting info, (2) calls addWorksheet with that info, (3) pulls the data with read.xlsx, and (4) calls writeData on the new worksheet. I'm not sure that will get everything, but maybe it will be a start.

@debarros
Copy link
Contributor

debarros commented May 1, 2017

The following function did not work. I am guessing that I am missing some stuff. I tried loading the package sample workbook, running the function to copy the IrisSample tab, and saving the workbook. It generated an error when I tried to open it (something about removing the "Slicer"), and the copied tab had none of the formatting of the original tab.

copyWorksheet = function (wb, sheetName, newSheetName){
  wb$addWorksheet(newSheetName)
  n = which(wb$sheet_names == newSheetName)
  old = which(wb$sheet_names == sheetName)
  wb$worksheets[[n]]$autoFilter <- wb$worksheets[[old]]$autoFilter
  wb$worksheets[[n]]$colBreaks <- wb$worksheets[[old]]$colBreaks
  wb$worksheets[[n]]$cols <- wb$worksheets[[old]]$cols
  wb$worksheets[[n]]$conditionalFormatting <- wb$worksheets[[old]]$conditionalFormatting
  wb$worksheets[[n]]$dimension <- wb$worksheets[[old]]$dimension
  wb$worksheets[[n]]$drawing <- wb$worksheets[[old]]$drawing
  wb$worksheets[[n]]$extLst <- wb$worksheets[[old]]$extLst
  wb$worksheets[[n]]$freezePane <- wb$worksheets[[old]]$freezePane
  wb$worksheets[[n]]$legacyDrawing <- wb$worksheets[[old]]$legacyDrawing
  wb$worksheets[[n]]$legacyDrawingHF <- wb$worksheets[[old]]$legacyDrawingHF
  wb$worksheets[[n]]$oleObjects <- wb$worksheets[[old]]$oleObjects
  wb$worksheets[[n]]$pageMargins <- wb$worksheets[[old]]$pageMargins
  wb$worksheets[[n]]$pageSetup <- wb$worksheets[[old]]$pageSetup
  wb$worksheets[[n]]$rowBreaks <- wb$worksheets[[old]]$rowBreaks
  wb$worksheets[[n]]$sheetFormatPr <- wb$worksheets[[old]]$sheetFormatPr
  wb$worksheets[[n]]$sheetPr <- wb$worksheets[[old]]$sheetPr
  wb$worksheets[[n]]$sheetViews <- wb$worksheets[[old]]$sheetViews
  wb$worksheets[[n]]$tableParts <- wb$worksheets[[old]]$tableParts
  df = read.xlsx(wb, sheetName)
  writeData(wb = wb, x = df, sheet = newSheetName)
}
## load existing workbook from package folder
wb <- loadWorkbook(file = system.file("loadExample.xlsx", package= "openxlsx"))
names(wb)  #list worksheets
## Duplicate a worksheet
copyWorksheet(wb, "IrisSample","IrisSample2")
## Save workbook
saveWorkbook(wb, "loadExample.xlsx", overwrite = TRUE)

@dovrosenberg
Copy link

Are there plans to pull @kainhofer's work into the package? While not 100%, it does cover many use cases. Thanks!

@cryptobiotic
Copy link

@debarros Did you ever figure out how to get this to work?? I have tried copying a number of the underlying objects but when i go to save the wb i get 'attempt to select less than one element in get1index' I'm trying to combine workbooks with very unique formatting and i'm stuck.
"I am going to try writing something that (1) pulls the formatting info, (2) calls addWorksheet with that info, (3) pulls the data with read.xlsx, and (4) calls writeData on the new worksheet. I'm not sure that will get everything, but maybe it will be a start."

@debarros
Copy link
Contributor

@cryptobiotic No, I don't think I ever did get it to work. I'll have to go back and check the project I was working on at the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants