Rails axlsx: How to insert value into new cell in existing row

Viewed 505

I have 2 table with one-to-many relationship, activities table and direct_activities table

In the axslx file, I have code like this

wb = xlsx_package.workbook
activities = Activity.all.includes(:direct_activities)
wb.add_worksheet(name: "Sheet 1") do |sheet|
  activities.each do |activity|
    sheet.add_row ["#{activity.name}"]
    activity.direct_activities.each do |direct_activity|
      sheet.add_row ["#{direct_activity.name}"]
    end
    sheet.add_row []
  end
end

The problem is: with this code, all the data inputted vertically, how can I make so that every each activity, the row get back to 1 and set the current cell to different cell.

Something like this

<table border="solid 1px black" border-collapse="collapse">
  <tr>
    <td>Activity A</td>
    <td>Activity B</td>
  </tr>
  <tr>
    <td>Direct Activity A</td>
    <td>Direct Activity D</td>
  </tr>
  <tr>
    <td>Direct Activity B</td>
    <td>Direct Activity E</td>
  </tr>
  <tr>
    <td>Direct Activity C</td>
  </tr>
</table>

Note: I'm very sorry for my bad english

1 Answers

Use add_cell

add_cell(value = '', options = {}) ⇒ Cell

This adds a single cell to the row based on the data provided and updates the worksheet's autofit data.

Related