Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 3028 convert csv to excel #5209

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: rubocop
  • Loading branch information
xeniabarreto committed Sep 15, 2023
commit 7334fd4872f900612a2ff521434be89abf32a0f2
6 changes: 3 additions & 3 deletions app/controllers/case_contact_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def index
filename: "case-contacts-report-#{Time.zone.now.to_i}.csv"
end

format.xlsx do
format.xlsx do
send_data case_contact_report.to_excel,
filename: "case-contacts-report-#{Time.zone.now.to_i}.xlsx"
end
filename: "case-contacts-report-#{Time.zone.now.to_i}.xlsx"
end
end
end

Expand Down
15 changes: 8 additions & 7 deletions app/services/case_contacts_export_excel_service.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# frozen_string_literal: true

# CaseContactsExportExcelService handles the conversion of case contact data to Excel format.
# It provides methods for exporting case contact information to Excel files.
class CaseContactsExportExcelService
attr_reader :case_contacts, :filtered_columns

FONT_SIZE = 11
LINE_PADDING = 10

Expand All @@ -15,18 +18,16 @@ def perform
Axlsx::Package.new do |p|
p.workbook.add_worksheet(name: 'Case Contacts') do |sheet|
sheet.add_row(filtered_columns.map(&:to_s).map(&:titleize))

if case_contacts.present?
wrap_style = p.workbook.styles.add_style(alignment: { wrap_text: true, vertical: :center })

case_contacts.decorate.each do |case_contact|
format_row(sheet, case_contact, wrap_style)
end
configure_case_contact_notes_width(sheet)
sheet.rows.each_with_index do |row, index|
if index > 0
infer_row_height(row)
end
infer_row_height(row) if index.positive?
end
end
end
Expand All @@ -51,13 +52,13 @@ def infer_row_height(row)
physical_lines = row.each_with_index.map do |cell, column_index|
text = cell.value
column_width = row.worksheet.column_info[column_index].width

text_lines = text.to_s.lines
text_lines.map { |line| (string_width(line, row, FONT_SIZE) / column_width.to_f).ceil }.sum
end.max
row.height = (physical_lines * FONT_SIZE) + LINE_PADDING
end

def string_width(string, row, font_size)
font_scale = font_size / row.worksheet.workbook.font_scale_divisor
(string.to_s.size + 3) * font_scale
Expand Down