Skip to content

Commit 1c2e757

Browse files
feat: Set-OpenXML ( Fixes #19 )
Adding link, auto prefixing slash, supporting `[byte[]]` and`[IO.Stream]`
1 parent ebb4eae commit 1c2e757

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Commands/Set-OpenXML.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ function Set-OpenXML
1010
Set-OpenXML -Uri '/index.html' -Content ([xml]"<h1>Hello World</h1>") -ContentType text/html |
1111
Set-OpenXML -Uri '/404.html' -Content ([xml]"<h1>File Not Found</h1>") -ContentType text/html |
1212
Export-OpenXML ./Examples/Sample2.docx
13+
.LINK
14+
Get-OpenXML
1315
#>
1416
param(
1517
# The uri to set
@@ -43,6 +45,11 @@ function Set-OpenXML
4345
return $InputObject
4446
}
4547

48+
# If the uri is not prefixed,
49+
if ($uri -notmatch '^/') {
50+
$uri = "/$uri" # add it to avoid easy errors.
51+
}
52+
4653
# Get or create the part
4754
$part =
4855
if ($InputObject.PartExists($uri)) {
@@ -55,8 +62,18 @@ function Set-OpenXML
5562

5663
# Get the stream
5764
$partStream = $part.GetStream()
65+
# First see if the content is a byte[]
66+
if ($content -is [byte[]]) {
67+
# if so, just write it
68+
$partStream.Write($content, 0, $content.Length)
69+
}
70+
# If the content is a stream,
71+
elseif ($content -is [IO.Stream]) {
72+
# copy it in.
73+
$content.CopyTo($partStream)
74+
}
5875
# If the content was xml or could be,
59-
if ($content -is [xml] -or ($contentXml = $content -as [xml])) {
76+
elseif ($content -is [xml] -or ($contentXml = $content -as [xml])) {
6077
if ($contentXml) { $content = $contentXml }
6178
$buffer = $OutputEncoding.GetBytes($content.OuterXml)
6279
# write it to the package.

0 commit comments

Comments
 (0)