From 91da711635e1c498b02228b2b0bf6e4ca6a9e683 Mon Sep 17 00:00:00 2001 From: Josh Hendricks Date: Tue, 1 Feb 2022 21:37:55 -0800 Subject: [PATCH 1/4] docs: add Add-ExcelImage example --- Examples/AddPicture/Add-ExcelImage.ps1 | 115 +++++++++++++++++++++++++ Examples/AddPicture/AddPicture.ps1 | 34 ++++++++ Examples/AddPicture/Octocat.jpg | Bin 0 -> 6272 bytes Examples/AddPicture/README.md | 19 ++++ 4 files changed, 168 insertions(+) create mode 100644 Examples/AddPicture/Add-ExcelImage.ps1 create mode 100644 Examples/AddPicture/AddPicture.ps1 create mode 100644 Examples/AddPicture/Octocat.jpg create mode 100644 Examples/AddPicture/README.md diff --git a/Examples/AddPicture/Add-ExcelImage.ps1 b/Examples/AddPicture/Add-ExcelImage.ps1 new file mode 100644 index 0000000..c3e6d17 --- /dev/null +++ b/Examples/AddPicture/Add-ExcelImage.ps1 @@ -0,0 +1,115 @@ +function Add-ExcelImage { + <# + .SYNOPSIS + Adds an image to a worksheet in an Excel package. + .DESCRIPTION + Adds an image to a worksheet in an Excel package using the + `WorkSheet.Drawings.AddPicture(name, image)` method, and places the + image at the location specified by the Row and Column parameters. + + Additional position adjustment can be made by providing RowOffset and + ColumnOffset values in pixels. + .EXAMPLE + $image = [System.Drawing.Image]::FromFile($octocat) + $xlpkg = $data | Export-Excel -Path $path -PassThru + $xlpkg.Sheet1 | Add-ExcelImage -Image $image -Row 4 -Column 6 -ResizeCell + + Where $octocat is a path to an image file, and $data is a collection of + data to be exported, and $path is the output path for the Excel document, + Add-Excel places the image at row 4 and column 6, resizing the column + and row as needed to fit the image. + .INPUTS + [OfficeOpenXml.ExcelWorksheet] + .OUTPUTS + None + #> + [CmdletBinding()] + param( + # Specifies the worksheet to add the image to. + [Parameter(Mandatory, ValueFromPipeline)] + [OfficeOpenXml.ExcelWorksheet] + $WorkSheet, + + # Specifies the Image to be added to the worksheet. + [Parameter(Mandatory)] + [System.Drawing.Image] + $Image, + + # Specifies the row where the image will be placed. Rows are counted from 1. + [Parameter(Mandatory)] + [ValidateRange(1, [int]::MaxValue)] + [int] + $Row, + + # Specifies the column where the image will be placed. Columns are counted from 1. + [Parameter(Mandatory)] + [ValidateRange(1, [int]::MaxValue)] + [int] + $Column, + + # Specifies the name to associate with the image. Names must be unique per sheet. + # Omit the name and a GUID will be used instead. + [Parameter()] + [string] + $Name, + + # Specifies the number of pixels to offset the image on the Y-axis. A + # positive number moves the image down by the specified number of pixels + # from the top border of the cell. + [Parameter()] + [int] + $RowOffset = 1, + + # Specifies the number of pixels to offset the image on the X-axis. A + # positive number moves the image to the right by the specified number + # of pixels from the left border of the cell. + [Parameter()] + [int] + $ColumnOffset = 1, + + # Increase the column width and row height to fit the image if the current + # dimensions are smaller than the image provided. + [Parameter()] + [switch] + $ResizeCell + ) + + begin { + <# + These ratios work on my machine but it feels fragile. Need to better + understand how row and column sizing works in Excel and what the + width and height units represent. + #> + $widthFactor = 1 / 7 + $heightFactor = 3 / 4 + } + + process { + if ([string]::IsNullOrWhiteSpace($Name)) { + $Name = (New-Guid).ToString() + } + if ($null -ne $WorkSheet.Drawings[$Name]) { + Write-Error "A picture with the name `"$Name`" already exists in worksheet $($WorkSheet.Name)." + return + } + + <# + The row and column offsets of 1 ensures that the image lands just + inside the gray cell borders at the top left. + #> + $picture = $WorkSheet.Drawings.AddPicture($Name, $Image) + $picture.SetPosition($Row - 1, $RowOffset, $Column - 1, $ColumnOffset) + + if ($ResizeCell) { + <# + Adding 1 to the image height and width ensures that when the + row and column are resized, the bottom right of the image lands + just inside the gray cell borders at the bottom right. + #> + $width = $widthFactor * ($Image.Width + 1) + $height = $heightFactor * ($Image.Height + 1) + $WorkSheet.Column($Column).Width = [Math]::Max($width, $WorkSheet.Column($Column).Width) + $WorkSheet.Row($Row).Height = [Math]::Max($height, $WorkSheet.Row($Row).Height) + } + } +} \ No newline at end of file diff --git a/Examples/AddPicture/AddPicture.ps1 b/Examples/AddPicture/AddPicture.ps1 new file mode 100644 index 0000000..5230641 --- /dev/null +++ b/Examples/AddPicture/AddPicture.ps1 @@ -0,0 +1,34 @@ +Add-Type -AssemblyName System.Drawing + +. $PSScriptRoot\Add-ExcelImage.ps1 + +$data = ConvertFrom-Csv @" +Region,State,Units,Price +West,Texas,927,923.71 +North,Tennessee,466,770.67 +East,Florida,520,458.68 +East,Maine,828,661.24 +West,Virginia,465,053.58 +North,Missouri,436,235.67 +South,Kansas,214,992.47 +North,North Dakota,789,640.72 +South,Delaware,712,508.55 +"@ + +$path = "$PSScriptRoot/Add-Picture-test.xlsx" +Remove-Item $path -ErrorAction SilentlyContinue + + +try { + $octocat = "$PSScriptRoot/Octocat.jpg" + $image = [System.Drawing.Image]::FromFile($octocat) + $xlpkg = $data | Export-Excel -Path $path -PassThru + $xlpkg.Sheet1 | Add-ExcelImage -Image $image -Row 4 -Column 6 -ResizeCell +} finally { + $image.Dispose() + if ($xlpkg) { + Close-ExcelPackage -ExcelPackage $xlpkg -Show + } +} + + diff --git a/Examples/AddPicture/Octocat.jpg b/Examples/AddPicture/Octocat.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a48c3499c8f316eae14f1722c2fa2004d06e8b69 GIT binary patch literal 6272 zcmcIoXH-*bvkrzPO%Un5$pI3MNbe#wQ~^T?MHHm>(2F1_ASHp&LzCV`Afb!Wi3kXx zH>n~e^d_KO&iCDWzWd$#>;Aa2_ntL-&%2*BYvx_=nrE)2t`-5b+8PiI01*)YKy-Zp zu4VzM0Lq&;$^LQQym@W@C6qTQDXA#0A8J}EIvQ#ks@p8L1z0%*I9RyYg@hOdq$QJ)^U(hVLPSPRe*G5%B_#uhgM|b1f0nCH06KEOGGG}<#0$7VM+BrJy6Oh7 zUk7>P2Jkx4zlD^Hn1l#Oe&gD!PYWOd0&mdKJT` zFYoYrB{A^be`w^LUt7m0F7*svP}sr5FW`%+ACXY?OWu=KQ#baBe1++{#$&n83-GVp z{7bIkh)Awo0(95VL_p%}SpLgTbb}5^eESZMm?5tU{r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{6x`zw6Bdwy?j#~(*n z1^UrAZrTQL$$n7|u*CP9?TK%;MJ26FLA@G_9Z~`<+`H{y=5eQ-J{xPH)kaaDuhdV_@0+0p(ui4<|e%)5)ast8kTxszDBfc z%mj9W6F-s>N|^;Xt{RR^oEG%3=o4WLj@$67iM9|xJ5y-6(_Odg=Z@;ZPuTch9`4a7 zh;>D?3_qJl__I`)e~Y9fMuygVLN+NvdOqIMBaN}gHJ_+fgUm%m3E$E%R&Co0RVQ!? z-g^I^BhpU*DjPsdY|#_l;1#%arz+Z;+=wDD=!b1`I_;c4U9I5>b{B5eo37YL4g7f~ zM56GJ+%_%`Fn8Ltqutli%QrhZb@1ifuYkAv`0s}CpQA>S4nPDsF-MGiM6(*}&+6J% z(N}w11^y1{&XYI@lrf;d3%D*m;W6EkD9Ug&i4ChlXX(kGBKn8jly=pvtRkBS+-x=(pLZ1p;g1*IlEPPIt8>|*GR{&|IF^{>1gjx%W zgO<}@PFk1D=Cvb+YOE~8wyyy9Wn%i`>hjB5i_TuBPHh@wM!ktgsm4sL|1wEFNl3FZ znTcNftma1G5?Y^coM{9A?g4;Ju1q4}MyWgwv{PNt3w*lTov+668uR_f+5TQ9sS7dn ix?hH#H(+CXxz*IWU;X2Ky8Eo literal 0 HcmV?d00001 diff --git a/Examples/AddPicture/README.md b/Examples/AddPicture/README.md new file mode 100644 index 0000000..ff36c37 --- /dev/null +++ b/Examples/AddPicture/README.md @@ -0,0 +1,19 @@ +# Add-ExcelImage Example + +Adding pictures to an Excel worksheet is possible by calling the `AddPicture(name, image)` +method on the `Drawings` property of an `ExcelWorksheet` object. + +The `Add-ExcelImage` example here demonstrates how to add a picture at a given +cell location, and optionally resize the row and column to fit the image. + +Care has been taken in this example to get the image placement to be just inside +the cell border, and if the `-ResizeCell` switch is present, the height and width +of the row and column will be increased, if needed, so that the bottom right of +the image also lands just inside the cell border. + +The Excel row and column sizes are measured in "point" units rather than pixels, +and at the moment a fixed multiplication factor is used to convert the size of +the image in pixels, to the corresponding height and width values in Excel. + +You may find that images with a different DPI, or different resolution, DPI, or +text scaling options result in imperfect row and column sizing. \ No newline at end of file From cf5d3f83d6bb13176fd57252373b2a47fddfaa66 Mon Sep 17 00:00:00 2001 From: Josh Hendricks Date: Wed, 2 Feb 2022 08:16:22 -0800 Subject: [PATCH 2/4] docs: Update readme --- Examples/AddPicture/AddPicture.ps1 | 6 +++--- Examples/AddPicture/README.md | 22 ++++++++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Examples/AddPicture/AddPicture.ps1 b/Examples/AddPicture/AddPicture.ps1 index 5230641..d5e04fe 100644 --- a/Examples/AddPicture/AddPicture.ps1 +++ b/Examples/AddPicture/AddPicture.ps1 @@ -25,10 +25,10 @@ try { $xlpkg = $data | Export-Excel -Path $path -PassThru $xlpkg.Sheet1 | Add-ExcelImage -Image $image -Row 4 -Column 6 -ResizeCell } finally { - $image.Dispose() + if ($image) { + $image.Dispose() + } if ($xlpkg) { Close-ExcelPackage -ExcelPackage $xlpkg -Show } } - - diff --git a/Examples/AddPicture/README.md b/Examples/AddPicture/README.md index ff36c37..8127209 100644 --- a/Examples/AddPicture/README.md +++ b/Examples/AddPicture/README.md @@ -6,14 +6,28 @@ method on the `Drawings` property of an `ExcelWorksheet` object. The `Add-ExcelImage` example here demonstrates how to add a picture at a given cell location, and optionally resize the row and column to fit the image. +## Running the example + +To try this example, run the script `AddPicture.ps1`. The `Add-ExcelImage` +function will be dot-sourced, and an Excel document will be created in the same +folder with a sample data set. The Octocat image will then be embedded into +Sheet1. + +The creation of the Excel document and the `System.Drawing.Image` object +representing Octocat are properly disposed within a `finally` block to ensure +that the resources are released, even if an error occurs in the `try` block. + +## Note about column and row sizing + Care has been taken in this example to get the image placement to be just inside the cell border, and if the `-ResizeCell` switch is present, the height and width of the row and column will be increased, if needed, so that the bottom right of the image also lands just inside the cell border. The Excel row and column sizes are measured in "point" units rather than pixels, -and at the moment a fixed multiplication factor is used to convert the size of -the image in pixels, to the corresponding height and width values in Excel. +and a fixed multiplication factor is used to convert the size of the image in +pixels, to the corresponding height and width values in Excel. -You may find that images with a different DPI, or different resolution, DPI, or -text scaling options result in imperfect row and column sizing. \ No newline at end of file +It's possible that different DPI or text scaling options could result in +imperfect column and row sizing and if a better strategy is found for converting +the image dimensions to column and row sizes, this example will be updated. From f33215382af9c0e68ce168dd245316c944911dce Mon Sep 17 00:00:00 2001 From: Josh Hendricks Date: Wed, 2 Feb 2022 08:31:35 -0800 Subject: [PATCH 3/4] docs: rename to AddImage for consistency --- .../{AddPicture => AddImage}/Add-ExcelImage.ps1 | 0 .../AddPicture.ps1 => AddImage/AddImage.ps1} | 0 Examples/{AddPicture => AddImage}/Octocat.jpg | Bin Examples/{AddPicture => AddImage}/README.md | 2 +- 4 files changed, 1 insertion(+), 1 deletion(-) rename Examples/{AddPicture => AddImage}/Add-ExcelImage.ps1 (100%) rename Examples/{AddPicture/AddPicture.ps1 => AddImage/AddImage.ps1} (100%) rename Examples/{AddPicture => AddImage}/Octocat.jpg (100%) rename Examples/{AddPicture => AddImage}/README.md (95%) diff --git a/Examples/AddPicture/Add-ExcelImage.ps1 b/Examples/AddImage/Add-ExcelImage.ps1 similarity index 100% rename from Examples/AddPicture/Add-ExcelImage.ps1 rename to Examples/AddImage/Add-ExcelImage.ps1 diff --git a/Examples/AddPicture/AddPicture.ps1 b/Examples/AddImage/AddImage.ps1 similarity index 100% rename from Examples/AddPicture/AddPicture.ps1 rename to Examples/AddImage/AddImage.ps1 diff --git a/Examples/AddPicture/Octocat.jpg b/Examples/AddImage/Octocat.jpg similarity index 100% rename from Examples/AddPicture/Octocat.jpg rename to Examples/AddImage/Octocat.jpg diff --git a/Examples/AddPicture/README.md b/Examples/AddImage/README.md similarity index 95% rename from Examples/AddPicture/README.md rename to Examples/AddImage/README.md index 8127209..4d71141 100644 --- a/Examples/AddPicture/README.md +++ b/Examples/AddImage/README.md @@ -8,7 +8,7 @@ cell location, and optionally resize the row and column to fit the image. ## Running the example -To try this example, run the script `AddPicture.ps1`. The `Add-ExcelImage` +To try this example, run the script `AddImage.ps1`. The `Add-ExcelImage` function will be dot-sourced, and an Excel document will be created in the same folder with a sample data set. The Octocat image will then be embedded into Sheet1. From 5ab6a9116d03d86269de4945cadd92bb9492032c Mon Sep 17 00:00:00 2001 From: dfinke Date: Thu, 3 Feb 2022 18:43:15 -0500 Subject: [PATCH 4/4] @joshooaj added throw if not on Windows, pls review --- Examples/AddImage/Add-ExcelImage.ps1 | 4 ++++ Examples/AddImage/AddImage.ps1 | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Examples/AddImage/Add-ExcelImage.ps1 b/Examples/AddImage/Add-ExcelImage.ps1 index c3e6d17..38ce80e 100644 --- a/Examples/AddImage/Add-ExcelImage.ps1 +++ b/Examples/AddImage/Add-ExcelImage.ps1 @@ -75,6 +75,10 @@ function Add-ExcelImage { ) begin { + if ($IsWindows -eq $false) { + throw "This only works on Windows and won't run on $([environment]::OSVersion)" + } + <# These ratios work on my machine but it feels fragile. Need to better understand how row and column sizing works in Excel and what the diff --git a/Examples/AddImage/AddImage.ps1 b/Examples/AddImage/AddImage.ps1 index d5e04fe..4d38e09 100644 --- a/Examples/AddImage/AddImage.ps1 +++ b/Examples/AddImage/AddImage.ps1 @@ -1,3 +1,7 @@ +if ($IsWindows -eq $false) { + throw "This only works on Windows and won't run on $([environment]::OSVersion)" +} + Add-Type -AssemblyName System.Drawing . $PSScriptRoot\Add-ExcelImage.ps1 @@ -24,7 +28,8 @@ try { $image = [System.Drawing.Image]::FromFile($octocat) $xlpkg = $data | Export-Excel -Path $path -PassThru $xlpkg.Sheet1 | Add-ExcelImage -Image $image -Row 4 -Column 6 -ResizeCell -} finally { +} +finally { if ($image) { $image.Dispose() }