How to export an Excel sheet range to a picture, from within R

前端 未结 3 712
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 13:24

We are trying to automate the creation of some picture files within an R Script.

We have the Excel files looking the way that we want

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 14:10

    Consider having R do exactly as VBA does in your macro: making a COM interface to the Excel object library. You can do so with the RDCOMClient package, retaining nearly same code as macro in the R syntax.

    library(RDCOMClient)
    
    xlApp <- COMCreate("Excel.Application")
    xlWbk <- xlApp$Workbooks()$Open("C:\\Path\\To\\test_import.xlsx")
    xlScreen = 1
    xlBitmap = 2
    
    xlWbk$Worksheets("deletedFields")$Range("A8:J36")$CopyPicture(xlScreen, xlBitmap)
    
    xlApp[['DisplayAlerts']] <- FALSE
    
    oCht <- xlApp[['Charts']]$Add()
    oCht$Paste()
    oCht$Export("C:\\Temp\\SavedRange.jpg", "JPG")
    oCht$Delete()
    
    # CLOSE WORKBOOK AND APP
    xlWbk$Close(FALSE)
    xlApp$Quit()
    
    # RELEASE RESOURCES
    oCht <- xlWbk <- xlApp <- NULL    
    rm(oCht, xlWbk, xlApp)
    gc()
    

    Output (random data/chart)

提交回复
热议问题