3

I am using axlsx gem to create excel sheets. I am trying to generate a report in which there is a heading. I would like to add comments to each cell that contains heading text. I can do it normally in Libre office calc by right clicking on the cell and selecting Insert Comment. I would like to do the same while trying to generate an excel sheet using a ruby program.

My code looks as shown below:

wb = xlsx_package.workbook
style_shout = wb.styles.add_style sz: 12, b: true, alignment: { horizontal: :center }

choices = ["Title", "First Name","Last Name", "Company","ID Number", "Email ID"]

# Build the Excel
wb.add_worksheet(name: "users_list") do |sheet|
  sheet.add_row choices.flatten, :style => style_shout
end

The documentation of axlsx shows a add_comment method, that is used to add a comment to a sheet. I would like to add comment to a particular cell. Can someone help me out with this?

Sri Murthy Upadhyayula
  • 22,620
  • 1
  • 18
  • 20

1 Answers1

8

You can do the following, and specify the cell reference you want the comment added to. In the example below, it is set to 'A1', you will need to set it to the cell that you want the comment to render in. (e.g. 'B7')

sheet.add_comment :ref => 'A1', :author => 'Bob', :text => 'Yes We Can!'

Best

randym

randym
  • 2,430
  • 1
  • 19
  • 18
  • Thanks for the reply randym...It worked great...But when I open the generated excel sheet, all the comments are open and are showing up. Ideally, when you add a comment for a cell in excel sheet, you get to see the comment only when you place the cursor on it. Is there a way in which we can add comments such that they show up only when you place your cursor on it? Thanks in advance. – Sri Murthy Upadhyayula Feb 12 '13 at 09:36
  • Okey...I see the issue here. From the discussion at: https://github.com/randym/axlsx/issues/170 , I understood that Libre Office has a problem with its filter. The excel sheet opens properly with MS Excel and OpenOffice. – Sri Murthy Upadhyayula Feb 14 '13 at 09:48