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

前端 未结 3 695
隐瞒了意图╮
隐瞒了意图╮ 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:06

    Hmm, not sure about posting, maybe it got redundant through Ian´s post. Its a bit more generic, but I can also remove it.

    library(xlsx)
    
    OutputPicFileName <- "Chart.jpg"
    ScriptFileName <- "Chart.vbs"
    xclFileName <- "test_import.xlsx"
    xclRng <- "A8:J36"
    file <- system.file("tests", xclFileName, package = "xlsx")
    fileDirec <- unlist(strsplit(file, xclFileName))
    
    CreateChart <- function(fileDirec, OutputPicFileName, ScriptFileName, xclRng){
      setwd(fileDirec)
      filePath <- file(paste0(fileDirec, ScriptFileName))
      writeLines(
        c(
          "Dim App, WBook, Sht, Rng, FileName, ChartObj, Chart",
           paste0("FileName = \"", gsub("/", "\\\\", fileDirec), xclFileName ,"\""),
           "Set App = CreateObject(\"Excel.Application\")",
           "Set WBook = App.WorkBooks.Open(FileName)",
           "Set Sht = App.Worksheets(1)",
           paste0("Set Rng = Sht.Range(\"", xclRng,"\")"),
           "Rng.CopyPicture",
           "Set ChartObj = App.Charts",
           "Set Chart = ChartObj.Add() ",
           "Chart.paste",
           paste0("Chart.Export \"", gsub("/", "\\\\", fileDirec) , OutputPicFileName ,"\", \"JPG\"")
        ), 
        filePath
      )
      close(filePath)
      shell.exec(ScriptFileName)
    }
    
    CreateChart(fileDirec, OutputPicFileName, ScriptFileName, xclRng)
    
    # Result in: fileDirec
    

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-12-31 14:12

    You can do this with vbs. Most vbs is identical to vba so you can write out your dynamic vbs script which includes your macro as text and then call it with shell.

    Here is a working example:

    fileConn<-file("c:/rworking/test/test.vbs")
    writeLines(c("Dim xlApp, xlBook, xlSht",
        "Dim filename",
        "filename = \"c:\\Rworking\\test\\test_import.xlsx\"",
        "Set xlApp = CreateObject(\"Excel.Application\")",
        "xlApp.Visible = True",
        "set xlBook = xlApp.WorkBooks.Open(filename)",
        "set xlSht = xlApp.Worksheets(1)",
        "set rng = xlSht.Range(\"A8:J36\")",
        "rng.CopyPicture",
        "Set oCht = xlApp.Charts",
        "oCht.Add() ",
        "Set oCht = oCht(1)",
        "oCht.paste",
        "oCht.Export \"C:\\rworking\\test\\Test.jpg\", \"JPG\""), 
        fileConn)
    
    close(fileConn)
    
    shell.exec("c:/rworking/test/test.vbs")
    
    0 讨论(0)
提交回复
热议问题