Skip to content

Commit

Permalink
Append BOM to ensure Excel can open CSV directly & correctly Encoding…
Browse files Browse the repository at this point in the history
…: UTF-8
  • Loading branch information
Eric-Guo authored and oliverguenther committed Sep 6, 2024
1 parent f1ae476 commit 93a0f13
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/models/exports/concerns/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def success(serialized)
::Exports::Result
.new format: :csv,
title: csv_export_filename,
content: serialized,
content: "\xEF\xBB\xBF#{serialized}", # Make Excel open CSV happy by append UTF8 BOM
mime_type: "text/csv"
end

Expand Down
6 changes: 3 additions & 3 deletions spec/models/projects/exporter/csv_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
it "does not render project custom fields in the header" do
expect(parsed.size).to eq 2

expect(header).to eq ["id", "Identifier", "Name", "Description", "Status", "Public"]
expect(header).to eq ["\xEF\xBB\xBFid", "Identifier", "Name", "Description", "Status", "Public"]
end

it "does not render the custom field values in the rows if enabled for a project" do
Expand All @@ -93,7 +93,7 @@
expect(cf_names).not_to include(not_used_string_cf.name)
expect(cf_names).not_to include(hidden_cf.name)

expect(header).to eq ["id", "Identifier", "Name", "Description", "Status", "Public", *cf_names]
expect(header).to eq ["\xEF\xBB\xBFid", "Identifier", "Name", "Description", "Status", "Public", *cf_names]
end

it "renders the custom field values in the rows if enabled for a project" do
Expand Down Expand Up @@ -126,7 +126,7 @@
expect(cf_names).to include(not_used_string_cf.name)
expect(cf_names).to include(hidden_cf.name)

expect(header).to eq ["id", "Identifier", "Name", "Description", "Status", "Public", *cf_names]
expect(header).to eq ["\xEF\xBB\xBFid", "Identifier", "Name", "Description", "Status", "Public", *cf_names]
end

it "renders the custom field values in the rows if enabled for a project" do
Expand Down
4 changes: 2 additions & 2 deletions spec/models/work_package/exporter/csv_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
data = CSV.parse instance.export!.content

expect(data.size).to eq(5)
expect(data.pluck(0)).to eq ["Type", "Type A", "Type A", "Type A", "Type B"]
expect(data.pluck(0)).to eq ["\xEF\xBB\xBFType", "Type A", "Type A", "Type A", "Type B"]
end
end

Expand Down Expand Up @@ -171,7 +171,7 @@
data = CSV.parse instance.export!.content

expect(data.size).to eq(5)
expect(data.pluck(0)).to eq %w[Subject WP4 WP2 WP1 WP3]
expect(data.pluck(0)).to eq %w[WP4 WP2 WP1 WP3].unshift("\xEF\xBB\xBFSubject")
end
end
end

0 comments on commit 93a0f13

Please sign in to comment.