Skip to content
Merged
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
18 changes: 18 additions & 0 deletions Sources/PSWriteOffice/Cmdlets/Word/NewOfficeWordTextCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public class NewOfficeWordTextCommand : PSCmdlet

protected override void ProcessRecord()
{
var textLength = Text.Length;
if (Bold != null && Bold.Length != textLength)
{
throw new ArgumentException("Bold length must match Text length.", nameof(Bold));
}
if (Italic != null && Italic.Length != textLength)
{
throw new ArgumentException("Italic length must match Text length.", nameof(Italic));
}
if (Underline != null && Underline.Length != textLength)
{
throw new ArgumentException("Underline length must match Text length.", nameof(Underline));
}
if (Color != null && Color.Length != textLength)
{
throw new ArgumentException("Color length must match Text length.", nameof(Color));
}

var paragraph = WordDocumentService.AddText(
ParameterSetName == "Document" ? Document : null,
ParameterSetName == "Paragraph" ? Paragraph : null,
Expand Down
18 changes: 18 additions & 0 deletions Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using DocumentFormat.OpenXml.Wordprocessing;
using OfficeIMO.Word;

Expand All @@ -9,6 +10,23 @@
bool?[]? italic, UnderlineValues?[]? underline, string[]? color, JustificationValues? alignment,
WordParagraphStyles? style)
{
if (bold != null && bold.Length != text.Length)
{
throw new ArgumentException("bold length must match text length", nameof(bold));
}
if (italic != null && italic.Length != text.Length)
{
throw new ArgumentException("italic length must match text length", nameof(italic));
}
if (underline != null && underline.Length != text.Length)
{
throw new ArgumentException("underline length must match text length", nameof(underline));
}
if (color != null && color.Length != text.Length)
{
throw new ArgumentException("color length must match text length", nameof(color));
}

var para = paragraph ?? document!.AddParagraph();

for (var t = 0; t < text.Length; t++)
Expand All @@ -17,11 +35,11 @@

if (bold != null && t < bold.Length && bold[t].HasValue)
{
para.Bold = bold[t].Value;

Check warning on line 38 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / macOS

Nullable value type may be null.

Check warning on line 38 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / macOS

Nullable value type may be null.

Check warning on line 38 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / Ubuntu

Nullable value type may be null.

Check warning on line 38 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / Ubuntu

Nullable value type may be null.

Check warning on line 38 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / Windows

Nullable value type may be null.

Check warning on line 38 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / Windows

Nullable value type may be null.
}
if (italic != null && t < italic.Length && italic[t].HasValue)
{
para.Italic = italic[t].Value;

Check warning on line 42 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / macOS

Nullable value type may be null.

Check warning on line 42 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / macOS

Nullable value type may be null.

Check warning on line 42 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / Ubuntu

Nullable value type may be null.

Check warning on line 42 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / Ubuntu

Nullable value type may be null.

Check warning on line 42 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / Windows

Nullable value type may be null.

Check warning on line 42 in Sources/PSWriteOffice/Services/Word/WordDocumentService.Text.cs

View workflow job for this annotation

GitHub Actions / Windows

Nullable value type may be null.
}
}

Expand Down
15 changes: 15 additions & 0 deletions Tests/WordCmdlets.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ Describe 'Word cmdlets' {
Close-OfficeWord -Document $doc
}

It 'throws when optional array length mismatches Text' {
$path = Join-Path $TestDrive 'mismatch.docx'
$doc = New-OfficeWord -FilePath $path
{ New-OfficeWordText -Document $doc -Text @('one','two') -Bold @($true) } | Should -Throw
{ New-OfficeWordText -Document $doc -Text @('one','two') -Color @('FF0000') } | Should -Throw
Close-OfficeWord -Document $doc
}

It 'throws in service when array length mismatches Text' {
$path = Join-Path $TestDrive 'mismatchService.docx'
$doc = New-OfficeWord -FilePath $path
{ [PSWriteOffice.Services.Word.WordDocumentService]::AddText($doc, $null, @('a','b'), @($true), $null, $null, $null, $null, $null) } | Should -Throw
Close-OfficeWord -Document $doc
}

It 'adds table' {
$path = Join-Path $TestDrive 'table.docx'
$doc = New-OfficeWord -FilePath $path
Expand Down
Loading