diff --git a/example.exs b/example.exs index 15ee23f..a4bc1cf 100755 --- a/example.exs +++ b/example.exs @@ -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") diff --git a/lib/elixlsx/style/font.ex b/lib/elixlsx/style/font.ex index d69dcd6..17e9c8b 100644 --- a/lib/elixlsx/style/font.ex +++ b/lib/elixlsx/style/font.ex @@ -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__ @@ -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, @@ -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""" @@ -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 diff --git a/lib/elixlsx/xml_templates.ex b/lib/elixlsx/xml_templates.ex index e9ea6c0..0afc114 100644 --- a/lib/elixlsx/xml_templates.ex +++ b/lib/elixlsx/xml_templates.ex @@ -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 @@ -679,6 +707,7 @@ defmodule Elixlsx.XMLTemplates do |> wrap_text(font) |> horizontal_alignment(font) |> vertical_alignment(font) + |> text_rotation_alignment(font) case attrs do "" ->