使用RDCOMClient编辑excel属性

时间:2018-05-03 11:30:17

标签: r excel rdcomclient

我无法找到RDComClient包的任何文档。我无法找到合适的参数来更改单元格的背景填充。

library(RDCOMClient)
xlApp <- COMCreate("Excel.Application")
book = xlApp$Workbooks()$Open("C:/Users/koyeli.majumder/Desktop/Dashboard/National Level Dashboard.xlsb")

sheet = book$Worksheets()$Item(1) 
xlApp[["Visible"]] = TRUE

cell  <- sheet$Cells(6,6)
cell[["Value"]] <- 3.1
cell[["NumberFormat"]]  = "[Red]"  # till this it was fine

cell[["Style"]] = 1

这会产生错误

<checkErrorInfo> 8002000E 
Error: Invalid number of parameters.

1 个答案:

答案 0 :(得分:1)

RDCOMClient是一个通用的COM接口。它不是特定于Excel。您将需要参考通常引用Basic或javascript的常规Excel文档并将其转换为R。

内部颜色设置如下。颜色索引号可在此处找到:https://msdn.microsoft.com/en-us/library/cc296089(v=office.12).aspx

library(RDCOMClient)

xl <- COMCreate("Excel.Application")
xl[["Visible"]] <- TRUE
wkbk <- xl$Workbooks()$Add()
sheet <- xl$ActiveSheet()

x12 <- sheet$Cells(1,2)
x12[["Value"]] <- 123
x12[["Interior"]][["ColorIndex"]] <- 3  # Red

screenshot