@@ -16,8 +16,8 @@ Functions to create Excel spreadsheets/workbooks:
1616 SaveWorkbook([string]$filename)
1717 AddNewWorksheet([string]$tabname)
1818 AddWorksheetFromText([string[]]$text, [string]$tabname)
19- AddWorksheetFromCsvFile([string]$filename, [string]$tabname, [string]$CrLfEncoded)
20- AddWorksheetFromCsvData([string[]]$csv, [string]$tabname, [string]$CrLfEncoded)
19+ AddWorksheetFromCsvFile([string]$filename, [string]$tabname, [string]$CrLfEncoded, [switch]$AddChart )
20+ AddWorksheetFromCsvData([string[]]$csv, [string]$tabname, [string]$CrLfEncoded, [switch]$AddChart )
2121 CreateExcelFromCsvFile([string]$filename, [string]$tabname, [string]$CrLfEncoded, [string]$saveAsName)
2222
2323Function to determine whether a file is a Win32 EXE, a Win32 DLL, or neither
@@ -167,7 +167,7 @@ function AddWorksheetFromText([string[]]$text, [string]$tabname)
167167}
168168
169169# Add a new named worksheet from CSV data in the specified file, optionally replacing encoded CrLf with CrLf.
170- function AddWorksheetFromCsvFile ([string ]$filename , [string ]$tabname , [string ]$CrLfEncoded )
170+ function AddWorksheetFromCsvFile ([string ]$filename , [string ]$tabname , [string ]$CrLfEncoded , [ switch ] $AddChart )
171171{
172172 Write-Host " Populating tab `" $tabname `" ..." - ForegroundColor Cyan
173173
@@ -199,25 +199,29 @@ function AddWorksheetFromCsvFile([string]$filename, [string]$tabname, [string]$C
199199 $global :ExcelAppInstance.ActiveWindow.SplitColumn = 0
200200 $global :ExcelAppInstance.ActiveWindow.SplitRow = 1
201201 $global :ExcelAppInstance.ActiveWindow.FreezePanes = $true
202- $global :ExcelAppInstance.ActiveWindow.Zoom = 80
202+ # $global:ExcelAppInstance.ActiveWindow.Zoom = 80
203203
204204 $dummy = $worksheet.Range (" A2" ).Select()
205205
206206 # Formatting: autosize column widths, then set maximum width (except on last column)
207- $maxWidth = 40
207+ $maxWidth = 50
208208 $maxHeight = 120
209209
210210 $dummy = $worksheet.Cells.EntireColumn.AutoFit ()
211- $ix = 1
212- # Do this until the next to last column; don't set max width on the last column
213- while ( $worksheet.Cells (1 , $ix + 1 ).Text.Length -gt 0 )
211+ # Don't set max width if 3 columns or fewer
212+ if ($worksheet.UsedRange.Columns.Count -gt 3 )
214213 {
215- $cells = $worksheet.Cells (1 , $ix )
216- # Write-Host ($cells.Text + "; " + $cells.ColumnWidth)
217- if ($cells.ColumnWidth -gt $maxWidth ) { $cells.ColumnWidth = $maxWidth }
218- $ix ++
214+ $ix = 1
215+ # Do this until the next to last column; don't set max width on the last column
216+ while ( $worksheet.Cells (1 , $ix + 1 ).Text.Length -gt 0 )
217+ {
218+ $cells = $worksheet.Cells (1 , $ix )
219+ # Write-Host ($cells.Text + "; " + $cells.ColumnWidth)
220+ if ($cells.ColumnWidth -gt $maxWidth ) { $cells.ColumnWidth = $maxWidth }
221+ $ix ++
222+ }
219223 }
220-
224+
221225 # Formatting: autosize row heights, then set maximum height (if CrLf replacement on)
222226 $dummy = $worksheet.Cells.EntireRow.AutoFit ()
223227 # If line breaks added, limit autofit row height to
@@ -233,20 +237,77 @@ function AddWorksheetFromCsvFile([string]$filename, [string]$tabname, [string]$C
233237 }
234238 }
235239
240+ if ($AddChart )
241+ {
242+ # If lots of input data, limit number of entries in chart
243+ $chartLimit = 20
244+
245+ $oShape = $worksheet.Shapes.AddChart2 (216 , 57 )
246+ # Have to cast from double to single to avoid runtime errors
247+ $oShape.Left = [System.Single ]($worksheet.Range (" D2" ).Left)
248+ $oShape.Top = [System.Single ]($worksheet.Range (" D2" ).Top)
249+ $oShape.Visible = $true
250+
251+ $oChartObject = $worksheet.ChartObjects (1 )
252+ $oChart = $oChartObject.Chart
253+ $rowCount = $worksheet.UsedRange.Rows.Count
254+ $oChart.ChartTitle.Text = $tabname
255+ if ($rowCount -le $chartLimit )
256+ {
257+ # Use whatever data is there
258+ $oChart.SetSourceData ($worksheet.UsedRange , 2 ) # 2 = xlColumns
259+ }
260+ else
261+ {
262+ # Build chart from top $chartLimit entries, then sum the rest into "Others"
263+ $sSeries1 = [System.Text.StringBuilder ]::new()
264+ $sSeries2 = [System.Text.StringBuilder ]::new()
265+ 2 .. ($chartLimit + 1 ) | ForEach-Object {
266+ if ($_ -gt 2 ) {
267+ [void ]$sSeries1.Append (" ," )
268+ [void ]$sSeries2.Append (" ," )
269+ }
270+ [void ]$sSeries1.Append (" `" " + $worksheet.Range (' $A' + $_ ).Text + " `" " )
271+ [void ]$sSeries2.Append ($worksheet.Range (' $B' + $_ ).Text)
272+ }
273+ [void ]$sSeries1.Append (" ,`" Others`" " )
274+ [void ]$sSeries2.Append (" ," + $global :ExcelAppInstance.WorksheetFunction.Sum ($worksheet.Range (' $B' + ($chartLimit + 2 ) + " :B" + $rowCount )))
275+ $sSeries =
276+ " =SERIES(`" " + $worksheet.Range (' $B1' ).Text + " `" ," +
277+ " {" + $sSeries1.ToString () + " },{" + $sSeries2.ToString () + " },1)"
278+ # Write-Host $sSeries -ForegroundColor Green
279+ $oChart.SeriesCollection (1 ).Formula = $sSeries
280+ # $oChart.SetSourceData($worksheet.Range('$A$1:$B$' + ($chartLimit + 1).ToString()), 2) # 2 = xlColumns
281+ # $oChart.ChartTitle.Text = "Top " + $chartLimit.ToString() + " " + $tabname
282+ $oShape.Height = $oShape.Height * 2
283+ }
284+
285+ $oAxes = $oChart.Axes (1 )
286+ $oAxes.ReversePlotOrder = $true
287+ $oAxes.TickLabelSpacing = 1
288+
289+ $dummy = $worksheet.Range (" A2" ).Select()
290+
291+ # Release COM interface references
292+ $dummy = [System.Runtime.Interopservices.Marshal ]::ReleaseComObject($oAxes )
293+ $dummy = [System.Runtime.Interopservices.Marshal ]::ReleaseComObject($oChartObject )
294+ $dummy = [System.Runtime.Interopservices.Marshal ]::ReleaseComObject($oShape )
295+ }
296+
236297 # Release COM interface references
237298 $dummy = [System.Runtime.Interopservices.Marshal ]::ReleaseComObject($query )
238299 $dummy = [System.Runtime.Interopservices.Marshal ]::ReleaseComObject($Connector )
239300 $dummy = [System.Runtime.Interopservices.Marshal ]::ReleaseComObject($worksheet )
240301}
241302
242303# Add a new named worksheet from in-memory CSV data (string array), optionally replacing encoded CrLf with CrLf.
243- function AddWorksheetFromCsvData ([string []]$csv , [string ]$tabname , [string ]$CrLfEncoded )
304+ function AddWorksheetFromCsvData ([string []]$csv , [string ]$tabname , [string ]$CrLfEncoded , [ switch ] $AddChart )
244305{
245306 Write-Host " Preparing data for tab `" $tabname `" ..." - ForegroundColor Cyan
246307
247308 if ($null -eq $global :ExcelAppInstance ) { return $null }
248309
249- if ($null -ne $csv )
310+ if ($null -ne $csv -and $csv .Length -gt 0 )
250311 {
251312 $OutputEncodingPrevious = $OutputEncoding
252313 $OutputEncoding = [System.Text.ASCIIEncoding ]::Unicode
@@ -255,7 +316,7 @@ function AddWorksheetFromCsvData([string[]]$csv, [string]$tabname, [string]$CrLf
255316
256317 $csv | Out-File $tempfile - Encoding unicode
257318
258- AddWorksheetFromCsvFile - filename $tempfile - tabname $tabname - CrLfEncoded $CrLfEncoded
319+ AddWorksheetFromCsvFile - filename $tempfile - tabname $tabname - CrLfEncoded $CrLfEncoded - AddChart: $AddChart
259320
260321 Remove-Item $tempfile
261322
@@ -428,3 +489,6 @@ Set-Variable -Name NeverExecutableExts -Option Constant -Value `
428489 " .zip" , " .7z" , " .tar" ,
429490 " .wav" , " .wmv" , " .mp3" , " .mp4" , " .mpg" , " .mpeg" , " .avi" , " .mov"
430491
492+ Set-Variable - Name sNoPublisher - Option Constant - Value " -"
493+ Set-Variable - Name sUnsigned - Option Constant - Value " [not signed]"
494+ Set-Variable - Name sFiltered - Option Constant - Value " FILTERED"
0 commit comments