【问题标题】:Applying formula to each column R openxlsx将公式应用于每列 R openxlsx
【发布时间】:2021-02-15 10:00:33
【问题描述】:

我有一个包含多个列的 Excel 工作簿,其中列名是日期。有没有办法将公式应用于 openxlsx 中的每一列。我需要一种方法来使用适当的 excel 公式动态填充每一列,因为列的数量很大。

这是表格 1。请注意:列名顶部的字母表示 excel 列名,每行开头的数字是 excel 行号。

      A          B           C          D
   Product   1/1/2020     1/1/2019   1/1/2018      
1     1          1          10          1          
2     2          2          20          1           
3     3          3          30          10            
4     4          2          10          5            
5     5          4           5          10      

这是第 2 页

      A         B          C            D           E
   Product    Skill     1/1/2020     1/1/2019   1/1/2018      
1     1         W2         1            1          10          
2     2         W45        20           1           0           
3     3         W0         40           5           0            
4     4         W1         50           5           1            
5     5         W2         2            2           1      

这是结果表。 C1单元格的excel公式“='Sheet1'!B1*'Sheet2'!C1”

单元格 D1 的 excel 公式 "='Sheet1'!C1*'Sheet2'!D1"

单元格 E1 的 excel 公式 "='Sheet1'!D1*'Sheet2'!E1"

       A         B          C            D          E
    Product    Skill     1/1/2020     1/1/2019   1/1/2018      
1      1         W2         1           10          10          
2      2         W45        40          20           0           
3      3         W0         120         150          0            
4      4         W1         100         50           5            
5      5         W2         8           10          10

由于我是 openxlsx 的新手,有没有办法使用 write_formula 函数来做到这一点。我想自动化这个过程,因为列的数量非常大。谢谢。

【问题讨论】:

    标签: r excel excel-formula openxlsx


    【解决方案1】:

    我试图给出一个解决方案。您可以检查是否可以接受。

    library(openxlsx)
    st1 <- read.xlsx("example.xlsx",colNames = FALSE,sheet = 1)
    st2 <- read.xlsx("example.xlsx",colNames = FALSE,sheet = 2)
    st3 = st2
    st3[3:5] = st1[2:4] * st2[3:5]
    # This means the multiply operation of two sheets
    # st4$X3 = st1$X2 * st2$X3
    # st4$X4 = st1$X3 * st2$X4
    # st4$X5 = st1$X4 * st2$X5
    

    【讨论】:

    • 感谢您的解决方案,但我正在寻找使用 openxlsx 的 write_formula 函数的方法。
    • 很好。但我还是想建议,如果没有更多的限制,我们最好关注如何解决问题,而不是必须使用哪个函数。
    【解决方案2】:

    我认为您想将 writeFormula() 包装在 for 循环中。

    library(openxlsx)
    library(glue) #I find this easier than paste(), ymmv
    wb <- your_workbook
    sheetname <- sheet_you_are_working_on
    
    col_start <- some_number
    col_end   <- another_number
    
    row_start <- third_number
    row_end   <- fourth_number
    
    for(c in col_start:col_end}
      writeFormula(wb, sheet_name, 
                   glue("='Sheet1'!{col}{row}*'Sheet2'!{col}{row}",
                        col = letters[c], #might need to make your own letters if more than 24 columns
                        row = row_start : row_end
                         )
                    )
    }
    #example with numbers
    for(c in 4:5}
      writeFormula(wb, sheet_name, 
                   glue("='Sheet1'!{col}{row}*'Sheet2'!{col}{row}",
                        col = letters[c], #"C" and "D" are within letters()
                        row = 1:5
                         )
                    )
    }
    

    如果您愿意,您可以通过第二个循环来定义您的工作表名称,从而在工作表中变得更加出色。

    【讨论】:

      猜你喜欢
      • 2012-07-30
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 2018-05-04
      • 2016-04-09
      • 2014-09-16
      相关资源
      最近更新 更多