awalker89 / openxlsx

R package for .xlsx file reading and writing.
Other
364 stars 79 forks source link

Writing data without overwriting all cells? #408

Open md0u80c9 opened 6 years ago

md0u80c9 commented 6 years ago

Hi,

Is there a way to write data to a range without overwriting all the cells? I wondered if this would be possible with NULL, but doesn't appear to be. If not - would that be something which is possible? The main benefit would be that default data typing would be preserved, eg. the following example:

library(tibble)
library(openxlsx)
testDf <- tibble::tribble(~x, ~y, ~Z, 1, NULL, 3,NULL, 5, NULL, 7, NULL, 9)
testDf2 <- tibble::tribble(~x, ~y, ~z, NULL, "Two", NULL, "Four", NULL, "Six", NULL, "Eight", NULL)
wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(wb, "Demo")
openxlsx::writeData(wb, "Demo", testDf, startRow = 1, startCol = 1,
                     colNames = FALSE, rowNames = FALSE, borders = 'none', borderStyle = 'none')
openxlsx::writeData(wb, "Demo", testDf2, startRow = 1, startCol = 1,
                     colNames = FALSE, rowNames = FALSE, borders = 'none', borderStyle = 'none')
openxlsx::saveWorkbook(wb, "WBTest.xlsx")

The goal is to basically have alternating squares with numeric format data and string data, without resorting to a for loop over the individual cells.

kainhofer commented 6 years ago

I can only second that feature request. I'm reading out funds data from several databases and need to insert them into an Excel workbook where some columns have complex formulas of the preceding columns. Unfortunately, writeData always overwrites the whole region and thus also these formulas.

I agree that it might not be desirable as the default behavior, but it might be implemented as a skip=FALSE/TRUE argument to writeData?