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

add ability to protect sheets and lock/unlock cells #91

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
10 changes: 10 additions & 0 deletions example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ sheet6 =
# nest further
|> Sheet.group_cols("C", "D")

# Sheets can be protected. Cells are locked by default but can be set to be unlocked
# Here, A1 is editable while the sheet is protected, but B1 is not
sheet7 =
%Sheet{
name: "Protected",
protected: true
}
|> Sheet.set_cell("A1", "Can be edited", locked: false)
|> Sheet.set_cell("B1", "Cannot be edited")

Workbook.append_sheet(workbook, sheet4)
|> Workbook.append_sheet(sheet5)
|> Workbook.append_sheet(sheet6)
Expand Down
6 changes: 4 additions & 2 deletions lib/elixlsx/sheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ defmodule Elixlsx.Sheet do
group_rows: [],
merge_cells: [],
pane_freeze: nil,
show_grid_lines: true
show_grid_lines: true,
protected: false

@type t :: %Sheet{
name: String.t(),
Expand All @@ -36,7 +37,8 @@ defmodule Elixlsx.Sheet do
group_rows: list(rowcol_group),
merge_cells: [{String.t(), String.t()}],
pane_freeze: {number, number} | nil,
show_grid_lines: boolean()
show_grid_lines: boolean(),
protected: boolean()
}
@type rowcol_group :: Range.t() | {Range.t(), opts :: keyword}

Expand Down
9 changes: 6 additions & 3 deletions lib/elixlsx/style/cell_style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ defmodule Elixlsx.Style.CellStyle do
alias Elixlsx.Style.Font
alias Elixlsx.Style.Fill
alias Elixlsx.Style.BorderStyle
alias Elixlsx.Style.Protection

defstruct font: nil, fill: nil, numfmt: nil, border: nil
defstruct font: nil, fill: nil, numfmt: nil, border: nil, protection: nil

@type t :: %CellStyle{
font: Font.t(),
fill: Fill.t(),
numfmt: NumFmt.t(),
border: BorderStyle.t()
border: BorderStyle.t(),
protection: Protection.t()
}

def from_props(props) do
font = Font.from_props(props)
fill = Fill.from_props(props)
numfmt = NumFmt.from_props(props)
border = BorderStyle.from_props(props[:border])
protection = Protection.from_props(props)

%CellStyle{font: font, fill: fill, numfmt: numfmt, border: border}
%CellStyle{font: font, fill: fill, numfmt: numfmt, border: border, protection: protection}
end

def is_date?(cellstyle) do
Expand Down
25 changes: 25 additions & 0 deletions lib/elixlsx/style/protection.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Elixlsx.Style.Protection do
@moduledoc ~S"""
Protection properties.

Supported formatting properties are:

- locked: boolean
"""
alias __MODULE__

defstruct locked: nil

@type t :: %Protection{
locked: boolean
}

@doc ~S"""
Create a Protection object from a property list.
"""
def from_props(props) do
ft = %Protection{locked: props[:locked]}

if ft == %Protection{}, do: nil, else: ft
end
end
27 changes: 25 additions & 2 deletions lib/elixlsx/xml_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Elixlsx.XMLTemplates do
alias Elixlsx.Style.Font
alias Elixlsx.Style.Fill
alias Elixlsx.Style.BorderStyle
alias Elixlsx.Style.Protection
alias Elixlsx.Sheet

# TODO: the xml_text_exape functions belong into Elixlsx.Util,
Expand Down Expand Up @@ -291,6 +292,12 @@ defmodule Elixlsx.XMLTemplates do
"""
end

defp xl_sheet_protection(%Sheet{protected: true}) do
"<sheetProtection sheet=\"1\" objects=\"1\" scenarios=\"1\"/>"
end

defp xl_sheet_protection(_), do: ""

defp xl_sheet_rows(data, row_heights, grouping_info, wci) do
rows =
Enum.zip(data, 1..length(data))
Expand Down Expand Up @@ -459,6 +466,7 @@ defmodule Elixlsx.XMLTemplates do
</sheetData>
""" <>
xl_merge_cells(sheet.merge_cells) <>
xl_sheet_protection(sheet) <>
"""
<pageMargins left="0.75" right="0.75" top="1" bottom="1.0" header="0.5" footer="0.5"/>
</worksheet>
Expand Down Expand Up @@ -583,15 +591,25 @@ defmodule Elixlsx.XMLTemplates do
alignment ->
{"applyAlignment=\"1\"", alignment}
end
end
end

{apply_protection, protection_tag} =
case style.protection do
nil ->
{"", ""}

protection ->
{"applyProtection=\"1\"", "<protection #{protection_attrs(protection)}/>"}
end

"""
<xf borderId="#{borderid}"
fillId="#{fillid}"
fontId="#{fontid}"
numFmtId="#{numfmtid}"
xfId="0" #{apply_alignment}>
xfId="0" #{apply_alignment} #{apply_protection}>
#{wrap_text_tag}
#{protection_tag}
</xf>
"""
end
Expand Down Expand Up @@ -646,6 +664,11 @@ defmodule Elixlsx.XMLTemplates do
end
end

@spec protection_attrs(Protection.t()) :: String.t()
defp protection_attrs(%Protection{locked: true}), do: "locked=\"1\" "
defp protection_attrs(%Protection{locked: false}), do: "locked=\"0\" "
defp protection_attrs(_), do: ""

# Returns the inner content of the <CellXfs> block.
@spec make_cellxfs(list(CellStyle.t()), WorkbookCompInfo.t()) :: String.t()
defp make_cellxfs(ordered_style_list, wci) do
Expand Down