问题
I have a macro-enabled template in excel that I wish to populate programmatically from R.
I have an R process that retrieves different set of data for every item I with to process, processes them, and then I want it to duplicate and populate the template for each data set, bypassing the need to fill the template out by hand for every set of data.
I have been using the XLConnect R package, however it does not support .xlsm files. I thought about using RExcel, but I want to run the program from an R environment, not from within Excel (also I had many difficulties trying to install RExcel).
This is for a course curriculum database validation process that must be done each year, so filling the template out by hand for every single course in my department is simply not feasible and must be done in an automated manner. Part of the reason I want to use R is because I can build a nice user interface with Shiny, where excel workbooks just feel like death.
Is there another way to populate excel macro cells programmatically and actually get the macros to accept and run on the data, or do I need to learn VBA and write a program in excel to handle this?
回答1:
Something like the following:
library(xlsx)
xl <- loadWorkbook("Workbook1.xlsm")
sheets <- getSheets(xl)
cells <- getCells(getRows(sheets[[1]]))
setCellValue(cells[[1]], 400)
saveWorkbook(xl, "NewWorkbook.xlsm")
will preserve all the macros from Workbook1.xlsm
whilst populating actual cells with data.
来源:https://stackoverflow.com/questions/31493130/populating-excel-macro-enabled-cells-from-r-outside-environment