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

Image support - continuation #124

Open
wants to merge 5 commits 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ elixlsx-*.tar

# Misc.
*.swp

# Elixir LS cache
/.elixir_ls/
13 changes: 13 additions & 0 deletions example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,20 @@ sheet6 =
# nest further
|> Sheet.group_cols("C", "D")

# Images
sheet7 = %Sheet{
name: "Images",
rows: List.duplicate(["A", "B", "C", "D", "E"], 5)
}

sheet7 =
sheet7
|> Sheet.insert_image(0, 5, "ladybug-3475779_640.jpg")
|> Sheet.set_row_height(1, 40)
|> Sheet.insert_image(6, 6, "ladybug-3475779_640.jpg")

Workbook.append_sheet(workbook, sheet4)
|> Workbook.append_sheet(sheet5)
|> Workbook.append_sheet(sheet6)
|> Workbook.append_sheet(sheet7)
|> Elixlsx.write_to("example.xlsx")
Binary file added ladybug-3475779_640.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 42 additions & 1 deletion lib/elixlsx/compiler.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule Elixlsx.Compiler do
alias Elixlsx.Compiler.WorkbookCompInfo
alias Elixlsx.Compiler.SheetCompInfo
alias Elixlsx.Compiler.DrawingCompInfo
alias Elixlsx.Compiler.CellStyleDB
alias Elixlsx.Compiler.DrawingDB
alias Elixlsx.Compiler.StringDB
alias Elixlsx.XML
alias Elixlsx.Sheet
Expand All @@ -25,6 +27,28 @@ defmodule Elixlsx.Compiler do
{Enum.reverse(sheetCompInfos), nextrID}
end

@doc ~S"""
Accepts a list of Sheets and the next free relationship ID.
Returns a tuple containing a list of DrawingCompInfo's based on the images
within the sheets and the next free relationship ID.
"""
@spec make_drawing_info(nonempty_list(Sheet.t()), non_neg_integer) ::
{list(DrawingCompInfo.t()), non_neg_integer}
def make_drawing_info(sheets, init_rId) do
# fold helper. aggregator holds {list(DrawingCompInfo), drawingidx, rId}.
add_sheet = fn sheet, {dci, idx, rId} ->
if sheet.images == [] do
{dci, idx, rId}
else
{[DrawingCompInfo.make(idx, rId) | dci], idx + 1, rId + 1}
end
end

# TODO probably better to use a zip [1..] |> map instead of fold[l|r]/reverse
{sheetCompInfos, _, nextrID} = List.foldl(sheets, {[], 1, init_rId}, add_sheet)
{Enum.reverse(sheetCompInfos), nextrID}
end

def compinfo_cell_pass_value(wci, value) do
cond do
is_binary(value) && XML.valid?(value) ->
Expand Down Expand Up @@ -59,6 +83,19 @@ defmodule Elixlsx.Compiler do
end
end

def compinfo_image_pass(wci, image) do
update_in(
wci.drawingdb,
&DrawingDB.register_image(&1, image)
)
end

def compinfo_from_images(wci, images) do
List.foldl(images, wci, fn image, wci ->
compinfo_image_pass(wci, image)
end)
end

