From 652255ca9d7910e3fcdd89113f44e66601d76804 Mon Sep 17 00:00:00 2001 From: dfinke Date: Fri, 18 Dec 2015 12:58:42 -0500 Subject: [PATCH] Initial commit [charting] --- Charting.ps1 | 49 +++++++++++++++++++++ Get-ExcelColumnName.ps1 | 22 ++++++++++ Get-XYRange.ps1 | 26 +++++++++++ ImportExcel.psm1 | 6 +++ InferData.ps1 | 95 +++++++++++++++++++++++++++++++++++++++++ Install.ps1 | 2 +- Invoke-Sum.ps1 | 17 ++++++++ New-PSItem.ps1 | 23 ++++++++++ 8 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 Charting.ps1 create mode 100644 Get-ExcelColumnName.ps1 create mode 100644 Get-XYRange.ps1 create mode 100644 InferData.ps1 create mode 100644 Invoke-Sum.ps1 create mode 100644 New-PSItem.ps1 diff --git a/Charting.ps1 b/Charting.ps1 new file mode 100644 index 0000000..95d841d --- /dev/null +++ b/Charting.ps1 @@ -0,0 +1,49 @@ +function DoChart { + param( + $targetData, + $title, + $chartType + ) + + if($targetData[0] -is [System.ValueType]) { + $chart = New-ExcelChart -YRange "A1:A$($targetData.count)" -Title $title -ChartType $chartType + } else { + $xyRange = Get-XYRange $targetData + + $X = $xyRange.XRange.ExcelColumn + $XRange = "{0}2:{0}{1}" -f $X,($targetData.count+1) + + $Y = $xyRange.YRange.ExcelColumn + $YRange = "{0}2:{0}{1}" -f $Y,($targetData.count+1) + + $chart = New-ExcelChart -XRange $xRange -YRange $yRange -Title $title -ChartType $chartType + } + + $xlFile = (New-TemporaryFile).fullname -replace "tmp","xlsx" + $targetData | Export-Excel $xlFile -ExcelChartDefinition $chart -Show -AutoSize + + } + + function BarChart { + param($targetData,$title) + + DoChart $targetData $title BarStacked + } + + function PieChart { + param($targetData,$title) + + DoChart $targetData $title Pie + } + + function LineChart { + param($targetData,$title) + + DoChart $targetData $title Line + } + + function ColumnChart { + param($targetData,$title) + + DoChart $targetData $title ColumnStacked + } diff --git a/Get-ExcelColumnName.ps1 b/Get-ExcelColumnName.ps1 new file mode 100644 index 0000000..7562391 --- /dev/null +++ b/Get-ExcelColumnName.ps1 @@ -0,0 +1,22 @@ +function Get-ExcelColumnName { + param( + [Parameter(ValueFromPipeline=$true)] + $columnNumber=1 + ) + + Process { + $dividend = $columnNumber + $columnName = @() + while($dividend -gt 0) { + $modulo = ($dividend - 1) % 26 + $columnName += [char](65 + $modulo) + $dividend = [int](($dividend -$modulo)/26) + } + + [PSCustomObject] @{ + ColumnNumber = $columnNumber + ColumnName = $columnName -join '' + } + + } +} \ No newline at end of file diff --git a/Get-XYRange.ps1 b/Get-XYRange.ps1 new file mode 100644 index 0000000..179679d --- /dev/null +++ b/Get-XYRange.ps1 @@ -0,0 +1,26 @@ +function Get-XYRange { + param($targetData) + + $record = $targetData| select -First 1 + $p=$record.psobject.Properties.name + + $infer = for ($idx = 0; $idx -lt $p.Count; $idx++) { + + $name = $p[$idx] + $value = $record.$name + + $result=Invoke-AllTests $value -OnlyPassing -FirstOne + + [PSCustomObject]@{ + Name = $name + Value = $value + DataType = $result.DataType + ExcelColumn = (Get-ExcelColumnName ($idx+1)).ColumnName + } + } + + [PSCustomObject]@{ + XRange = $infer | ? {$_.datatype -match 'string'} | select -First 1 excelcolumn, name + YRange = $infer | ? {$_.datatype -match 'int|double'} |select -First 1 excelcolumn, name + } +} \ No newline at end of file diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 068e779..2df9e40 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -4,6 +4,12 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" . $PSScriptRoot\New-ConditionalFormattingIconSet.ps1 . $PSScriptRoot\Export-ExcelSheet.ps1 . $PSScriptRoot\New-ExcelChart.ps1 +. $PSScriptRoot\Invoke-Sum.ps1 +. $PSScriptRoot\InferData.ps1 +. $PSScriptRoot\Get-ExcelColumnName.ps1 +. $PSScriptRoot\Get-XYRange.ps1 +. $PSScriptRoot\Charting.ps1 +. $PSScriptRoot\New-PSItem.ps1 function Import-Excel { param( diff --git a/InferData.ps1 b/InferData.ps1 new file mode 100644 index 0000000..add8f04 --- /dev/null +++ b/InferData.ps1 @@ -0,0 +1,95 @@ +function Test-String{ + param($p) + + [PSCustomObject]@{ + Test=$p -is [string] + DataType = "string" + } +} + +function Test-Date { + param($p) + + [datetime]$result = [datetime]::MinValue + + [PSCustomObject]@{ + Test=[datetime]::TryParse($p, [ref]$result) + DataType = "datetime" + } +} + +function Test-Boolean { + param($p) + + #[bool]$result = [bool]::FalseString + [bool]$result = $false + + [PSCustomObject]@{ + Test=[bool]::TryParse($p, [ref]$result) + DataType = "bool" + } +} + +function Test-Number { + param($p) + + [double]$result = [double]::MinValue + + [PSCustomObject]@{ + Test=[double]::TryParse($p, [ref]$result) + DataType = "double" + } +} + +function Test-Integer { + param($p) + + [int]$result = [int]::MinValue + + [PSCustomObject]@{ + Test=[int]::TryParse($p, [ref]$result) + DataType = "int" + } +} + +$tests = [ordered]@{ + TestBoolean = Get-Command Test-Boolean + TestInteger = Get-Command Test-Integer + TestNumber = Get-Command Test-Number + TestDate = Get-Command Test-Date + TestString = Get-Command Test-String +} + +function Invoke-AllTests { + param( + $target, + [Switch]$OnlyPassing, + [Switch]$FirstOne + ) + + $resultCount=0 + $tests.GetEnumerator() | ForEach { + + $result=& $_.Value $target + + $testResult = [PSCustomObject]@{ + Test = $_.Key + Target = $target + Result = $result.Test + DataType= $result.DataType + } + + if(!$OnlyPassing) { + $testResult + } elseif ($result.Test -eq $true) { + if($FirstOne) { + if($resultCount -ne 1) { + $testResult + $resultCount+=1 + } + } else { + $testResult + } + } + } +} \ No newline at end of file diff --git a/Install.ps1 b/Install.ps1 index 9bd6e4a..874dc6a 100644 --- a/Install.ps1 +++ b/Install.ps1 @@ -1,6 +1,6 @@ param([string]$InstallDirectory) -$fileList = echo EPPlus.dll ImportExcel.psd1 ImportExcel.psm1 Export-Excel.ps1 New-ConditionalFormattingIconSet.ps1 Export-ExcelSheet.ps1 New-ExcelChart.ps1 +$fileList = echo EPPlus.dll ImportExcel.psd1 ImportExcel.psm1 Export-Excel.ps1 New-ConditionalFormattingIconSet.ps1 Export-ExcelSheet.ps1 New-ExcelChart.ps1 Invoke-Sum.ps1 InferData.ps1 Get-ExcelColumnName.ps1 Get-XYRange.ps1 Charting.ps1 New-PSItem.ps1 if ('' -eq $InstallDirectory) { diff --git a/Invoke-Sum.ps1 b/Invoke-Sum.ps1 new file mode 100644 index 0000000..78570d1 --- /dev/null +++ b/Invoke-Sum.ps1 @@ -0,0 +1,17 @@ +function Invoke-Sum { + param($data,$dimension,$measure) + + $h=@{} + + foreach ($item in $data){ + $h.$($item.$dimension)+=$item.$measure + } + + foreach ($entry in $h.GetEnumerator()){ + [PSCustomObject]@{ + Name=$entry.key + $measure=$entry.value + } + } + +} \ No newline at end of file diff --git a/New-PSItem.ps1 b/New-PSItem.ps1 new file mode 100644 index 0000000..b186dcc --- /dev/null +++ b/New-PSItem.ps1 @@ -0,0 +1,23 @@ +function New-PSItem { + + $totalArgs = $args.Count + + if($args[-1] -is [array]) { + $script:PSItemHeader=$args[-1] + $totalArgs-=1 + } + + $h=[ordered]@{} + + for ($idx = 0; $idx -lt $totalArgs; $idx+=1) { + if($PSItemHeader) { + $key = $PSItemHeader[$idx] + } else { + $key = "P$($idx+1)" + } + + $h.$key=$args[$idx] + } + + [PSCustomObject]$h +} \ No newline at end of file