引用另一个工作表中的单元格

时间:2019-05-23 07:38:51

标签: r openxlsx

有没有办法引用包openxlsx中另一个工作表中的单元格-插入Excel公式?我在包装文件中找不到它。尝试这样做:

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

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

1 个答案:

答案 0 :(得分: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)