Add-Table.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function Add-Table {
    <#
    .SYNOPSIS
        Add a table to an Excel worksheet
 
    .DESCRIPTION
        Add a table to an Excel worksheet
 
        Note:
            Each time you call this function, you need to save and re-create your Excel Object.
            If you attempt to modify the Excel object, save, modify, and save a second time, it will fail.
            See Save-Excel Passthru parameter for a workaround
 
    .PARAMETER Path
        Path to an xlsx file to add the table to
 
        If Path is specified and you do not use passthru, we save the file
 
    .PARAMETER Excel
        ExcelPackage to add the table to
 
        We do not save the ExcelPackage upon completion. See Save-Excel.
 
    .PARAMETER WorkSheetName
        If specified, use this worksheet as the source.
 
    .PARAMETER StartRow
        The top row for table data. If not specified, we use the dimensions start row
 
    .PARAMETER StartColumn
        The leftmost column for table data. If not specified, we use the dimensions start column
 
    .PARAMETER EndRow
        The bottom row for table data. If not specified, we use the dimensions' end row
 
    .PARAMETER EndColumn
        The rightmost column for table data. If not specified, we use the dimensions' end column
 
    .PARAMETER TableStyle
        Style of the table
 
    .PARAMETER TableName
        Name of the table, defaults to worksheet name if none provided
 
    .PARAMETER Passthru
        If specified, pass the ExcelPackage back
 
    .EXAMPLE
 
        Get-ChildItem C:\ -file | Export-XLSX -Path C:\temp\files.xlsx
 
        Add-Table -Path C:\Temp\files.xlsx -TableStyle Medium10
 
        # Get files, create an xlsx in C:\temp\ps.xlsx
 
        # Take existing xlsx and add a table with the Medium10 style
 
    .EXAMPLE
        # Create an xlsx.
            Get-ChildItem C:\ -file | Export-XLSX -Path C:\temp\files.xlsx
 
        # Open the excel file, add a table (this won't save), pass through the excel object, save.
            New-Excel -Path C:\temp\files.xlsx |
                Add-Table -TableStyle Medium10 -TableName "Files" -Passthru |
                Save-Excel -Close
 
    .NOTES
        Added by Andrew Widdersheim
 
    .LINK
        https://github.com/RamblingCookieMonster/PSExcel
 
    .FUNCTIONALITY
        Excel
    #>

    [OutputType([OfficeOpenXml.ExcelPackage])]
    [cmdletbinding(DefaultParameterSetName = 'Excel')]
    param(

        [parameter( Position = 0,
                    ParameterSetName = 'Excel',
                    Mandatory=$true,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$false)]
        [OfficeOpenXml.ExcelPackage]$Excel,

        [parameter( Position = 0,
                    ParameterSetName = 'File',
                    Mandatory=$true,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$false)]
        [validatescript({Test-Path $_})]
        [string]$Path,

        [parameter( Position = 1,
                    Mandatory=$false,
                    ValueFromPipeline=$false,
                    ValueFromPipelineByPropertyName=$false)]
        [string]$WorkSheetName,

        [parameter(
                    Mandatory=$false,
                    ValueFromPipeline=$false,
                    ValueFromPipelineByPropertyName=$false)]
        [int]$StartRow,

        [parameter(
                    Mandatory=$false,
                    ValueFromPipeline=$false,
                    ValueFromPipelineByPropertyName=$false)]
        [int]$StartColumn,

        [parameter(
                    Mandatory=$false,
                    ValueFromPipeline=$false,
                    ValueFromPipelineByPropertyName=$false)]
        [int]$EndRow,

        [parameter(
                    Mandatory=$false,
                    ValueFromPipeline=$false,
                    ValueFromPipelineByPropertyName=$false)]
        [int]$EndColumn,

        [OfficeOpenXml.Table.TableStyles]$TableStyle,

        [string]$TableName,

        [switch]$Passthru
    )
    Process
    {
        Write-Verbose "PSBoundParameters: $($PSBoundParameters | Out-String)"
        $SourceWS = @{}
        if($PSBoundParameters.ContainsKey( 'WorkSheetName') )
        {
            $SourceWS.Add('Name',$WorkSheetName)
        }

        Try
        {
            if($PSCmdlet.ParameterSetName -like 'File')
            {
                $Excel = New-Excel -Path $Path -ErrorAction Stop
            }

            $WorkSheets = @( $Excel | Get-Worksheet @SourceWS -ErrorAction Stop )
        }
        Catch
        {
            Throw "Could not get worksheets to search: $_"
        }

        If($WorkSheets.Count -eq 0)
        {
            Throw "Something went wrong, we didn't find a worksheet"
        }

        Foreach($SourceWorkSheet in $WorkSheets)
        {
            # Get the coordinates
                $dimension = $SourceWorkSheet.Dimension

                If(-not $StartRow)
                {
                    $StartRow = $dimension.Start.Row
                }
                If(-not $StartColumn)
                {
                    $StartColumn = $dimension.Start.Column
                }
                If(-not $EndRow)
                {
                    $EndRow = $dimension.End.Row
                }
                If(-not $EndColumn)
                {
                    $EndColumn = $dimension.End.Column
                }

                $Start = ConvertTo-ExcelCoordinate -Row $StartRow -Column $StartColumn
                $End = ConvertTo-ExcelCoordinate -Row $EndRow -Column $EndColumn
                $RangeCoordinates = "$Start`:$End"

                if(-not $TableName)
                {
                    $TableWorksheetName = $SourceWorkSheet.Name
                }
                else
                {
                    $TableWorksheetName = $TableName
                }

                Write-Verbose "Adding table over data range '$RangeCoordinates' with name $TableWorksheetName"
                $Table = $SourceWorkSheet.Tables.Add($SourceWorkSheet.Cells[$RangeCoordinates], $TableWorksheetName)

                if($TableStyle)
                {
                    Write-Verbose "Adding $TableStyle table style"
                    $Table.TableStyle = $TableStyle
                }

            if($PSCmdlet.ParameterSetName -like 'File' -and -not $Passthru)
            {
                Write-Verbose "Saving '$($Excel.File)'"
                $Excel.save()
                $Excel.Dispose()
            }
            if($Passthru)
            {
                $Excel
            }
        }
    }
}