diff --git a/app/helpers/table_helper.rb b/app/helpers/table_helper.rb index e5a64911d..0842cf1af 100644 --- a/app/helpers/table_helper.rb +++ b/app/helpers/table_helper.rb @@ -4,7 +4,7 @@ module TableHelper def table_data content = {} data = JSON.parse(contents)['data'] - return [] if data.blank? + return {} if data.blank? cells = metadata.fetch('cells', []) diff --git a/app/services/reports/docx/table_helper.rb b/app/services/reports/docx/table_helper.rb index 75630dd4b..f5f472f3b 100644 --- a/app/services/reports/docx/table_helper.rb +++ b/app/services/reports/docx/table_helper.rb @@ -8,7 +8,8 @@ module Reports table_data = add_headers_to_table(table_data, table_type == 'well_plates_table') if table.metadata.present? && table.metadata['cells'].is_a?(Array) - table.metadata['cells'].each do |cell| + table.metadata&.dig('cells')&.each do |cell| + next unless cell.is_a?(Hash) next unless cell['row'].present? && cell['col'].present? row_index = cell['row'].to_i + 1 @@ -21,6 +22,9 @@ module Reports end end @docx.p + + return unless table_data.present? && table_data[0].present? + @docx.table table_data, border_size: Constants::REPORT_DOCX_TABLE_BORDER_SIZE do cell_style rows[0], bold: true, background: color[:concrete] cell_style cols[0], bold: true, background: color[:concrete] @@ -50,18 +54,16 @@ module Reports end def add_headers_to_table(table, is_well_plate) - table.each_with_index do |row, index| + table&.each_with_index do |row, index| row.unshift(is_well_plate ? convert_index_to_letter(index) : index + 1) end - header_row = Array.new(table.first.length) do |index| - if index.zero? - '' - else - is_well_plate ? index : convert_index_to_letter(index - 1) - end + header_row = Array.new(table&.dig(0)&.length || 0) do |index| + next '' if index.zero? + + is_well_plate ? index : convert_index_to_letter(index - 1) end - table.unshift(header_row) + table&.unshift(header_row) end def convert_index_to_letter(index) diff --git a/app/views/protocols/_table_element.html.erb b/app/views/protocols/_table_element.html.erb index 06ef6b4af..dae63562c 100644 --- a/app/views/protocols/_table_element.html.erb +++ b/app/views/protocols/_table_element.html.erb @@ -12,14 +12,14 @@ - <% content[:contents].first.each_with_index do |el, index| %> + <% content[:contents]&.first&.each_with_index do |el, index| %> <% end %> - <% content[:contents].each_with_index do |line, row_index| %> + <% content[:contents]&.each_with_index do |line, row_index| %> <% line.each_with_index do |el, column_index| %>
<%= content[:columns_title][index] %>
<%= content[:rows_title][row_index] %>