mirror of
https://github.com/dfinke/ImportExcel.git
synced 2025-12-06 00:23:20 +00:00
Merge pull request #482 from jhoneill/master
Parameter clean up, improvements to Merge-MultipleSheets
This commit is contained in:
@@ -209,7 +209,7 @@
|
||||
#region set the rule conditions
|
||||
#for lessThan/GreaterThan/Equal/Between conditions make sure that strings are wrapped in quotes. Formulas should be passed with = which will be stripped.
|
||||
if ($RuleType -match "Than|Equal|Between" ) {
|
||||
if ($ConditionValue) {
|
||||
if ($PSBoundParameters.ContainsKey("ConditionValue" )) {
|
||||
$number = $Null
|
||||
#if the condition type is not a value type, but parses as a number, make it the number
|
||||
if ($ConditionValue -isnot [System.ValueType] -and [Double]::TryParse($ConditionValue, [System.Globalization.NumberStyles]::Any, [System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number) ) {
|
||||
@@ -219,7 +219,7 @@
|
||||
$ConditionValue = '"' + $ConditionValue +'"'
|
||||
}
|
||||
}
|
||||
if ($ConditionValue2) {
|
||||
if ($PSBoundParameters.ContainsKey("ConditionValue2")) {
|
||||
$number = $Null
|
||||
if ($ConditionValue -isnot [System.ValueType] -and [Double]::TryParse($ConditionValue2, [System.Globalization.NumberStyles]::Any, [System.Globalization.NumberFormatInfo]::CurrentInfo, [Ref]$number) ) {
|
||||
$ConditionValue2 = $number
|
||||
|
||||
@@ -412,7 +412,7 @@
|
||||
Runs a query against a SQL Server database and outputs the resulting rows DataRows using the -OutputAs parameter.
|
||||
The results are then piped to the Export-Excel function.
|
||||
NOTE: You need to install the SqlServer module from the PowerShell Gallery in oder to get the -OutputAs parameter for the Invoke-Sqlcmd cmdlet.
|
||||
|
||||
|
||||
.LINK
|
||||
https://github.com/dfinke/ImportExcel
|
||||
#>
|
||||
@@ -681,10 +681,10 @@
|
||||
$ws.Cells[$Row, $StartColumn].Value = $Title
|
||||
$ws.Cells[$Row, $StartColumn].Style.Font.Size = $TitleSize
|
||||
|
||||
if ($TitleBold) {
|
||||
if ($PSBoundParameters.ContainsKey("TitleBold")) {
|
||||
#Set title to Bold face font if -TitleBold was specified.
|
||||
#Otherwise the default will be unbolded.
|
||||
$ws.Cells[$Row, $StartColumn].Style.Font.Bold = $True
|
||||
$ws.Cells[$Row, $StartColumn].Style.Font.Bold = [boolean]$TitleBold
|
||||
}
|
||||
if ($TitleBackgroundColor ) {
|
||||
if ($TitleBackgroundColor -is [string]) {$TitleBackgroundColor = [System.Drawing.Color]::$TitleBackgroundColor }
|
||||
@@ -820,6 +820,7 @@
|
||||
}
|
||||
catch {Write-Warning -Message "Failed adding named ranges to worksheet '$WorksheetName': $_" }
|
||||
}
|
||||
#Empty string is not allowed as a name for ranges or tables.
|
||||
if ($RangeName) { Add-ExcelName -Range $ws.Cells[$dataRange] -RangeName $RangeName}
|
||||
|
||||
if ($TableName) {
|
||||
@@ -894,22 +895,22 @@
|
||||
$ws.View.FreezePanes(1, 2)
|
||||
Write-Verbose -Message "Froze first column"
|
||||
}
|
||||
|
||||
#Must be 1..maxrows or and array of 1..maxRows,1..MaxCols
|
||||
if ($FreezePane) {
|
||||
$freezeRow, $freezeColumn = $FreezePane
|
||||
if (-not $freezeColumn -or $freezeColumn -eq 0) {
|
||||
$freezeColumn = 1
|
||||
}
|
||||
|
||||
if ($freezeRow -gt 1) {
|
||||
if ($freezeRow -ge 1) {
|
||||
$ws.View.FreezePanes($freezeRow, $freezeColumn)
|
||||
Write-Verbose -Message "Froze pandes at row $freezeRow and column $FreezeColumn"
|
||||
Write-Verbose -Message "Froze panes at row $freezeRow and column $FreezeColumn"
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {Write-Warning -Message "Failed adding Freezing the panes in worksheet '$WorksheetName': $_"}
|
||||
|
||||
if ($BoldTopRow) { #it sets bold as far as there are populated cells: for whole row could do $ws.row($x).style.font.bold = $true
|
||||
if ($PSBoundParameters.ContainsKey("BoldTopRow")) { #it sets bold as far as there are populated cells: for whole row could do $ws.row($x).style.font.bold = $true
|
||||
try {
|
||||
if ($Title) {
|
||||
$range = $ws.Dimension.Address -replace '\d+', ($StartRow + 1)
|
||||
@@ -917,7 +918,7 @@
|
||||
else {
|
||||
$range = $ws.Dimension.Address -replace '\d+', $StartRow
|
||||
}
|
||||
$ws.Cells[$range].Style.Font.Bold = $true
|
||||
$ws.Cells[$range].Style.Font.Bold = [boolean]$BoldTopRow
|
||||
Write-Verbose -Message "Set $range font style to bold."
|
||||
}
|
||||
catch {Write-Warning -Message "Failed setting the top row to bold in worksheet '$WorksheetName': $_"}
|
||||
@@ -1040,6 +1041,7 @@
|
||||
catch {Write-Warning -Message "Failed processing CellStyleSB in worksheet '$WorksheetName': $_"}
|
||||
}
|
||||
|
||||
#Can only add password, may want to support -password $Null removing password.
|
||||
if ($Password) {
|
||||
try {
|
||||
$ws.Protection.SetPassword($Password)
|
||||
|
||||
@@ -63,7 +63,7 @@
|
||||
#Join-Worksheet assumes each sheet has identical headers and the headers should be copied to the target sheet, unless -NoHeader is specified.
|
||||
[switch]$NoHeader,
|
||||
#If -NoHeader is NOT specified, then rows of data will be labeled with the name of the sheet they came from. FromLabel is the header for this column. If it is null or empty, the labels will be omitted.
|
||||
$FromLabel = "From" ,
|
||||
[string]$FromLabel = "From" ,
|
||||
#If specified, the copied blocks of data will have the name of the sheet they were copied from inserted above them as a title.
|
||||
[switch]$LabelBlocks,
|
||||
#Sets the width of the Excel columns to display all the data in their cells.
|
||||
@@ -161,6 +161,7 @@
|
||||
#Assume every row has titles in row 1, copy row 1 from first sheet to new sheet.
|
||||
$destinationSheet.Select("A$row")
|
||||
$ExcelPackage.Workbook.Worksheets[1].cells["1:1"].Copy($destinationSheet.SelectedRange)
|
||||
#fromlabel can't be an empty string
|
||||
if ($FromLabel ) {
|
||||
#Add a column which says where the data comes from.
|
||||
$fromColumn = ($destinationSheet.Dimension.Columns + 1)
|
||||
|
||||
@@ -124,6 +124,8 @@
|
||||
)
|
||||
|
||||
#region Read Excel data
|
||||
if ($Differencefile -is [System.IO.FileInfo]) {$Differencefile = $Differencefile.FullName}
|
||||
if ($Referencefile -is [System.IO.FileInfo]) {$Referencefile = $Referencefile.FullName}
|
||||
if ($Referencefile -and $Differencefile) {
|
||||
#if the filenames don't resolve, give up now.
|
||||
try { $oneFile = ((Resolve-Path -Path $Referencefile -ErrorAction Stop).path -eq (Resolve-Path -Path $Differencefile -ErrorAction Stop).path)}
|
||||
@@ -375,9 +377,9 @@ Function Merge-MultipleSheets {
|
||||
[cmdletbinding()]
|
||||
#[Alias("Merge-MulipleSheets")] #There was a spelling error in the first release. This was there to ensure things didn't break but intelisense gave the alias first.
|
||||
param (
|
||||
#Paths to the files to be merged.
|
||||
#Paths to the files to be merged. Files are also accepted
|
||||
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
|
||||
[string[]]$Path ,
|
||||
$Path ,
|
||||
#The row from where we start to import data, all rows above the Start row are disregarded. By default this is the first row.
|
||||
[int]$Startrow = 1,
|
||||
|
||||
@@ -527,8 +529,8 @@ Function Merge-MultipleSheets {
|
||||
$sheet.Column($cell.start.Column).HIDDEN = $true
|
||||
}
|
||||
}
|
||||
|
||||
Close-ExcelPackage -ExcelPackage $excel -Show:$Show
|
||||
if ($Passthru) {$excel}
|
||||
else {Close-ExcelPackage -ExcelPackage $excel -Show:$Show}
|
||||
Write-Progress -Activity "Merging sheets" -Completed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,14 +401,16 @@ function Add-ExcelChart {
|
||||
}
|
||||
if ($NoLegend) { $chart.Legend.Remove() }
|
||||
else {
|
||||
if ($LegendPosition) {$Chart.Legend.Position = $LegendPosition}
|
||||
if ($LegendSize) {$chart.Legend.Font.Size = $LegendSize}
|
||||
if ($legendBold) {$chart.Legend.Font.Bold = $true}
|
||||
if ($PSBoundParameters.ContainsKey('LegendPosition')) {$chart.Legend.Position = $LegendPosition}
|
||||
if ($PSBoundParameters.ContainsKey('LegendBold')) {$chart.Legend.Font.Bold = [boolean]$LegendBold}
|
||||
if ($LegendSize) {$chart.Legend.Font.Size = $LegendSize}
|
||||
}
|
||||
|
||||
if ($XAxisTitleText) {
|
||||
$chart.XAxis.Title.Text = $XAxisTitleText
|
||||
if ($XAxisTitleBold) {$chart.XAxis.Title.Font.Bold = $true}
|
||||
if ($PSBoundParameters.ContainsKey('XAxisTitleBold')) {
|
||||
$chart.XAxis.Title.Font.Bold = [boolean]$XAxisTitleBold
|
||||
}
|
||||
if ($XAxisTitleSize) {$chart.XAxis.Title.Font.Size = $XAxisTitleSize}
|
||||
}
|
||||
if ($XAxisPosition) {Write-Warning "X-axis position is not being set propertly at the moment, parameter ignored" }
|
||||
@@ -421,7 +423,9 @@ function Add-ExcelChart {
|
||||
|
||||
if ($YAxisTitleText) {
|
||||
$chart.YAxis.Title.Text = $YAxisTitleText
|
||||
if ($YAxisTitleBold) {$chart.YAxis.Title.Font.Bold = $true}
|
||||
if ($PSBoundParameters.ContainsKey('YAxisTitleBold')) {
|
||||
$chart.YAxis.Title.Font.Bold = [boolean]$YAxisTitleBold
|
||||
}
|
||||
if ($YAxisTitleSize) {$chart.YAxis.Title.Font.Size = $YAxisTitleSize}
|
||||
}
|
||||
if ($YAxisPosition) {Write-Warning "Y-axis position is not being set propertly at the moment, parameter ignored" }
|
||||
|
||||
@@ -56,6 +56,10 @@ Install-Module ImportExcel
|
||||
# What's new 5.4
|
||||
|
||||
- Thank you to Conrad Agramont, Twitter: [@AGramont](https://twitter.com/@AGramont) for the `AddMultiWorkSheet.ps1` example. Much appreciated!
|
||||
- Fixed several more bugs where parametrs were ignored if passed a zero value
|
||||
- Merge-MultipleSheets is more robust.
|
||||
- Set-ExcelRow and Set-ExcelColumn trap attempts to process a sheet with no rows/columns.
|
||||
- Help has been proof-read .
|
||||
|
||||
# What's new 5.3.4
|
||||
|
||||
|
||||
@@ -147,7 +147,7 @@
|
||||
Add-ExcelName -Range $Worksheet.Cells[($StartRow+1), $Column, $endRow, $Column] -RangeName $Worksheet.Cells[$StartRow, $Column].Value
|
||||
}
|
||||
|
||||
#Fill in the data
|
||||
#Fill in the data -it can be zero null or and empty string.
|
||||
if ($PSBoundParameters.ContainsKey('Value')) { foreach ($row in ($StartRow..$endRow)) {
|
||||
if ($Value -is [scriptblock]) { #re-create the script block otherwise variables from this function are out of scope.
|
||||
$cellData = & ([scriptblock]::create( $Value ))
|
||||
|
||||
@@ -168,7 +168,7 @@
|
||||
foreach ($file in $updates) {
|
||||
try {$xl = Open-ExcelPackage -Path $file.name }
|
||||
catch {Write-warning -Message "Can't open $($file.Name) for writing." ; return}
|
||||
if ($AllDataBackgroundColor) {
|
||||
if ($PSBoundParameters.ContainsKey("AllDataBackgroundColor")) {
|
||||
$file.Group._sheet | Sort-Object -Unique | ForEach-Object {
|
||||
$ws = $xl.Workbook.Worksheets[$_]
|
||||
if ($headerName) {$range = "A" + $startrow + ":" + $ws.dimension.end.address}
|
||||
@@ -181,7 +181,7 @@
|
||||
$range = $ws.Dimension -replace "\d+",$row._row
|
||||
Set-Format -WorkSheet $ws -Range $range -BackgroundColor $BackgroundColor
|
||||
}
|
||||
if ($TabColor) {
|
||||
if ($PSBoundParameters.ContainsKey("TabColor")) {
|
||||
if ($TabColor -is [string]) {$TabColor = [System.Drawing.Color]::$TabColor }
|
||||
foreach ($tab in ($file.group._sheet | Select-Object -Unique)) {
|
||||
$xl.Workbook.Worksheets[$tab].TabColor = $TabColor
|
||||
|
||||
Reference in New Issue
Block a user