From b777c06838907ccd59f91626c567ba8468ca162d Mon Sep 17 00:00:00 2001 From: dfinke Date: Fri, 18 Mar 2016 09:36:18 -0400 Subject: [PATCH] Added `Get-Range`, `New-Plot` and Plot Cos example --- Examples/Plot/PlotCos.ps1 | 4 + Get-Range.ps1 | 3 + ImportExcel.psm1 | 4 + Install.ps1 | 6 +- InstallModule.ps1 | 4 +- README.md | 5 ++ plot.ps1 | 152 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 175 insertions(+), 3 deletions(-) create mode 100644 Examples/Plot/PlotCos.ps1 create mode 100644 Get-Range.ps1 create mode 100644 plot.ps1 diff --git a/Examples/Plot/PlotCos.ps1 b/Examples/Plot/PlotCos.ps1 new file mode 100644 index 0000000..aec098a --- /dev/null +++ b/Examples/Plot/PlotCos.ps1 @@ -0,0 +1,4 @@ +$plt = New-Plot +$plt.Plot((Get-Range 0 5 .02|%{[math]::Cos(2*[math]::pi*$_)})) +$plt.SetChartSize(800,300) +$plt.Show() \ No newline at end of file diff --git a/Get-Range.ps1 b/Get-Range.ps1 new file mode 100644 index 0000000..e25db9c --- /dev/null +++ b/Get-Range.ps1 @@ -0,0 +1,3 @@ +function Get-Range ($start=0,$stop,$step=1) { + for ($idx = $start; $idx -lt $stop; $idx+=$step) {$idx} +} \ No newline at end of file diff --git a/ImportExcel.psm1 b/ImportExcel.psm1 index 45e7b62..17ac457 100644 --- a/ImportExcel.psm1 +++ b/ImportExcel.psm1 @@ -15,6 +15,10 @@ Add-Type -Path "$($PSScriptRoot)\EPPlus.dll" . $PSScriptRoot\Get-ExcelSheetInfo.ps1 . $PSScriptRoot\Get-HtmlTable.ps1 . $PSScriptRoot\Import-Html.ps1 +. $PSScriptRoot\Get-Range.ps1 +. $PSScriptRoot\plot.ps1 + +function New-Plot { [psplot]::new() } function Import-Excel { param( diff --git a/Install.ps1 b/Install.ps1 index fda1852..f75302f 100644 --- a/Install.ps1 +++ b/Install.ps1 @@ -18,7 +18,9 @@ $fileList = echo ` New-ConditionalText.ps1 ` Get-HtmlTable.ps1 ` Import-Html.ps1 ` - Get-ExcelSheetInfo.ps1 + Get-ExcelSheetInfo.ps1 ` + Get-Range.ps1 ` + plot.ps1 if ('' -eq $InstallDirectory) { @@ -43,4 +45,4 @@ $wc = New-Object System.Net.WebClient $fileList | ForEach-Object { $wc.DownloadFile("https://raw.github.com/dfinke/ImportExcel/master/$_","$installDirectory\$_") - } + } \ No newline at end of file diff --git a/InstallModule.ps1 b/InstallModule.ps1 index 49ec83f..5eff7df 100644 --- a/InstallModule.ps1 +++ b/InstallModule.ps1 @@ -22,7 +22,9 @@ $targetFiles = echo ` Get-ExcelSheetInfo.ps1 ` New-ConditionalText.ps1 ` Get-HtmlTable.ps1 ` - Import-Html.ps1 + Import-Html.ps1 ` + Get-Range.ps1 ` + plot.ps1 ls $targetFiles | ForEach { diff --git a/README.md b/README.md index d9ac488..f21d0d0 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,11 @@ iex (new-object System.Net.WebClient).DownloadString('https://raw.github.com/dfi # What's new +#### [ADD DATE] +* Added `Get-Range`, `New-Plot` and Plot Cos example +* Updated EPPlus DLL. Allows markers to be changed and colored +* Handles and warns if auto name range names are also valid Excel ranges + #### 3/7/2016 * Added `Header` and `FirstDataRow` for `Import-Html` diff --git a/plot.ps1 b/plot.ps1 new file mode 100644 index 0000000..f04d629 --- /dev/null +++ b/plot.ps1 @@ -0,0 +1,152 @@ +class PSPlot { + hidden $path + hidden $pkg + hidden $ws + hidden $chart + + PSPlot() { + $this.path=[System.IO.Path]::GetTempFileName() -replace "\.tmp", ".xlsx" + $this.pkg = New-Object OfficeOpenXml.ExcelPackage $this.path + $this.ws=$this.pkg.Workbook.Worksheets.Add("plot") + } + + Plot($yValues) { + + $this.NewChart() + + $xValues = 0..$yValues.Count + + $xCol = 'A' + $yCol = 'B' + + $this.AddDataToSheet($xCol,$yCol,'x','y',$xValues,$yValues) + $this.AddSeries($xCol,$yCol,$yValues) + $this.SetChartPosition($yCol) + } + + Plot($xValues,$yValues) { + + $this.NewChart() + + $xCol = 'A' + $yCol = 'B' + + $this.AddDataToSheet($xCol,$yCol,'x','y',$xValues,$yValues) + $this.AddSeries($xCol,$yCol,$yValues) + $this.SetChartPosition($yCol) + } + + Plot($xValues,$yValues,$x1Values,$y1Values) { + + $this.NewChart() + + $xCol = 'A' + $yCol = 'B' + + $this.AddDataToSheet($xCol,$yCol,'x','y',$xValues,$yValues) + $this.AddSeries($xCol,$yCol,$yValues) + + $xCol=$this.GetNextColumnName($yCol) + $yCol=$this.GetNextColumnName($xCol) + + $this.AddDataToSheet($xCol,$yCol,'x1','y1',$x1Values,$y1Values) + $this.AddSeries($xCol,$yCol,$y1Values) + + $this.SetChartPosition($yCol) + } + + Plot($xValues,$yValues,$x1Values,$y1Values,$x2Values,$y2Values) { + + $this.NewChart() + + $xCol = 'A' + $yCol = 'B' + + $this.AddDataToSheet($xCol,$yCol,'x','y',$xValues,$yValues) + $this.AddSeries($xCol,$yCol,$yValues) + + $xCol=$this.GetNextColumnName($yCol) + $yCol=$this.GetNextColumnName($xCol) + + $this.AddDataToSheet($xCol,$yCol,'x1','y1',$x1Values,$y1Values) + $this.AddSeries($xCol,$yCol,$y1Values) + + $xCol=$this.GetNextColumnName($yCol) + $yCol=$this.GetNextColumnName($xCol) + + $this.AddDataToSheet($xCol,$yCol,'x2','y2',$x2Values,$y2Values) + $this.AddSeries($xCol,$yCol,$y2Values) + + $this.SetChartPosition($yCol) + } + + SetChartPosition($yCol) { + $columnNumber = $this.GetColumnNumber($yCol)+1 + $this.chart.SetPosition(1,0,$columnNumber,0) + } + + AddSeries($xCol,$yCol,$yValues) { + $yRange = "{0}2:{0}{1}" -f $yCol,($yValues.Count+1) + $xRange = "{0}2:{0}{1}" -f $xCol,($yValues.Count+1) + $Series=$this.chart.Series.Add($yRange,$xRange) + } + + hidden [string]GetNextColumnName($columnName) { + return $this.GetColumnName($this.GetColumnNumber($columnName)+1) + } + + hidden [int]GetColumnNumber($columnName) { + $sum=0 + + $columnName.ToCharArray() | + ForEach { + $sum*=26 + $sum+=[char]$_.tostring().toupper()-[char]'A'+1 + } + + return $sum + } + + hidden [string]GetColumnName($columnNumber) { + $dividend = $columnNumber + $columnName = @() + while($dividend -gt 0) { + $modulo = ($dividend - 1) % 26 + $columnName += [char](65 + $modulo) + $dividend = [int](($dividend -$modulo)/26) + } + + return ($columnName -join '') + } + + hidden AddDataToSheet($xColumn,$yColumn,$xHeader,$yHeader,$xValues,$yValues) { + $count=$yValues.Count + $this.ws.Cells["$($xColumn)1"].Value=$xHeader + $this.ws.Cells["$($yColumn)1"].Value=$yHeader + + for ($idx= 0; $idx-lt $count; $idx++) { + $row=$idx+2 + $this.ws.Cells["$($xColumn)$($row)"].Value=$xValues[$idx] + $this.ws.Cells["$($yColumn)$($row)"].Value=$yValues[$idx] + } + } + + hidden NewChart() { + $chartType="XYScatter" + #$chartType="line" + $this.chart=$this.ws.Drawings.AddChart("plot", $chartType) + $this.chart.Title.Text = 'Plot' + $this.chart.Legend.Remove() + $this.SetChartSize(300,300) + } + + SetChartSize([int]$width,[int]$height){ + $this.chart.SetSize($width, $height) + } + + Show() { + $this.pkg.Save() + $this.pkg.Dispose() + Invoke-Item $this.path + } +} \ No newline at end of file