2

I am writing some R code that reads in sheets from many Excel workbooks, identifies cells meeting certain criteria, and then saves these very same workbooks with the identified cells of interest in bold font. Because this is an efficiency tool, I do not want to have to go into every workbook that I plan to run my code on and add a custom "bold" cell style.

My plan was to read in the cell style from a template workbook:

bold <- XLConnect::getCellStyle(templateWB, "bold")

Then, I wanted to use the style from the template workbook to create bold cells in the new workbooks:

    XLConnect::setCellStyle(wb, sheet = sheetName, row= boldRow, col = boldCol, cellstyle = bold)

Trying to do this, however, results in an error:

"Error: IllegalArgumentException (Java): This Style does not belong to the supplied Workbook Stlyes Source. Are you trying to assign a style from one workbook to the cell of a differnt workbook?"

Clearly I cannot apply the cell style from one workbook to sheets in another workbook. Any suggestions for a workaround in R?

Thank you.

Andrew
  • 89
  • 5

1 Answers1

1

The cellstyle you imported comes from templateWB and therefore this cellstyle is only recognized by that workbook. You cannot apply this cellstyle to another workbook.

I had exactly the same problem and solved it by simply working in that workbook and saving it somewhere else with saveWorkbook(templateWB, "some other path"). Another option that should work is:

wb <- templateWB 
bold <- XLConnect::getCellStyle(templateWB, "bold")
XLConnect::setCellStyle(wb, sheet = sheetName, row= boldRow, col = boldCol, 
                        cellstyle = bold)
KenHBS
  • 6,756
  • 6
  • 37
  • 52