通过 xlsx 更新 excel 并通过 libreoffice 打印 pdf。但是内容没有更新

Updating excel through xlsx and printing pdf through libreoffice. But content does not get updated

我正在使用 R 中的 xlsx 包更新 excel 模板,然后使用 R 中的系统命令将其写为 pdf。在系统命令中我使用的是 libreoffice-calc(类似于 excel).但是如果我打开它,我使用 R 所做的更新会在文件中更新。但 pdf 不打印它。

在这个例子中,chk.xlsx 是一个有两个标签的 excel sheet,第二个标签需要打印。

library(xlsx)
wb<-loadWorkbook("chk.xlsx")
sheets <- getSheets(wb)
sheet2 <- sheets[["Sheet1"]]
a=c(1:5)
b=c(6:10)
x <- as.data.frame(cbind(a,b))
addDataFrame(x, sheet2, startRow=2, startColumn=1,col.names=F, row.names=F)

wb$setForceFormulaRecalculation(TRUE)
#setForceFormulaRecalculation(wb,"data",TRUE)

saveWorkbook(wb, "chk.xlsx")
system(paste0("libreoffice --convert-to pdf chk.xlsx")) # empty pdf
#system(paste0("libreoffice --headless --convert-to pdf chk.xlsx")) # empty pdf
#system(paste0("libreoffice --headless --invisible --convert-to pdf chk.xlsx")) # empty pdf

即使在运行R 脚本运行后,我尝试从命令行(不是Rstudio/R 控制台的终端)运行,我也没有得到更新的sheet的印刷品。

 libreoffice --headless --convert-to pdf chk.xlsx

终于解决了

excel 中的公式不会在 LibreOffice 中自动更新。所以,你将不得不这样做。

工具 > 选项 > LibreOfficeCalc > 公式 > 文件加载时重新计算

将其设置为始终重新计算。 (虽然通过命令行设置它会很好)