Excel And Ruby: Formatting One Cell With Multiple Fonts
Join the DZone community and get the full member experience.Join For Free
From the Ruby on Windows blog. Given a worksheet (ws) where each cell contains text as "last name, first name", format last names in bold, while first names remain in non-bold font.
ws.Range('A1:A3').Cells.each do |cell| # find the index of the first comma: comma = cell.Value.to_s.index(',') # get the range of characters before the comma lastname = cell.Characters(1, comma) # get the range of characters after the comma firstname = cell.Characters(comma + 2, cell.Value.to_s.size) # apply formatting: lastname.Font.Bold = true firstname.Font.Bold = false endFurther details and discussion here.
Opinions expressed by DZone contributors are their own.