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 #79

Open
wants to merge 3 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
12 changes: 12 additions & 0 deletions example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ sheet4
sheet5 = %Sheet{name: "No gridlines shown", show_grid_lines: false}
|> Sheet.set_at(0, 0, "Just this cell")

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

sheet6 =
sheet6
|> 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)
|> 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.
55 changes: 48 additions & 7 deletions lib/elixlsx/compiler.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule Elixlsx.Compiler do
alias Elixlsx.Compiler.WorkbookCompInfo
alias Elixlsx.Compiler.SheetCompInfo
alias Elixlsx.Compiler.DrawingCompInfo
alias Elixlsx.Compiler.CellStyleDB
alias Elixlsx.Compiler.StringDB
alias Elixlsx.Compiler.DrawingDB
alias Elixlsx.Sheet

@doc ~S"""
Expand All @@ -23,7 +25,29 @@ defmodule Elixlsx.Compiler do
{Enum.reverse(sheetCompInfos), nextrID}
end

def compinfo_cell_pass_value wci, value do
@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) && String.valid?(value)
-> update_in wci.stringdb, &StringDB.register_string(&1, value)
Expand Down Expand Up @@ -62,20 +86,37 @@ defmodule Elixlsx.Compiler do
end
end

@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
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_sheets(WorkbookCompInfo.t(), list(Sheet.t())) :: WorkbookCompInfo.t()
def compinfo_from_sheets(wci, sheets) do
List.foldl(sheets, wci, fn sheet, wci ->
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,
next_free_xl_rid: next_rId,
drawing_info: dci,
next_free_xl_rid: next_rId
}
|> compinfo_from_sheets(workbook.sheets)
|> CellStyleDB.register_all
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
58 changes: 58 additions & 0 deletions lib/elixlsx/compiler/drawing_db.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 @@ -5,25 +5,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
65 changes: 65 additions & 0 deletions lib/elixlsx/image.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
defmodule Elixlsx.Image do
alias Elixlsx.Image

@moduledoc ~S"""
Structure for excel drawing files.

- x_offset: integer
- y_offset: integer
- x_scale: float
- y_scale: float
- positioning: atom (:absolute, :oneCell, :twoCell)
"""

defstruct file_path: "",
type: "image/png",
extension: "png",
rowidx: 0,
colidx: 0,
x_offset: 0,
y_offset: 0,
x_scale: 1,
y_scale: 1,
positioning: :twoCell

@type t :: %Image{
file_path: String.t(),
type: String.t(),
extension: String.t(),
rowidx: integer,
colidx: integer,
x_offset: integer,
y_offset: integer,
x_scale: float,
y_scale: float,
positioning: atom
}

@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_offset: Keyword.get(opts, :x_offset, 0),
y_offset: Keyword.get(opts, :y_offset, 0),
x_scale: Keyword.get(opts, :x_scale, 1),
y_scale: Keyword.get(opts, :y_scale, 1),
positioning: Keyword.get(opts, :positioning, :twoCell)
}
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
45 changes: 35 additions & 10 deletions lib/elixlsx/sheet.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule Elixlsx.Sheet do
alias __MODULE__
alias Elixlsx.Sheet
alias Elixlsx.Image
alias Elixlsx.Util
@moduledoc ~S"""
Describes a single sheet with a given name. The name can be up to 31 characters long.
Expand All @@ -16,10 +17,11 @@ defmodule Elixlsx.Sheet do
The property list describes formatting options for that
cell. See Font.from_props/1 for a list of options.
"""
defstruct name: "", rows: [], col_widths: %{}, row_heights: %{}, merge_cells: [], pane_freeze: nil, show_grid_lines: true
defstruct name: "", rows: [], images: [], col_widths: %{}, row_heights: %{}, merge_cells: [], pane_freeze: nil, show_grid_lines: true
@type t :: %Sheet {
name: String.t,
rows: list(list(any())),
images: list(Image.t()),
col_widths: %{pos_integer => number},
row_heights: %{pos_integer => number},
merge_cells: [],
Expand Down Expand Up @@ -96,28 +98,36 @@ defmodule Elixlsx.Sheet do
"""
def set_at(sheet, rowidx, colidx, content, opts \\ [])
when is_number(rowidx) and is_number(colidx) do
sheet = maybe_extend(sheet, rowidx, colidx)

update_in(sheet.rows, fn rows ->
List.update_at(rows, rowidx, fn cols ->
List.replace_at(cols, colidx, [content | opts])
end)
end)
end

@spec maybe_extend(Sheet.t(), non_neg_integer, non_neg_integer) :: Sheet.t()
defp maybe_extend(sheet, rowidx, colidx) do
cond do
length(sheet.rows) <= rowidx ->
# append new rows, call self again with new sheet
n_new_rows = rowidx - length(sheet.rows)
new_rows = 0..n_new_rows |> Enum.map(fn _ -> [] end)

update_in(sheet.rows, &(&1 ++ new_rows)) |>
set_at(rowidx, colidx, content, opts)
update_in(sheet.rows, &(&1 ++ new_rows))
|> maybe_extend(rowidx, colidx)

length(Enum.at(sheet.rows, rowidx)) <= colidx ->
n_new_cols = colidx - length(Enum.at(sheet.rows, rowidx))
new_cols = 0..n_new_cols |> Enum.map(fn _ -> nil end)
new_row = Enum.at(sheet.rows, rowidx) ++ new_cols

update_in(sheet.rows, &(List.replace_at &1, rowidx, new_row)) |>
set_at(rowidx, colidx, content, opts)
update_in(sheet.rows, &List.replace_at(&1, rowidx, new_row))
|> maybe_extend(rowidx, colidx)

true ->
update_in sheet.rows, fn rows ->
List.update_at rows, rowidx, fn cols ->
List.replace_at cols, colidx, [content | opts]
end
end
sheet
end
end

Expand Down Expand Up @@ -156,4 +166,19 @@ defmodule Elixlsx.Sheet do
def remove_pane_freeze(sheet) do
%{sheet | pane_freeze: nil}
end

@doc """
Insert an image at a given position.
"""
@spec insert_image(Sheet.t(), non_neg_integer, non_neg_integer, String.t(), key: any) ::
Sheet.t()
def insert_image(sheet, rowidx, colidx, imagepath, opts \\ [])
when is_number(rowidx) and is_number(colidx) do
image = Image.new(imagepath, rowidx, colidx, opts)
# Ensure there are enough rows and columns to accomodate the image position
sheet = maybe_extend(sheet, rowidx, colidx)

# Add the image to the list of images in this sheet
update_in(sheet.images, &[image | &1])
end
end
Loading