@spec compinfo_from_rows(WorkbookCompInfo.t(), list(list(any()))) :: WorkbookCompInfo.t()
def compinfo_from_rows(wci, rows) do
List.foldl(rows, wci, fn cols, wci ->
Expand All @@ -71,16 +108,20 @@ defmodule Elixlsx.Compiler do
@spec compinfo_from_sheets(WorkbookCompInfo.t(), list(Sheet.t())) :: WorkbookCompInfo.t()
def compinfo_from_sheets(wci, sheets) do
List.foldl(sheets, wci, fn sheet, wci ->
compinfo_from_rows(wci, sheet.rows)
wci
|> compinfo_from_rows(sheet.rows)
|> compinfo_from_images(sheet.images)
end)
end

@first_free_rid 2
def make_workbook_comp_info(workbook) do
{sci, next_rId} = make_sheet_info(workbook.sheets, @first_free_rid)
{dci, next_rId} = make_drawing_info(workbook.sheets, next_rId)

%WorkbookCompInfo{
sheet_info: sci,
drawing_info: dci,
next_free_xl_rid: next_rId
}
|> compinfo_from_sheets(workbook.sheets)
Expand Down
24 changes: 24 additions & 0 deletions lib/elixlsx/compiler/drawing_comp_info.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Elixlsx.Compiler.DrawingCompInfo do
alias Elixlsx.Compiler.DrawingCompInfo

@moduledoc ~S"""
Compilation info for a Drawing, to be filled during the actual
write process.
"""
defstruct rId: "", filename: "drawing1.xml", drawingId: 0

@type t :: %DrawingCompInfo{
rId: String.t(),
filename: String.t(),
drawingId: non_neg_integer
}

@spec make(non_neg_integer, non_neg_integer) :: DrawingCompInfo.t()
def make(drawingidx, rId) do
%DrawingCompInfo{
rId: "rId" <> to_string(rId),
filename: "drawing" <> to_string(drawingidx) <> ".xml",
drawingId: drawingidx
}
end
end
56 changes: 56 additions & 0 deletions lib/elixlsx/compiler/drawing_db.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule Elixlsx.Compiler.DrawingDB do
alias __MODULE__
alias Elixlsx.Compiler.DBUtil
alias Elixlsx.Image

@doc """
Database of drawing elements in the whole document. drawing id values must be
unique across the document regardless of what kind of drawing they are.
So far this only supports images, but could be extended to include other
kinds of drawing.
An alternative would be to add a Drawing module and have "subclasses" for
different drawing types
"""

defstruct images: %{}, element_count: 0

@type t :: %DrawingDB{
images: %{Image.t() => pos_integer},
element_count: non_neg_integer
}

def register_image(drawingdb, image) do
case Map.fetch(drawingdb.images, image) do
:error ->
%DrawingDB{
images: Map.put(drawingdb.images, image, drawingdb.element_count + 1),
element_count: drawingdb.element_count + 1
}

{:ok, _} ->
drawingdb
end
end

def get_id(drawingdb, image) do
case Map.fetch(drawingdb.images, image) do
:error ->
raise %ArgumentError{
message: "Invalid key provided for DrawingDB.get_id: " <> inspect(image)
}

{:ok, id} ->
id
end
end

def id_sorted_drawings(db), do: DBUtil.id_sorted_values(db.images)

def image_types(db) do
db.images
|> Enum.reduce(%MapSet{}, fn {i, _}, acc ->
MapSet.put(acc, {i.extension, i.type})
end)
|> Enum.to_list()
end
end
6 changes: 5 additions & 1 deletion lib/elixlsx/compiler/workbook_comp_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ defmodule Elixlsx.Compiler.WorkbookCompInfo do
required to generate the XML file.
It is used as the aggregator when folding over the individual
cells.
cells and images.
"""
defstruct sheet_info: nil,
drawing_info: nil,
stringdb: %Compiler.StringDB{},
fontdb: %Compiler.FontDB{},
filldb: %Compiler.FillDB{},
cellstyledb: %Compiler.CellStyleDB{},
numfmtdb: %Compiler.NumFmtDB{},
borderstyledb: %Compiler.BorderStyleDB{},
drawingdb: %Compiler.DrawingDB{},
next_free_xl_rid: nil

@type t :: %Compiler.WorkbookCompInfo{
sheet_info: [Compiler.SheetCompInfo.t()],
drawing_info: [Compiler.DrawingCompInfo.t()],
stringdb: Compiler.StringDB.t(),
fontdb: Compiler.FontDB.t(),
filldb: Compiler.FillDB.t(),
cellstyledb: Compiler.CellStyleDB.t(),
numfmtdb: Compiler.NumFmtDB.t(),
borderstyledb: Compiler.BorderStyleDB.t(),
drawingdb: Compiler.DrawingDB.t(),
next_free_xl_rid: non_neg_integer
}
end
72 changes: 72 additions & 0 deletions lib/elixlsx/image.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
defmodule Elixlsx.Image do
alias Elixlsx.Image

@moduledoc ~S"""
Structure for excel drawing files.
- x_from_offset: integer
- x_to_offset: integer
- y_from_offset: integer
- y_to_offset: integer
- positioning: atom (:absolute, :oneCell, :twoCell)
- width: integer
- height: integer
"""

defstruct file_path: "",
type: "image/png",
extension: "png",
rowidx: 0,
colidx: 0,
x_from_offset: 0,
y_from_offset: 0,
x_to_offset: 0,
y_to_offset: 0,
positioning: "",
width: 1,
height: 1

@type t :: %Image{
file_path: String.t(),
type: String.t(),
extension: String.t(),
rowidx: integer,
colidx: integer,
x_from_offset: integer,
y_from_offset: integer,
x_to_offset: integer,
y_to_offset: integer,
positioning: atom | String.t(),
width: integer,
height: integer
}

@doc """
Create an image struct based on opts
"""
def new(file_path, rowidx, colidx, opts \\ []) do
{ext, type} = image_type(file_path)

%Image{
file_path: file_path,
type: type,
extension: ext,
rowidx: rowidx,
colidx: colidx,
x_from_offset: Keyword.get(opts, :x_from_offset, 0),
y_from_offset: Keyword.get(opts, :y_from_offset, 0),
x_to_offset: Keyword.get(opts, :x_to_offset, 0),
y_to_offset: Keyword.get(opts, :y_to_offset, 0),
positioning: Keyword.get(opts, :positioning, ""),
width: Keyword.get(opts, :width, 1),
height: Keyword.get(opts, :height, 1)
}
end

defp image_type(file_path) do
case Path.extname(file_path) do
".jpg" -> {"jpg", "image/jpeg"}
".jpeg" -> {"jpeg", "image/jpeg"}
".png" -> {"png", "image/png"}
end
end
end
Loading