1

有什么方法可以引用 openxlsx 包中另一个工作表中的单元格 - 插入 Excel 公式?我在包文档中没有找到它。试图这样做:

writeData(wb,
            "Sheet1",
            "='Sheet2'!B2",
            startCol = 1,
            startRow = 1)

但后来我不得不双击 Excel 文件中的单元格以显示引用单元格中的值...

4

1 回答 1

1

我们需要使用writeFormula,见例子:

library(openxlsx)

wb <- createWorkbook()

addWorksheet(wb = wb, sheetName = "Sheet1")
addWorksheet(wb = wb, sheetName = "Sheet2")

# add some data on Sheet2, so we can get some value using formula in Sheet1
writeData(wb = wb, sheet = "Sheet2", x = mtcars)

writeFormula(wb, sheet = "Sheet1", x = "=Sheet2!B2",
          startCol = 1, startRow = 1)

# test: view file without saving:
openXL(wb)
于 2019-05-23T08:23:29.287 回答