Multiple Tables per Worksheet and Data Validation within each Table.
mc1903 opened this issue · comments
Hi Doug,
I'm looking for some help to see if there are there better ways of:
- tracking the worksheet rows where variable length tables start & end?
- defining a data validation range that is an entire column within a table. I have tried using
'Table1[Region]'
as the Validation Range but it does not work. The workbook is created but gets repaired on opening and the tables are lost.
The below is functional, but ugly. I would be grateful if you could offer any advice or pointers to clean it up?
Contrived example with janky test code:
The workbook Template.xlsx
has a single worksheet, called Lookups
which is my data validation list contents:
A | |
---|---|
1 | Region |
2 | North |
3 | East |
4 | South |
5 | West |
$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
"@ | Sort-Object {$_.State}
$data2 = $data | Sort-Object {$_.State} -Descending
$templateFile = "P:\ImportExcel_Testing\Template.xlsx"
$template = $data | Export-Excel -Path $templateFile -WorksheetName 'Data' -StartRow 2 -AutoSize -TableName 'Table1' -PassThru
$lastRow = $template.Data.Dimension.Rows
$ValidationParams = @{
Worksheet = $template.Data
ShowErrorMessage = $true
ErrorStyle = 'stop'
ErrorTitle = 'Invalid Data'
}
$regionValidationParams1 = @{
#Range = 'Table1[Region]'
Range = "A3:A$($lastRow +1)"
ValidationType = 'List'
Formula = 'Lookups!$A$2:$A$5'
ErrorBody = "You must select an item from the list."
}
$nextRow = $($lastRow +3)
$template = $data2 | Export-Excel -ExcelPackage $template -WorksheetName 'Data' -StartRow $nextRow -AutoSize -TableName 'Table2' -PassThru
$lastRow = $template.Data.Dimension.Rows
$regionValidationParams2 = @{
#Range = 'Table2[Region]'
Range = "A$($nextRow +1):A$($lastRow +1)"
ValidationType = 'List'
Formula = 'Lookups!$A$2:$A$5'
ErrorBody = "You must select an item from the list."
}
Add-ExcelDataValidationRule @ValidationParams @regionValidationParams1
Add-ExcelDataValidationRule @ValidationParams @regionValidationParams2
Close-ExcelPackage -ExcelPackage $template -SaveAs "P:\ImportExcel_Testing\Template_New.xlsx" -Show
The Data
worksheet that is generated looks like this, with only the Region
column in each table having the in-cell drop down list.
Thanks
M
@mc1903 thanks for using ImportExcel.
I have not been that down that road like that. I'll think about it.
Off the top of my head I thought maybe it has something to do with with more than one table for the worksheet.
Does it work with only one table? I beleive the table is defined globally in the xlsx and I have not investigated how to keep it to a sheet (if possible) or if that'll make a difference.
@dfinke thanks for creating ImportExcel.
I use it pretty much every week to automate mind numbing data transformation tasks.
It amazes people who spend their days copy & pasting data between workbooks, that it can be completed in a few seconds, with greater accuracy; just with a small amount of effort in PowerShell.
If you do have any future thoughts on my questions I would be grateful if you can pop them in this thread.
Have a great weekend.
M