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

feat: allow hidden sheets #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ sheet6 =
# nest further
|> Sheet.group_cols("C", "D")

# Sheets can also be hidden.
sheet7 = Sheet.with_name("Hidden", state: :hidden)

Workbook.append_sheet(workbook, sheet4)
|> Workbook.append_sheet(sheet5)
|> Workbook.append_sheet(sheet6)
|> Workbook.append_sheet(sheet7)
|> Elixlsx.write_to("example.xlsx")
16 changes: 13 additions & 3 deletions lib/elixlsx/sheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Elixlsx.Sheet do
group_rows: [],
merge_cells: [],
pane_freeze: nil,
state: :visible,
show_grid_lines: true,
data_validations: []

Expand All @@ -40,6 +41,7 @@ defmodule Elixlsx.Sheet do
group_rows: list(rowcol_group),
merge_cells: [{String.t(), String.t()}],
pane_freeze: {number, number} | nil,
state: :visible | :hidden | :very_hidden,
show_grid_lines: boolean(),
data_validations: list({String.t(), String.t(), list(String.t()) | String.t()})
}
Expand All @@ -49,10 +51,18 @@ defmodule Elixlsx.Sheet do
Create a sheet with a sheet name.

The name can be up to 31 characters long.

## Options

* `state` - sets the default visibility of the sheet. Can be one of:
- `:visible` (default)
- `:hidden`
- `:very_hidden`
"""
@spec with_name(String.t()) :: Sheet.t()
def with_name(name) do
%Sheet{name: name}
@spec with_name(String.t(), Keyword.t()) :: Sheet.t()
def with_name(name, opts \\ []) do
state = Keyword.get(opts, :state, :visible)
%Sheet{name: name, state: state}
end

defp split_cell_content_props(cell) do
Expand Down
8 changes: 7 additions & 1 deletion lib/elixlsx/xml_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,14 @@ defmodule Elixlsx.XMLTemplates do
}
end

state = case sheet_info.state do
:hidden -> "hidden"
:very_hidden -> "veryHidden"
_ -> "visible"
end

"""
<sheet name="#{xml_escape(sheet_info.name)}" sheetId="#{sheet_comp_info.sheetId}" state="visible" r:id="#{
<sheet name="#{xml_escape(sheet_info.name)}" sheetId="#{sheet_comp_info.sheetId}" state="#{state}" r:id="#{
sheet_comp_info.rId
}"/>
"""
Expand Down