mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
formatted
This commit is contained in:
@@ -194,10 +194,10 @@ Function Export-Excel {
|
|||||||
https://github.com/dfinke/ImportExcel
|
https://github.com/dfinke/ImportExcel
|
||||||
#>
|
#>
|
||||||
|
|
||||||
[CmdletBinding(DefaultParameterSetName='Default')]
|
[CmdletBinding(DefaultParameterSetName = 'Default')]
|
||||||
Param(
|
Param(
|
||||||
$Path,
|
$Path,
|
||||||
[Parameter(ValueFromPipeline=$true)]
|
[Parameter(ValueFromPipeline = $true)]
|
||||||
$TargetData,
|
$TargetData,
|
||||||
[String]$WorkSheetname = 'Sheet1',
|
[String]$WorkSheetname = 'Sheet1',
|
||||||
[String]$Title,
|
[String]$Title,
|
||||||
@@ -223,12 +223,12 @@ Function Export-Excel {
|
|||||||
[Switch]$FreezeFirstColumn,
|
[Switch]$FreezeFirstColumn,
|
||||||
[Switch]$FreezeTopRowFirstColumn,
|
[Switch]$FreezeTopRowFirstColumn,
|
||||||
[Int[]]$FreezePane,
|
[Int[]]$FreezePane,
|
||||||
[Parameter(ParameterSetName='Default')]
|
[Parameter(ParameterSetName = 'Default')]
|
||||||
[Switch]$AutoFilter,
|
[Switch]$AutoFilter,
|
||||||
[Switch]$BoldTopRow,
|
[Switch]$BoldTopRow,
|
||||||
[Switch]$NoHeader,
|
[Switch]$NoHeader,
|
||||||
[String]$RangeName,
|
[String]$RangeName,
|
||||||
[ValidateScript({
|
[ValidateScript( {
|
||||||
if ($_.Contains(' ')) {
|
if ($_.Contains(' ')) {
|
||||||
throw 'Tablename has spaces.'
|
throw 'Tablename has spaces.'
|
||||||
}
|
}
|
||||||
@@ -242,9 +242,9 @@ Function Export-Excel {
|
|||||||
$true
|
$true
|
||||||
}
|
}
|
||||||
})]
|
})]
|
||||||
[Parameter(ParameterSetName='Table')]
|
[Parameter(ParameterSetName = 'Table')]
|
||||||
[String]$TableName,
|
[String]$TableName,
|
||||||
[Parameter(ParameterSetName='Table')]
|
[Parameter(ParameterSetName = 'Table')]
|
||||||
[OfficeOpenXml.Table.TableStyles]$TableStyle = 'Medium6',
|
[OfficeOpenXml.Table.TableStyles]$TableStyle = 'Medium6',
|
||||||
[Object[]]$ExcelChartDefinition,
|
[Object[]]$ExcelChartDefinition,
|
||||||
[String[]]$HideSheet,
|
[String[]]$HideSheet,
|
||||||
@@ -362,7 +362,7 @@ Function Export-Excel {
|
|||||||
|
|
||||||
$R = $null
|
$R = $null
|
||||||
|
|
||||||
if ([Double]::TryParse([String]$Value,[System.Globalization.NumberStyles]::Any,
|
if ([Double]::TryParse([String]$Value, [System.Globalization.NumberStyles]::Any,
|
||||||
[System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$R)) {
|
[System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$R)) {
|
||||||
$R
|
$R
|
||||||
}
|
}
|
||||||
@@ -386,11 +386,10 @@ Function Export-Excel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($Now) {
|
if ($Now) {
|
||||||
$Path = [System.IO.Path]::GetTempFileName() -replace '\.tmp','.xlsx'
|
$Path = [System.IO.Path]::GetTempFileName() -replace '\.tmp', '.xlsx'
|
||||||
$Show = $true
|
$Show = $true
|
||||||
$AutoSize = $true
|
$AutoSize = $true
|
||||||
if (!$TableName)
|
if (!$TableName) {
|
||||||
{
|
|
||||||
$AutoFilter = $true
|
$AutoFilter = $true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -491,11 +490,11 @@ Function Export-Excel {
|
|||||||
$totalRows = $ws.Dimension.Rows
|
$totalRows = $ws.Dimension.Rows
|
||||||
$totalColumns = $ws.Dimension.Columns
|
$totalColumns = $ws.Dimension.Columns
|
||||||
|
|
||||||
foreach($c in 0..($totalColumns-1)) {
|
foreach ($c in 0..($totalColumns - 1)) {
|
||||||
$targetRangeName = "$($script:Header[$c])"
|
$targetRangeName = "$($script:Header[$c])"
|
||||||
|
|
||||||
$targetColumn = $c+1
|
$targetColumn = $c + 1
|
||||||
$theCell = $ws.Cells[2,$targetColumn,$totalRows,$targetColumn ]
|
$theCell = $ws.Cells[2, $targetColumn, $totalRows, $targetColumn ]
|
||||||
$ws.Names.Add($targetRangeName, $theCell) | Out-Null
|
$ws.Names.Add($targetRangeName, $theCell) | Out-Null
|
||||||
|
|
||||||
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress($targetRangeName)) {
|
if ([OfficeOpenXml.FormulaParsing.ExcelUtilities.ExcelAddressUtil]::IsValidAddress($targetRangeName)) {
|
||||||
@@ -528,7 +527,7 @@ Function Export-Excel {
|
|||||||
$cer = $ws.Dimension.End.Row
|
$cer = $ws.Dimension.End.Row
|
||||||
$cec = $script:Header.Count
|
$cec = $script:Header.Count
|
||||||
|
|
||||||
$targetRange = $ws.Cells[$csr, $csc, $cer,$cec]
|
$targetRange = $ws.Cells[$csr, $csc, $cer, $cec]
|
||||||
$tbl = $ws.Tables.Add($targetRange, $TableName)
|
$tbl = $ws.Tables.Add($targetRange, $TableName)
|
||||||
$tbl.TableStyle = $TableStyle
|
$tbl.TableStyle = $TableStyle
|
||||||
}
|
}
|
||||||
@@ -539,7 +538,7 @@ Function Export-Excel {
|
|||||||
|
|
||||||
$wsPivot.View.TabSelected = $true
|
$wsPivot.View.TabSelected = $true
|
||||||
|
|
||||||
$pivotTableDataName=$WorkSheetname + 'PivotTableData'
|
$pivotTableDataName = $WorkSheetname + 'PivotTableData'
|
||||||
|
|
||||||
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells['A1'], $ws.Cells[$dataRange], $pivotTableDataName)
|
$pivotTable = $wsPivot.PivotTables.Add($wsPivot.Cells['A1'], $ws.Cells[$dataRange], $pivotTableDataName)
|
||||||
|
|
||||||
@@ -592,29 +591,29 @@ Function Export-Excel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($AutoFilter) {
|
if ($AutoFilter) {
|
||||||
$ws.Cells[$dataRange].AutoFilter=$true
|
$ws.Cells[$dataRange].AutoFilter = $true
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($FreezeTopRow) {
|
if ($FreezeTopRow) {
|
||||||
$ws.View.FreezePanes(2,1)
|
$ws.View.FreezePanes(2, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($FreezeTopRowFirstColumn) {
|
if ($FreezeTopRowFirstColumn) {
|
||||||
$ws.View.FreezePanes(2,2)
|
$ws.View.FreezePanes(2, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($FreezeFirstColumn) {
|
if ($FreezeFirstColumn) {
|
||||||
$ws.View.FreezePanes(1,2)
|
$ws.View.FreezePanes(1, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($FreezePane) {
|
if ($FreezePane) {
|
||||||
$freezeRow,$freezeColumn=$FreezePane
|
$freezeRow, $freezeColumn = $FreezePane
|
||||||
if (-not $freezeColumn -or $freezeColumn -eq 0) {
|
if (-not $freezeColumn -or $freezeColumn -eq 0) {
|
||||||
$freezeColumn=1
|
$freezeColumn = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($freezeRow -gt 1) {
|
if ($freezeRow -gt 1) {
|
||||||
$ws.View.FreezePanes($freezeRow,$freezeColumn)
|
$ws.View.FreezePanes($freezeRow, $freezeColumn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($BoldTopRow) {
|
if ($BoldTopRow) {
|
||||||
@@ -635,9 +634,9 @@ Function Export-Excel {
|
|||||||
$pkg.Workbook.WorkSheets[$Sheet].Hidden = 'Hidden'
|
$pkg.Workbook.WorkSheets[$Sheet].Hidden = 'Hidden'
|
||||||
}
|
}
|
||||||
|
|
||||||
$chartCount=0
|
$chartCount = 0
|
||||||
foreach ($chartDef in $ExcelChartDefinition) {
|
foreach ($chartDef in $ExcelChartDefinition) {
|
||||||
$ChartName = 'Chart' + (Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.',''
|
$ChartName = 'Chart' + (Split-Path -Leaf ([System.IO.path]::GetTempFileName())) -replace 'tmp|\.', ''
|
||||||
$chart = $ws.Drawings.AddChart($ChartName, $chartDef.ChartType)
|
$chart = $ws.Drawings.AddChart($ChartName, $chartDef.ChartType)
|
||||||
$chart.Title.Text = $chartDef.Title
|
$chart.Title.Text = $chartDef.Title
|
||||||
|
|
||||||
@@ -650,22 +649,23 @@ Function Export-Excel {
|
|||||||
$chart.Datalabel.ShowPercent = $chartDef.ShowPercent
|
$chart.Datalabel.ShowPercent = $chartDef.ShowPercent
|
||||||
}
|
}
|
||||||
|
|
||||||
$chart.SetPosition($chartDef.Row, $chartDef.RowOffsetPixels,$chartDef.Column, $chartDef.ColumnOffsetPixels)
|
$chart.SetPosition($chartDef.Row, $chartDef.RowOffsetPixels, $chartDef.Column, $chartDef.ColumnOffsetPixels)
|
||||||
$chart.SetSize($chartDef.Width, $chartDef.Height)
|
$chart.SetSize($chartDef.Width, $chartDef.Height)
|
||||||
|
|
||||||
$chartDefCount = @($chartDef.YRange).Count
|
$chartDefCount = @($chartDef.YRange).Count
|
||||||
if ($chartDefCount -eq 1) {
|
if ($chartDefCount -eq 1) {
|
||||||
$Series=$chart.Series.Add($chartDef.YRange, $chartDef.XRange)
|
$Series = $chart.Series.Add($chartDef.YRange, $chartDef.XRange)
|
||||||
|
|
||||||
$SeriesHeader=$chartDef.SeriesHeader
|
$SeriesHeader = $chartDef.SeriesHeader
|
||||||
if (-not $SeriesHeader) {
|
if (-not $SeriesHeader) {
|
||||||
$SeriesHeader = 'Series 1'
|
$SeriesHeader = 'Series 1'
|
||||||
}
|
}
|
||||||
|
|
||||||
$Series.Header = $SeriesHeader
|
$Series.Header = $SeriesHeader
|
||||||
} else {
|
}
|
||||||
for($idx = 0; $idx -lt $chartDefCount; $idx += 1) {
|
else {
|
||||||
$Series=$chart.Series.Add($chartDef.YRange[$idx], $chartDef.XRange)
|
for ($idx = 0; $idx -lt $chartDefCount; $idx += 1) {
|
||||||
|
$Series = $chart.Series.Add($chartDef.YRange[$idx], $chartDef.XRange)
|
||||||
|
|
||||||
if ($chartDef.SeriesHeader.Count -gt 0) {
|
if ($chartDef.SeriesHeader.Count -gt 0) {
|
||||||
$SeriesHeader = $chartDef.SeriesHeader[$idx]
|
$SeriesHeader = $chartDef.SeriesHeader[$idx]
|
||||||
@@ -685,12 +685,12 @@ Function Export-Excel {
|
|||||||
foreach ($targetConditionalText in $ConditionalText) {
|
foreach ($targetConditionalText in $ConditionalText) {
|
||||||
$target = "Add$($targetConditionalText.ConditionalType)"
|
$target = "Add$($targetConditionalText.ConditionalType)"
|
||||||
|
|
||||||
$Range=$targetConditionalText.Range
|
$Range = $targetConditionalText.Range
|
||||||
if (-not $Range) {
|
if (-not $Range) {
|
||||||
$Range = $ws.Dimension.Address
|
$Range = $ws.Dimension.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
$rule=($ws.Cells[$Range].ConditionalFormatting).PSObject.Methods[$target].Invoke()
|
$rule = ($ws.Cells[$Range].ConditionalFormatting).PSObject.Methods[$target].Invoke()
|
||||||
|
|
||||||
if ($targetConditionalText.Text) {
|
if ($targetConditionalText.Text) {
|
||||||
if ($targetConditionalText.ConditionalType -match 'equal|notequal|lessthan|lessthanorequal|greaterthan|greaterthanorequal') {
|
if ($targetConditionalText.ConditionalType -match 'equal|notequal|lessthan|lessthanorequal|greaterthan|greaterthanorequal') {
|
||||||
|
|||||||
Reference in New Issue
Block a user