-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathGet-ExcelFileSchema.ps1
47 lines (37 loc) · 1.24 KB
/
Get-ExcelFileSchema.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function Get-ExcelFileSchema {
<#
.SYNOPSIS
Gets the schema of an Excel file.
.DESCRIPTION
The Get-ExcelFileSchema function gets the schema of an Excel file by returning the property names of the first row of each worksheet in the file.
.PARAMETER Path
Specifies the path to the Excel file.
.PARAMETER Compress
Indicates whether to compress the json output.
.OUTPUTS
Json
.EXAMPLE
Get-ExcelFileSchema -Path .\example.xlsx
#>
[CmdletBinding()]
param(
[Parameter(ValueFromPipelineByPropertyName, Mandatory)]
[Alias('FullName')]
$Path,
[Switch]$Compress
)
Begin {
$result = @()
}
Process {
$excelFiles = Get-ExcelFileSummary $Path
foreach ($excelFile in $excelFiles) {
$data = Import-Excel $Path -WorksheetName $excelFile.WorksheetName | Select-Object -First 1
$names = $data[0].PSObject.Properties.name
$result += $excelFile | Add-Member -MemberType NoteProperty -Name "PropertyNames" -Value $names -PassThru
}
}
End {
$result | ConvertTo-Json -Compress:$Compress
}
}