Populating Excel Macro-enabled cells from R/outside environment

☆樱花仙子☆ 提交于 2019-12-31 07:41:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!