Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with getting a Table without Headers #1569

Closed
NoralK opened this issue Feb 28, 2024 · 6 comments
Closed

Help with getting a Table without Headers #1569

NoralK opened this issue Feb 28, 2024 · 6 comments

Comments

@NoralK
Copy link

NoralK commented Feb 28, 2024

I have look for hours trying to figure out how to get a Table Range back that does not include the table headers so I can add a named reference that has a scope of Workbook. I would use -RangeName when creating the Table but two issues with that, 1) the scope of the RangeName is the worksheet only and 2) the Range includes the headers. This is what I have:

$TableRange = $xl.Workbook.Worksheets["theSheet"].Tables["theTable"].Address.Address
$RangeCells=$ws.cells[$TableRange]
$xl.Workbook.Names.Add("theName", $RangeCells)

Please let me know and if there is a better way.
Cheers

@dfinke
Copy link
Owner

dfinke commented Feb 28, 2024

Sorry @NoralK, I have never tried to that nor have I seen it done. I believe you are the first 🎉

@NoralK
Copy link
Author

NoralK commented Mar 4, 2024

@dfinke Does this mean you will not look into it?

@dfinke
Copy link
Owner

dfinke commented Mar 7, 2024

@NoralK not for a while. I have other pressing matters.

@pauljnav
Copy link

pauljnav commented Mar 8, 2024

Hi @NoralK (CC: @dfinke)
In VBA, the Range.Offset property represents a range that's offset from the specified range.
I see the same exists in ImportExcel for the $RangeCells object.

Try the following for size:

# Define offset range, omitting row 1.
$offsetRange = $RangeCells.Offset(1, 0)
# Reveal the cell addresses to confirm
$offsetRange | Select-Object Address
$RangeCells | Select-Object Address

@NoralK
Copy link
Author

NoralK commented Mar 9, 2024

@pauljnav Thank you! for that nudge...I was able to come up with this complete solution:

$xlSourcefile = "./theFile.xlsx"

$data = Import-Csv "./SingleColumnOfDataWithHeaderRow.csv"

$data | Export-Excel $xlSourcefile -WorksheetName "theSheet" `
    -TableStyle None `
    -AutoSize -StartRow 1 -StartColumn 6 `
    -TableName "theTable"

$xl = Open-ExcelPackage $xlSourcefile
$ws = $xl.Workbook.Worksheets["theSheet"]
$TableRange = $ws.Tables["theTable"].Address.Address
# Original Range F1:F50
$RangeCells=$ws.cells[$TableRange]
# Define offset range, omitting row 1
#     however this gives an extra row that we do not want.
# will product F2:F51
$offsetRange = $RangeCells.Offset(1, 0)
# Need F2:F50 - mash it together
$NewTableRange = "$($offsetRange.Start.Address):$($rangecells.End.Address)"
# Get the cells
$RangeWithoutHeaders = $ws.cells[$NewTableRange]
# Add the named range
# F2:F50 
$xl.Workbook.Names.Add("theName", $RangeWithoutHeaders) | Out-Null
# Now I can reference theName[ColumnName] in the entire
#     Workbook where theTable[ColumnName] has issues
Close-ExcelPackage $xl

@NoralK NoralK closed this as completed Mar 9, 2024
@dfinke
Copy link
Owner

dfinke commented Mar 10, 2024

Glad Open-ExcelPackage and the -PassThru are part of the ImportExcel. Escape hatch to the underlying object model.

Thanks all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants