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

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

提交回复
热议问题