Skip to content

Is it possible to trim the table headers before import? #1258

@dfinke

Description

@dfinke

Discussed in #1257

Originally posted by ccna2ccda September 27, 2022
Hi everyone,

When working on excel worksheets, I found more than once that I didn't expect the table header or even the cell value has trailing or leading spaces. "Team " and "Team" look no difference, but it makes code difficult to maintain.

I am wondering if the import-excel can include a switch to trim table headers or even cell before returning the psobject arraylist. I used my function to trim cells, but since the property name can't be changed, i don't know any easy to handle headers.

Any feedback is much appreciated. Thanks.

function trimTable {
[CmdletBinding()]
param($table)

if ($null -ne $table -and $table.count -ne 0) {
    write-verbose -Message "Trim each cell of String type in the Table."  
    $row_num = 2
    $last_non_empty_row = -1
    foreach ($row in $table) {
        foreach ($pname in $table[0].psobject.properties.name) {
            if ($null -ne $row.$pname -and $row.$pname.getType().name -eq "String") {
                $row.$pname = $row.$pname.trim()
            } 

            # non-empty cell
            if ("$($row.$pname)".Length -gt 0) {
                $last_non_empty_row = $row_num
            }
        }
        $row_num ++
    }
}
return $last_non_empty_row

}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions