zeehio / condformat

R package to apply conditional formatting rules to a data.frame
Other
25 stars 6 forks source link

Export to excel with different number formats #25

Open kp-source opened 4 years ago

kp-source commented 4 years ago

Hi,

If I am exporting data frames to excel, can I show the currency and percentage symbols in the excel spreadsheet on certain columns ($, %)?

Cheers!

zeehio commented 4 years ago

I don't think I am going to provide that level of fine tuning for excel myself, since I feel it is at the limit of the scope of the package.

However, condformat uses openxlsx to export to Excel, and the following example code may be useful, if you combine it with function calls to openxlsx (please read the documentation for that package).

This is a minimal example:

library(condformat)
library(openxlsx)
data(iris)
x <- condformat(iris)
wb <- openxlsx::createWorkbook(creator = "")
addWorksheet(wb, sheetName = "sheet name")
condformat:::condformat2excelsheet(x, wb, "sheet name")
# Add here any other openxlsx function call to format the columns any way you want
saveWorkbook(wb, file = "iris.xlsx")

If this works for you, please tell me and I will document and export condformat2excelsheet in a future version, so you don't need ::: anymore.

kp-source commented 4 years ago

Hey,

I tried and the method did work. However, I was not able to simultaneously color a cell and apply a number style like a $ sign or a percent sign to the same cell.

Cheers!