diff --git a/app/services/procedure_export_v2_service.rb b/app/services/procedure_export_v2_service.rb index 2a14615fa..f29a5b1d2 100644 --- a/app/services/procedure_export_v2_service.rb +++ b/app/services/procedure_export_v2_service.rb @@ -70,6 +70,10 @@ class ProcedureExportV2Service row_style: { background_color: nil, color: "000000", font_size: 12 } } + def sanitize_sheet_name(name) + ActiveStorage::Filename.new(name.to_s).sanitized.truncate(30) + end + def options_for(table) case table when :dossiers @@ -80,7 +84,7 @@ class ProcedureExportV2Service { instances: avis.to_a, sheet_name: 'Avis' }.merge(DEFAULT_STYLES) when Array # We have to truncate the label here as spreadsheets have a (30 char) limit on length. - { instances: table.last, sheet_name: table.first.to_s.truncate(30) }.merge(DEFAULT_STYLES) + { instances: table.last, sheet_name: sanitize_sheet_name(table.first) }.merge(DEFAULT_STYLES) end end end diff --git a/spec/services/procedure_export_v2_service_spec.rb b/spec/services/procedure_export_v2_service_spec.rb index d0dfa759a..79dd7aa0c 100644 --- a/spec/services/procedure_export_v2_service_spec.rb +++ b/spec/services/procedure_export_v2_service_spec.rb @@ -184,6 +184,16 @@ describe ProcedureExportV2Service do it 'should have data' do expect(repetition_sheet.data.size).to eq(4) end + + context 'with invalid characters' do + before do + champ_repetition.type_de_champ.update(libelle: 'A / B \ C') + end + + it 'should have valid sheet name' do + expect(subject.sheets.map(&:name)).to eq(['Dossiers', 'Etablissements', 'Avis', "A - B - C"]) + end + end end end end