diff --git a/Export-Excel.ps1 b/Export-Excel.ps1 index 58bde31..8e955b3 100644 --- a/Export-Excel.ps1 +++ b/Export-Excel.ps1 @@ -1339,6 +1339,14 @@ function Add-ExcelTable { [Switch]$PassThru ) try { + if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress($TableName)) { + Write-Warning -Message "$tableName reads as an Excel address, and so is not allowed as a table name." + return + } + if ($tableName -notMatch '^[A-Z]') { + Write-Warning -Message "$tableName is not allowed as a table name because it does not begin with a letter." + return + } if ($TableName -match "\W") { Write-Warning -Message "At least one character in $TableName is illegal in a table name and will be replaced with '_' . " $TableName = $TableName -replace '\W', '_' diff --git a/New-ConditionalText.ps1 b/New-ConditionalText.ps1 index 29d58fe..3f32fb6 100644 --- a/New-ConditionalText.ps1 +++ b/New-ConditionalText.ps1 @@ -36,8 +36,8 @@ function New-ConditionalText { This builds on the previous example, and specifies a condition of <=3 with a format of red text on a white background; this applies to a named range - "Finish Position". The range could be written "C:C" to specify a named - column, or "C2:C102" to specify certain cells in the column. + "Finish Position". The range could be written -Range "C:C" to specify a + named column, or -Range "C2:C102" to specify certain cells in the column. .Link Add-Add-ConditionalFormatting New-ConditionalFormattingIconSet diff --git a/README.md b/README.md index 0f49ce6..10fbc5d 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ Install-Module ImportExcel -scope CurrentUser Install-Module ImportExcel ``` +# What's new +- Set-ExcelRow and Set-ExcelColumn now check that the worksheet name they passed exists in the workbook. + # What's new 5.4 - Thank you to Conrad Agramont, Twitter: [@AGramont](https://twitter.com/@AGramont) for the `AddMultiWorkSheet.ps1` example. Much appreciated! diff --git a/Set-Column.ps1 b/Set-Column.ps1 index 36a181e..e0eeebd 100644 --- a/Set-Column.ps1 +++ b/Set-Column.ps1 @@ -121,7 +121,12 @@ begin { #if we were passed a package object and a worksheet name , get the worksheet. - if ($ExcelPackage) {$Worksheet = $ExcelPackage.Workbook.Worksheets[$Worksheetname] } + if ($ExcelPackage) { + if ($ExcelPackage.Workbook.Worksheets.Name -notcontains $Worksheetname) { + throw "The Workbook does not contain a sheet named '$Worksheetname'" + } + else {$Worksheet = $ExcelPackage.Workbook.Worksheets[$Worksheetname] } + } #In a script block to build a formula, we may want any of corners or the column name, #if Column and Startrow aren't specified, assume first unused column, and first row diff --git a/Set-Row.ps1 b/Set-Row.ps1 index 5d36365..fe385c8 100644 --- a/Set-Row.ps1 +++ b/Set-Row.ps1 @@ -116,8 +116,12 @@ ) begin { #if we were passed a package object and a worksheet name , get the worksheet. - if ($ExcelPackage) {$Worksheet = $ExcelPackage.Workbook.worksheets[$Worksheetname] } - + if ($ExcelPackage) { + if ($ExcelPackage.Workbook.Worksheets.Name -notcontains $Worksheetname) { + throw "The Workbook does not contain a sheet named '$Worksheetname'" + } + else {$Worksheet = $ExcelPackage.Workbook.Worksheets[$Worksheetname] } + } #In a script block to build a formula, we may want any of corners or the columnname, #if row and start column aren't specified assume first unused row, and first column if (-not $StartColumn) {$StartColumn = $Worksheet.Dimension.Start.Column }