首页 > 解决方案 > 为什么在 R xlsx 中为 xlsm 格式化不起作用?

问题描述

所以我试图加载数据并将其写入 xlsm,但格式似乎不起作用。它正在写入没有边框的行(即使模板文件具有格式),我尝试锁定文件以防止编辑。尽管如此,它不会写入格式。

library(xlsx)
data = read.xlsx("my_data.xlsm", startRow=8)
pool_IDs = unique(data$Pay.pool.id)
for (i in pool_IDs) {
  temp = subset(data, Pay.pool.id==i)
  template = loadWorkBook('Template.xlsm')
  sheets = getSheets(template)
  sheet = sheets[[1]]
  cs1 = CellStyle(template) + Font(template)
  cs2 = CellStyle(template) + Font(template)
  cs3 = CellStyle(template) + Font(template) + Border()
  addDataFrame(temp, sheet, col.names=FALSE, row.names=FALSE, startRow=9,
               startColumn=1, colnamesStyle=cs2, rownamesStyle=cs1,
               colStyle=list('2'=cs2, '3'=cs2))
  print(paste("Processed pay pool", i)) 
  saveWorkbook(template, paste(i, ".xlsm", sep=''))
}

有人见过这个吗?

标签: rxlsxxlsm

解决方案


因此,默认情况下,cellStyles 不会从工作簿继承。您必须明确定义格式:

cs2 = CellStyle(template) + Font(template, color="blue")


推荐阅读