awalker89 / openxlsx

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

how to apply selectively borders to an existing xlsx using openxlsx #390

Closed nmolanog closed 6 years ago

nmolanog commented 6 years ago

The objective is to apply borders to parts of a table using openxlsx. Next I will show the desired output, builded in a way that is not the desired one: printing parts of the table and formating it in each step:

library(openxlsx)
library(tidyverse)
set.seed(15)
###create workbook
wb <- createWorkbook()
addWorksheet(wb, "test2")
#Sample a portion of iris to make it short
iris%>%dplyr::sample_n(15)->sample_iris

#split according to Species
sample_iris%>%filter(Species %in% "setosa")->p1
sample_iris%>%filter(Species %in% "versicolor")->p2
sample_iris%>%filter(Species %in% "virginica")->p3

##write each part and apply borders
writeData(wb, 1,p1, startRow = 1, startCol = 1,borders = "surrounding",borderStyle="thick")
writeData(wb, 1,p2, startRow = 1+dim(p1)[1], startCol = 1,borders = "surrounding",colNames =F,borderStyle="thick")
writeData(wb, 1,p3, startRow = 1+dim(p1)[1]+dim(p2)[1], startCol = 1,borders = "surrounding",colNames =F,borderStyle="thick")
saveWorkbook(wb, "test2.xlsx", overwrite = TRUE)

So "test2.xlsx" is the desired output. The thing is how to achieve it not by generating the file, but to modify an existing file. I mean, if the file "test1.xlsx" is created by write.xlsx(sample_iris, file = "test1.xlsx")

then, how to apply borders as desired on the already existing file? Thanks for any guide on this

Note: I posted the same question on https://stackoverflow.com/questions/50595559/how-to-apply-selectively-borders-to-an-existing-xlsx-using-openxlsx

nmolanog commented 6 years ago

I have posted a solution to this in https://stackoverflow.com/questions/50595559/how-to-apply-selectively-borders-to-an-existing-xlsx-using-openxlsx/50635219#50635219

I would appreciate your comments on this.