diff --git a/Public/ConvertFrom-ExcelSheet.md b/Public/ConvertFrom-ExcelSheet.md deleted file mode 100644 index b91972c..0000000 --- a/Public/ConvertFrom-ExcelSheet.md +++ /dev/null @@ -1,140 +0,0 @@ ---- -external help file: ImportExcel-help.xml -Module Name: ImportExcel -online version: -schema: 2.0.0 ---- - -# ConvertFrom-ExcelSheet - -## SYNOPSIS -Reads an Excel file an converts the data to a delimited text file. - -## SYNTAX - -``` -ConvertFrom-ExcelSheet [-Path] [[-OutputPath] ] [[-SheetName] ] [[-Encoding] ] - [[-Extension] ] [[-Delimiter] ] [] -``` - -## DESCRIPTION -{{ Fill in the Description }} - -## EXAMPLES - -### EXAMPLE 1 -``` -ConvertFrom-ExcelSheet .\TestSheets.xlsx .\data -``` - -Reads each sheet in TestSheets.xlsx and outputs it to the data directory as the sheet name with the extension .txt. - -### EXAMPLE 2 -``` -ConvertFrom-ExcelSheet .\TestSheets.xlsx .\data sheet?0 -``` - -Reads and outputs sheets like Sheet10 and Sheet20 form TestSheets.xlsx and outputs it to the data directory as the sheet name with the extension .txt. - -## PARAMETERS - -### -Path -{{ Fill Path Description }} - -```yaml -Type: String -Parameter Sets: (All) -Aliases: FullName - -Required: True -Position: 1 -Default value: None -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -OutputPath -{{ Fill OutputPath Description }} - -```yaml -Type: String -Parameter Sets: (All) -Aliases: - -Required: False -Position: 2 -Default value: .\ -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -SheetName -{{ Fill SheetName Description }} - -```yaml -Type: String -Parameter Sets: (All) -Aliases: - -Required: False -Position: 3 -Default value: * -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -Encoding -{{ Fill Encoding Description }} - -```yaml -Type: String -Parameter Sets: (All) -Aliases: - -Required: False -Position: 4 -Default value: UTF8 -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -Extension -{{ Fill Extension Description }} - -```yaml -Type: String -Parameter Sets: (All) -Aliases: - -Required: False -Position: 5 -Default value: .csv -Accept pipeline input: False -Accept wildcard characters: False -``` - -### -Delimiter -{{ Fill Delimiter Description }} - -```yaml -Type: String -Parameter Sets: (All) -Aliases: - -Required: False -Position: 6 -Default value: ; -Accept pipeline input: False -Accept wildcard characters: False -``` - -### CommonParameters -This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). - -## INPUTS - -## OUTPUTS - -## NOTES - -## RELATED LINKS diff --git a/Public/ConvertFrom-ExcelSheet.ps1 b/Public/ConvertFrom-ExcelSheet.ps1 index db054f4..95a46df 100644 --- a/Public/ConvertFrom-ExcelSheet.ps1 +++ b/Public/ConvertFrom-ExcelSheet.ps1 @@ -15,7 +15,8 @@ function ConvertFrom-ExcelSheet { $Property = "*", $ExcludeProperty = @(), [switch]$Append, - [string[]]$AsText = @() + [string[]]$AsText = @(), + [string[]]$AsDate = @() ) $Path = (Resolve-Path $Path).Path @@ -25,7 +26,7 @@ function ConvertFrom-ExcelSheet { $targetSheets = $workbook.Worksheets | Where-Object {$_.Name -Like $SheetName} $csvParams = @{NoTypeInformation = $true} + $PSBoundParameters - foreach ($p in 'OutputPath', 'SheetName', 'Extension', 'Property','ExcludeProperty', 'AsText') { + foreach ($p in 'OutputPath', 'SheetName', 'Extension', 'Property','ExcludeProperty', 'AsText','AsDate') { $csvParams.Remove($p) } @@ -34,7 +35,7 @@ function ConvertFrom-ExcelSheet { $csvParams.Path = "$OutputPath\$($Sheet.Name)$Extension" - Import-Excel -ExcelPackage $xl -Sheet $($sheet.Name) -AsText:$AsText | + Import-Excel -ExcelPackage $xl -Sheet $($sheet.Name) -AsText:$AsText -AsDate:$AsDate | Select-Object -Property $Property | Export-Csv @csvparams } diff --git a/Public/Import-Excel.ps1 b/Public/Import-Excel.ps1 index 11a856b..0afc8db 100644 --- a/Public/Import-Excel.ps1 +++ b/Public/Import-Excel.ps1 @@ -33,6 +33,7 @@ [Int]$EndColumn , [Switch]$DataOnly, [string[]]$AsText, + [string[]]$AsDate, [ValidateNotNullOrEmpty()] [String]$Password ) @@ -160,25 +161,39 @@ } else { #region Create one object per row - if ($AsText) { + if ($AsText -or $AsDate) { <#join items in AsText together with ~~~ . Escape any regex special characters... # which turns "*" into "\*" make it ".*". Convert ~~~ to $|^ and top and tail with ^%; So if we get "Week", "[Time]" and "*date*" ; make the expression ^week$|^\[Time\]$|^.*Date.*$ $make a regex for this which is case insensitive (option 1) and compiled (option 8) #> - $TextColExpression = "^" + [regex]::Escape($AsText -join "~~~").replace("\*", ".*").replace("~~~", "$|^") + "$" + $TextColExpression = '' + if ($AsText) { + $TextColExpression += '(?^' + [regex]::Escape($AsText -join '~~~').replace('\*', '.*').replace('~~~', '$|^') + '$)' + } + if ($AsText -and $AsDate) { + $TextColExpression += "|" + } + if ($AsDate) { + $TextColExpression += '(?^' + [regex]::Escape($AsDate -join '~~~').replace('\*', '.*').replace('~~~', '$|^') + '$)' + } $TextColRegEx = New-Object -TypeName regex -ArgumentList $TextColExpression , 9 } + else {$TextColRegEx = $null} foreach ($R in $Rows) { #Disabled write-verbose for speed # Write-Verbose "Import row '$R'" $NewRow = [Ordered]@{ } if ($TextColRegEx) { foreach ($P in $PropertyNames) { - if ($TextColRegEx.IsMatch($P.Value)) { - $NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Text + $MatchTest = $TextColRegEx.Match($P.value) + if ($MatchTest.groups.name -eq "astext") { + $NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Text } - else { $NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Value } + elseif ($MatchTest.groups.name -eq "asdate" -and $Worksheet.Cells[$R, $P.Column].Value -is [System.ValueType]) { + $NewRow[$P.Value] = [datetime]::FromOADate(($Worksheet.Cells[$R, $P.Column].Value)) + } + else { $NewRow[$P.Value] = $Worksheet.Cells[$R, $P.Column].Value } } } else { diff --git a/__tests__/ConvertFrom-ExcelSheet.Tests.ps1 b/__tests__/ConvertFrom-ExcelSheet.Tests.ps1 index 5065a33..dfc5498 100644 --- a/__tests__/ConvertFrom-ExcelSheet.Tests.ps1 +++ b/__tests__/ConvertFrom-ExcelSheet.Tests.ps1 @@ -12,25 +12,30 @@ Describe 'ConvertFrom-ExcelSheet / Export-ExcelSheet' { ConvertFrom-ExcelSheet -Path $dataPath -OutputPath $Outpath -AsText "GridPosition" -Property driver, @{n="date"; e={[datetime]::FromOADate($_.Date).tostring("#MM/dd/yyyy#")}} , FinishPosition, GridPosition $ThirdText = Get-Content (Join-path -Path $Outpath -ChildPath "First10Races.csv") + ConvertFrom-ExcelSheet -Path $dataPath -OutputPath $Outpath -AsDate "date" + $FourthText = Get-Content (Join-path -Path $Outpath -ChildPath "First10Races.csv") } Context "Exporting to CSV" { it "Exported the expected columns to a CSV file " { $firstText[0] | should be '"Race","Date","FinishPosition","Driver","GridPosition","Team","Points"' $SecondText[0] | should be '"Race","Date","FinishPosition","Driver","GridPosition","Team","Points"' $ThirdText[0] | should be '"Driver","date","FinishPosition","GridPosition"' + $FourthText[0] | should be '"Race","Date","FinishPosition","Driver","GridPosition","Team","Points"' } - it "Applied ASText and Properties correctly " { + it "Applied AsText, AsDate and Properties correctly " { $firstText[1] | should match '^"\w+","\d{5}","\d{1,2}","\w+ \w+","[1-9]\d?","\w+","\d{1,2}"$' $date = $firstText[1] -replace '^.*(\d{5}).*$', '$1' $date = [datetime]::FromOADate($date).toString("D") - $secondText[1] | should belike "*$date*" - $secondText[1] | should match '"0\d","\w+","\d{1,2}"$' - $ThirdText[1] | should match '^"\w+ \w+","#\d\d/\d\d/\d{4}#","\d","0\d"$' + $secondText[1] | should belike "*$date*" + $secondText[1] | should match '"0\d","\w+","\d{1,2}"$' + $ThirdText[1] | should match '^"\w+ \w+","#\d\d/\d\d/\d{4}#","\d","0\d"$' + $FourthText[1] | should match '^"\w+","[012]\d' } } Context "Export aliased to ConvertFrom" { - it "Applied ASText and Properties correctly " { + it "Definded the alias name with " { (Get-Alias Export-ExcelSheet).source | should be "ImportExcel" + (Get-Alias Export-ExcelSheet).Definition | should be "ConvertFrom-ExcelSheet" } } } \ No newline at end of file diff --git a/en/ImportExcel-help.xml b/en/ImportExcel-help.xml index a7eb2b6..a738fe4 100644 --- a/en/ImportExcel-help.xml +++ b/en/ImportExcel-help.xml @@ -6576,6 +6576,346 @@ Boston,2/18/2018,1000 + + + ConvertFrom-ExcelSheet + ConvertFrom + ExcelSheet + + Exports Sheets from Excel Workbooks to CSV files. + + + + This command provides a convenient way to run Import-Excel @ImportParameters | Select-Object @selectParameters | export-Csv @ ExportParameters It can take the parameters -AsText , as used in Import-Excel, )Properties & -ExcludeProperties as used in Select-Object and -Append, -Delimiter and -Encoding as used in Export-CSV + + + + ConvertFrom-ExcelSheet + + Path + + The path to the .XLSX file which will be exported. + + String + + String + + + None + + + OutputPath + + The directory where the output file(s) will be created. The file name(s) will match the name of the workbook page which contained the data. + + String + + String + + + None + + + SheetName + + The name of a sheet to export, or a regular expression which is used to identify sheets + + String + + String + + + None + + + Encoding + + Sets the text encoding for the output data file; UTF8 bu default + + Encoding + + Encoding + + + None + + + Extension + + Sets the file extension for the exported data, defaults to CSV + + + .txt + .log + .csv + + String + + String + + + None + + + Delimiter + + Selects , or ; as the delimeter for the exported data - if not specified , is used by default. + + + ; + + + + String + + String + + + None + + + Property + + Specifies the properties to select. Wildcards are permitted - the default is "*". The value of the Property parameter can be a new calculated property, and follows the same pattern as Select-Item + + Object + + Object + + + None + + + ExcludeProperty + + Specifies the properties that to exclude from the export. Wildcards are permitted. This parameter is effective only when the command also includes the Property parameter. + + Object + + Object + + + None + + + AsText + + AsText allows selected columns to be returned as the text displayed in their cells, instead of their value. (* is supported as a wildcard.) + + String[] + + String[] + + + None + + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + + + Append + + Use this parameter to have the export add output to the end of the file. Without this parameter, the command replaces the file contents without warning. + + + SwitchParameter + + + False + + + + + + Append + + Use this parameter to have the export add output to the end of the file. Without this parameter, the command replaces the file contents without warning. + + SwitchParameter + + SwitchParameter + + + False + + + AsText + + AsText allows selected columns to be returned as the text displayed in their cells, instead of their value. (* is supported as a wildcard.) + + String[] + + String[] + + + None + + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + + + Delimiter + + Selects , or ; as the delimeter for the exported data - if not specified , is used by default. + + String + + String + + + None + + + Encoding + + Sets the text encoding for the output data file; UTF8 bu default + + Encoding + + Encoding + + + None + + + ExcludeProperty + + Specifies the properties that to exclude from the export. Wildcards are permitted. This parameter is effective only when the command also includes the Property parameter. + + Object + + Object + + + None + + + Extension + + Sets the file extension for the exported data, defaults to CSV + + String + + String + + + None + + + OutputPath + + The directory where the output file(s) will be created. The file name(s) will match the name of the workbook page which contained the data. + + String + + String + + + None + + + Path + + The path to the .XLSX file which will be exported. + + String + + String + + + None + + + Property + + Specifies the properties to select. Wildcards are permitted - the default is "*". The value of the Property parameter can be a new calculated property, and follows the same pattern as Select-Item + + Object + + Object + + + None + + + SheetName + + The name of a sheet to export, or a regular expression which is used to identify sheets + + String + + String + + + None + + + + + + None + + + + + + + + + + System.Object + + + + + + + + + + + + + + -------------------------- Example 1 -------------------------- + PS C:\> ConvertFrom-ExcelSheet Path .\__tests__\First10Races.xlsx -OutputPath .. -AsText GridPosition,date + + First10Races.xlsx contains information about Motor races. The race date and grid (starting) position are stored with custom formats. The command specifies the path to the file, and the directory to create the output file, and specifies that the columns "GridPosition" and "Date" should be treated as text to preserve their formatting + + + + -------------------------- Example 2 -------------------------- + PS C:\> ConvertFrom-ExcelSheet Path .\__tests__\First10Races.xlsx -OutputPath .. -AsText "GridPosition" -Property driver, @{n="date"; e={[datetime]::FromOADate($_.Date).tostring("#MM/dd/yyyy#")}} , FinishPosition, GridPosition + + This uses the same file as example 1. Because the race date has a custom format, it imports as a number, The requirement is to create a CSV file with the Driver, a specially formatted Date, FinishPostion and GridPostion (keeping its custom formatting). The command specifies the path to the file, and the directory to create the output file, specifies that the column "GridPosition" should be treated as text instead of a number, and the output properties should be Driver, a calculated "date" field, FinishPosition and GridPsition. FromOADate converts the dates used by Excel (Days since Jan 1 1900) to a datetime object. + + + + + + Online Version: + https://github.com/dfinke/ImportExcel + + + ConvertFrom-ExcelToSQLInsert @@ -10499,6 +10839,18 @@ PS\> Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePiv None + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + Password @@ -10624,6 +10976,18 @@ PS\> Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePiv None + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + Password @@ -10735,6 +11099,18 @@ PS\> Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePiv None + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + Password @@ -10859,6 +11235,18 @@ PS\> Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePiv None + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + Password @@ -10985,6 +11373,18 @@ PS\> Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePiv None + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + Password @@ -11097,6 +11497,18 @@ PS\> Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePiv None + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + Password @@ -11249,6 +11661,18 @@ PS\> Export-Excel -ExcelPackage $excel -WorksheetName "Processes" -IncludePiv None + + AsDate + + Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + + String[] + + String[] + + + None + Password diff --git a/mdHelp/buildHelp.ps1 b/mdHelp/buildHelp.ps1 new file mode 100644 index 0000000..55abfbb --- /dev/null +++ b/mdHelp/buildHelp.ps1 @@ -0,0 +1,4 @@ +Import-Module platyPS +Get-ChildItem $PSScriptRoot -Directory | ForEach-Object { + New-ExternalHelp -Path $_.FullName -OutputPath (Join-Path $PSScriptRoot "..\$($_.Name)") -Force -Verbose +} \ No newline at end of file diff --git a/mdHelp/en/ConvertFrom-ExcelSheet.md b/mdHelp/en/ConvertFrom-ExcelSheet.md index ed25289..c0c2b8a 100644 --- a/mdHelp/en/ConvertFrom-ExcelSheet.md +++ b/mdHelp/en/ConvertFrom-ExcelSheet.md @@ -76,6 +76,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -AsDate +Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + +```yaml +Type: String[] +Parameter Sets: (All) +Aliases: + +Required: False +Position: 8 +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Delimiter Selects , or ; as the delimeter for the exported data - if not specified , is used by default. diff --git a/mdHelp/en/Import-Excel.md b/mdHelp/en/Import-Excel.md index 159f13d..18e64b1 100644 --- a/mdHelp/en/Import-Excel.md +++ b/mdHelp/en/Import-Excel.md @@ -395,6 +395,21 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -AsDate +Not all date formats are recognized as indicating the number in the cell represents a date AsDate forces the number which would be returned to be converted to a date. (* is supported as a wildcard.) + +```yaml +Type: String[] +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### -Password Accepts a string that will be used to open a password protected Excel file.