Skip to content

Commit

Permalink
Merge pull request xou#131 from Climate-Recovery-Exchange/feature/dat…
Browse files Browse the repository at this point in the history
…a_validation_with_source_from_sheet

support data validation where source is within same sheet or reference to other sheet
  • Loading branch information
xou committed Jul 19, 2023
2 parents 147628a + 4d8afb3 commit a70d878
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ sheet1 = Sheet.with_name("First")
|> Sheet.set_cell("A2", "cat")
|> Sheet.set_cell("A3", "cow")
|> Sheet.add_data_validations("A1", "A10", ["dog", "cat", "cow"])
# within same sheet
|> Sheet.add_data_validations("A1", "A10", "=$A$2:$A$16")
# reference to other sheet "=#{sheet.name}!$A$2:$A$16"
|> Sheet.add_data_validations("A1", "A10", "=sheet2!$A$2:$A$16")

workbook = %Workbook{sheets: [sheet1]}

Expand Down
2 changes: 1 addition & 1 deletion lib/elixlsx/sheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Elixlsx.Sheet do
merge_cells: [{String.t(), String.t()}],
pane_freeze: {number, number} | nil,
show_grid_lines: boolean(),
data_validations: list({String.t(), String.t(), list(String.t())})
data_validations: list({String.t(), String.t(), list(String.t()) | String.t()})
}
@type rowcol_group :: Range.t() | {Range.t(), opts :: keyword}

Expand Down
10 changes: 10 additions & 0 deletions lib/elixlsx/xml_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@ defmodule Elixlsx.XMLTemplates do
"""
end

defp make_data_validation({start_cell, end_cell, values}) when is_bitstring(values) do
"""
<dataValidation type="list" allowBlank="1" showErrorMessage="1" sqref="#{start_cell}:#{
end_cell
}">
<formula1>#{values}</formula1>
</dataValidation>
"""
end

defp make_data_validation({start_cell, end_cell, values}) do
joined_values =
values
Expand Down

0 comments on commit a70d878

Please sign in to comment.