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

Reference to cell in another worksheet

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

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

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

我们需要使用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)