Skip to content

Commit

Permalink
Add support for textRotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Antoine Reyt committed Oct 15, 2021
1 parent bb4cd57 commit a1f1f14
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
11 changes: 11 additions & 0 deletions example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,18 @@ sheet6 =
# nest further
|> Sheet.group_cols("C", "D")

# Text rotation.
sheet7 =
%Sheet{name: "Text rotation"}
|> Sheet.set_cell("A1", "Hello - angle_ccw", text_rotation: :angle_ccw)
|> Sheet.set_cell("B1", "Hello - angle_cw", text_rotation: :angle_cw)
|> Sheet.set_cell("C1", "Vertical - vertical", text_rotation: :vertical)
|> Sheet.set_cell("D1", "Hello - rotate_up", text_rotation: :rotate_up)
|> Sheet.set_cell("E1", "Hello - rotate_down", text_rotation: :rotate_down)
|> Sheet.set_cell("F1", "Hello - 20", text_rotation: 20)

Workbook.append_sheet(workbook, sheet4)
|> Workbook.append_sheet(sheet5)
|> Workbook.append_sheet(sheet6)
|> Workbook.append_sheet(sheet7)
|> Elixlsx.write_to("example.xlsx")
10 changes: 7 additions & 3 deletions lib/elixlsx/style/font.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ defmodule Elixlsx.Style.Font do
- align_horizontal: atom (:left, :right, :center, :justify, :general, :fill)
- align_vertical: atom (:top, :bottom, :center)
- font: String
- text_rotation: pos_integer : from 0 to 180 or 255 (default is 0) atom (:angle_ccw, :angle_cw, :vertical, :rotate_up, :rotate_down)
"""
import Elixlsx.Color, only: [to_rgb_color: 1]
alias __MODULE__
Expand All @@ -27,7 +28,8 @@ defmodule Elixlsx.Style.Font do
color: nil,
wrap_text: false,
align_horizontal: nil,
align_vertical: nil
align_vertical: nil,
text_rotation: nil

@type t :: %Font{
bold: boolean,
Expand All @@ -39,7 +41,8 @@ defmodule Elixlsx.Style.Font do
wrap_text: boolean,
align_horizontal: atom,
align_vertical: atom,
font: String.t()
font: String.t(),
text_rotation: pos_integer | atom
}

@doc ~S"""
Expand All @@ -56,7 +59,8 @@ defmodule Elixlsx.Style.Font do
wrap_text: !!props[:wrap_text],
align_horizontal: props[:align_horizontal],
align_vertical: props[:align_vertical],
font: props[:font]
font: props[:font],
text_rotation: props[:text_rotation]
}

if ft == %Font{}, do: nil, else: ft
Expand Down
29 changes: 29 additions & 0 deletions lib/elixlsx/xml_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,34 @@ defmodule Elixlsx.XMLTemplates do
end
end

@spec text_rotation_alignment(String.t(), Font.t()) :: String.t()
defp text_rotation_alignment(attrs, %Font{text_rotation: nil}), do: attrs

defp text_rotation_alignment(attrs, %Font{text_rotation: rotation}) do
with {:ok, angle} <- validate_text_rotation(rotation) do
attrs <> "textRotation=\"#{angle}\" "
else
_ ->
raise %ArgumentError{
message: """
Given text rotation not supported.
You can choose from: :angle_ccw, :angle_cw, :vertical, :rotate_up, :rotate_down
Or choose an angle from 0 to 180
"""
}
end
end

defp validate_text_rotation(:angle_ccw), do: {:ok, 45}
defp validate_text_rotation(:angle_cw), do: {:ok, 135}
defp validate_text_rotation(:vertical), do: {:ok, 255}
defp validate_text_rotation(:rotate_up), do: {:ok, 90}
defp validate_text_rotation(:rotate_down), do: {:ok, 180}
defp validate_text_rotation(255), do: {:ok, 255}
defp validate_text_rotation(angle) when angle >= 0 and angle <= 180, do: {:ok, angle}

# Creates an aligment xml tag from font style.
@spec make_style_alignment(Font.t()) :: String.t()
defp make_style_alignment(font) do
Expand All @@ -679,6 +707,7 @@ defmodule Elixlsx.XMLTemplates do
|> wrap_text(font)
|> horizontal_alignment(font)
|> vertical_alignment(font)
|> text_rotation_alignment(font)

case attrs do
"" ->
Expand Down

0 comments on commit a1f1f14

Please sign in to comment.