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

Elixlsx Fork - Patch issue with nil/empty cells unable to be styled #1

Merged
merged 3 commits into from
Jul 4, 2024
Merged
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Elixlsx

> ### Rum&Code
> ℹ️ This repo is a fork of [xou/elixlsx](https://github.com/xou/elixlsx/tree/master) to fix some of the shortcomings with the original library.
> #### Changelog & differences
> - Fix issue where passing `nil` to a cell's content ignores that cell's styling & formatting props
> - Add missing pattern in Elixlsx.Util.to_excel_datetime/1 to support empty cells

[![Build Status](https://travis-ci.com/xou/elixlsx.svg?branch=master)](https://travis-ci.org/xou/elixlsx)
[![Module Version](https://img.shields.io/hexpm/v/elixlsx.svg)](https://hex.pm/packages/elixlsx)
[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/elixlsx/)
Expand Down
3 changes: 3 additions & 0 deletions lib/elixlsx/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ defmodule Elixlsx.Util do
def to_excel_datetime({:formula, value}) do
{:formula, value}
end

@spec to_excel_datetime(any()) :: any()
def to_excel_datetime(value), do: value

@doc ~S"""
Replace_all(input, [{search, replace}]).
Expand Down
127 changes: 65 additions & 62 deletions lib/elixlsx/xml_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -221,68 +221,71 @@ defmodule Elixlsx.XMLTemplates do
|> List.foldl({"", 1}, fn cell, {acc, colidx} ->
{content, styleID, cellstyle} = split_into_content_style(cell, wci)

if is_nil(content) do
{acc, colidx + 1}
else
content =
if CellStyle.is_date?(cellstyle) do
U.to_excel_datetime(content)
else
content
end

cv = get_content_type_value(content, wci)

{content_type, content_value, content_opts} =
case cv do
{t, v} ->
{t, v, []}

{t, v, opts} ->
{t, v, opts}

:error ->
raise %ArgumentError{
message:
"Invalid column content at " <>
U.to_excel_coords(rowidx, colidx) <> ": " <> inspect(content)
}
end

cell_xml =
case content_type do
:formula ->
value =
if not is_nil(content_opts[:value]),
do: "<v>#{content_opts[:value]}</v>",
else: ""

"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}">
<f>#{content_value}</f>
#{value}
</c>
"""

:empty ->
"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}">
</c>
"""

type ->
"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}" t="#{type}">
<v>#{content_value}</v>
</c>
"""
end

{acc <> cell_xml, colidx + 1}
end
content =
if is_nil(content) do
""
else
content
end

content =
if CellStyle.is_date?(cellstyle) do
U.to_excel_datetime(content)
else
content
end

cv = get_content_type_value(content, wci)

{content_type, content_value, content_opts} =
case cv do
{t, v} ->
{t, v, []}

{t, v, opts} ->
{t, v, opts}

:error ->
raise %ArgumentError{
message:
"Invalid column content at " <>
U.to_excel_coords(rowidx, colidx) <> ": " <> inspect(content)
}
end

cell_xml =
case content_type do
:formula ->
value =
if not is_nil(content_opts[:value]),
do: "<v>#{content_opts[:value]}</v>",
else: ""

"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}">
<f>#{content_value}</f>
#{value}
</c>
"""

:empty ->
"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}">
</c>
"""

type ->
"""
<c r="#{U.to_excel_coords(rowidx, colidx)}"
s="#{styleID}" t="#{type}">
<v>#{content_value}</v>
</c>
"""
end

{acc <> cell_xml, colidx + 1}
end)

updated_row
Expand Down