diff --git a/docs/src/examples.md b/docs/src/examples.md index 17e166ae..f9709692 100644 --- a/docs/src/examples.md +++ b/docs/src/examples.md @@ -326,4 +326,33 @@ XLSX.setBorder(s, "F112,F113"; allsides = ["style" => "thin", "color" => "black" XLSX.setBorder(s, "B112:B114,D112:D115"; outside = ["style" => "thin", "color" => "black"]) XLSX.writexlsx("myNewTemplate.xlsx", f, overwrite=true) -``` \ No newline at end of file +``` + +## Adding a plot image + +Use Julia functionality to create a chart based upon data from a spreadsheet and then add that chart +(as a static image) back into the worksheet. + +![image|320x500](./images/Add_image_1.png) + +```julia +using CairoMakie, XLSX + +f=opentemplate("Example_add_chart.xlsx") +table = XLSX.gettable(f[1]) +x = 1:length(table.data[1]) + +fig = Figure() +ax = Axis(fig[1, 1], xticks=(x, table.data[1])) +barplot!(ax, x, table.data[2]) + +# Write PNG to IOBuffer +io = IOBuffer() +show(io, MIME("image/png"), fig) + +XLSX.addImage(f[1], "D2:H12", io) + +XLSX.writexlsx("Example_add_chart_out.xlsx", f, overwrite=true) +``` + +![image|320x500](./images/Add_image_2.png) \ No newline at end of file diff --git a/docs/src/images/Add_image_1.png b/docs/src/images/Add_image_1.png new file mode 100644 index 00000000..bfd390fa Binary files /dev/null and b/docs/src/images/Add_image_1.png differ diff --git a/docs/src/images/Add_image_2.png b/docs/src/images/Add_image_2.png new file mode 100644 index 00000000..141f2bf5 Binary files /dev/null and b/docs/src/images/Add_image_2.png differ diff --git a/src/images.jl b/src/images.jl index 66eceb57..356f4bad 100644 --- a/src/images.jl +++ b/src/images.jl @@ -109,9 +109,8 @@ Supported formats (auto-detected): PNG, JPEG, GIF. # Keyword Arguments -- `size`: provide the desired size of the image as a tuple of integers: `(width_px, height_px)`. Actual size -will snap to the nearest actual cell boundaries. If `nothing` (default), the image's native pixel size is used. -Ignored if `ref` is a cell range. +- `size`: provide the desired size of the image as a tuple of integers: `(width_px, height_px)`. If `nothing` (default), +the image's native pixel size is used. Ignored if `ref` is a cell range. Actual size will snap to the nearest cell boundaries. # Return Value diff --git a/src/table.jl b/src/table.jl index 5bdfc21b..5f1b1167 100644 --- a/src/table.jl +++ b/src/table.jl @@ -916,6 +916,7 @@ XLSXFile("blank.xlsx") containing 1 Worksheet """ function XLSXFile(table) + Tables.istable(table) || throw(XLSXError("Input must be a Tables.jl compatible table.")) isempty(Tables.rows(table)) && throw(XLSXError("Cannot create XLSXFile from an empty table.")) xf = newxlsx() writetable!(xf[1], table)