Skip to content

Commit

Permalink
Merge pull request #1032 from thecartercenter/12624-fix-duplicate-col…
Browse files Browse the repository at this point in the history
…umn-header

12624: xlsform export: fix duplicate column header error in Central
  • Loading branch information
cooperka authored Jan 22, 2025
2 parents e723d96 + d2e31ce commit 14c3ef3
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app/models/forms/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ def to_xls
end

questions.row(0).push("name", "required", "relevant", "constraint", "choice_filter")
settings.row(0).push("form_title", "form_id", "version", "default_language")

group_depth = 1 # assume base level
repeat_depth = 1
Expand Down Expand Up @@ -155,10 +154,9 @@ def to_xls

# if we have an option set, identify and save it so that we can add it to the choices sheet later.
# then, write the question, splitting it into multiple questions if there are option set levels.
os_name = ""
choice_filter = ""
if q.option_set_id.present?
os = OptionSet.find(q.option_set_id)
os = q.option_set
option_sets_used.push(q.option_set_id)

# include leading space to respect XLSForm format
Expand All @@ -169,7 +167,7 @@ def to_xls
# is the option set multilevel?
if os.level_names.present?
os.level_names.each_with_index do |level, l_index|
level_name = vanillify(level.values[0])
level_name = unique_level_name(os_name, level.values[0])

# Append level name to qtype
type_to_push = "#{qtype_converted} #{level_name}"
Expand Down Expand Up @@ -261,13 +259,15 @@ def to_xls
end

## Settings
settings.row(0).push("form_title", "form_id", "version", "default_language", "allow_choice_duplicates")

lang = @form.mission.setting.preferred_locales[0].to_s
version = if @form.current_version.present?
@form.current_version.decorate.name
else
"1"
end
settings.row(1).push(@form.name, @form.id, version, lang)
settings.row(1).push(@form.name, @form.id, version, lang, "yes")

## Write
file = StringIO.new
Expand Down Expand Up @@ -381,7 +381,7 @@ def options_to_xls(option_sets, locales)
if node.level.present?
# per XLSform style, option sets with levels need to have the
# list_name replaced with the level name to distinguish each row.
listname_to_push = node.level_name
listname_to_push = unique_level_name(os.name, node.level_name)

# Only attempt to access node ancestors if they exist
if node.ancestry_depth > 1
Expand Down Expand Up @@ -418,7 +418,7 @@ def options_to_xls(option_sets, locales)
# omit last entry (lowest level)
if os.level_names.present?
os.level_names[0..-2].each do |level|
header_row.push(vanillify(level.values[0]))
header_row.push(unique_level_name(os.name, level.values[0]))

# increment column counter
column_counter += 1
Expand All @@ -433,6 +433,12 @@ def options_to_xls(option_sets, locales)
os_matrix.insert(0, header_row)
end

# prepend option set name so that level names are unique
# this avoids duplicate header errors
def unique_level_name(os_name, level_name)
"#{vanillify(os_name)}_#{vanillify(level_name)}"
end

# recursively remove pesky characters and replace spaces with underscores
# for XLSForm compatibility
def vanillify(input)
Expand Down

0 comments on commit 14c3ef3

Please sign in to comment.