Formatting an entire excel workbook efficiently using the R xlsx package

∥☆過路亽.° 提交于 2019-12-01 02:15:28

As mentioned, consider the RDCOMClient package to interface to the Excel object library with access to most of its methods and properties. Here, you can format cells all at once similar to selecting a region and using the Excel.exe GUI of Ribbon to format cells.

Below is a loop version iterating across all worksheets to modify formats accordingly. I show its counterpart in Excel VBA.

VBA Code (native interface, so no assignment of Excel.Application or constants like xlCenter)

Sub FormatCells()
    Dim i As Integer
    Dim rng As Range

    For i = 1 To ThisWorkbook.Worksheets.Count
        Set rng = ThisWorkbook.Worksheets(i).Range(ThisWorkbook.Worksheets(i).Cells.Address)

        rng.NumberFormat = "#,##0"
        rng.Font.Name = "Calibri"
        rng.HorizontalAlignment = xlCenter
    Next i

    ThisWorkbook.Close True
    Application.Quit

End Sub

R Code (foreign interface, so assignment of all objects needed)

library(RDCOMClient)

xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open("D:\\Freelance Work\\Scripts\\FormatXLCells.xlsx")

xlCenter <- -4108

for (i in 1:xlWbk$Worksheets()$Count()){
  xlWks <- xlWbk$Worksheets(i)

  rng <- xlWks$Range(xlWks$Cells()$Address())
  rng[['NumberFormat']] <- "#,##0"
  rng[['Font']][['Name']] <- "Calibri"
  rng[['Font']][['Color']] <- 1
  rng[['HorizontalAlignment']] <- xlCenter

}

xlWbk$Close(TRUE)                    # SAVE AND CLOSE WORKBOOK
xlApp$Quit()                         # CLOSE COM APP 

# FREE RESOURCES
xlWks <- xlWbk <- xlApp <- NULL
rm(rng, xlWks, xlWbk, xlApp)
gc()                                 # NEEDED TO EFFECTIVELY END EXCEL PROCESS
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!