-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormat-ImportExcelDate.ps1
86 lines (81 loc) · 2.54 KB
/
Format-ImportExcelDate.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function Format-ImportExcelDate {
<#
.SYNOPSIS
Correct dates when using Import-Excel
.DESCRIPTION
Corects common Excel date/time formats when running Dfinke's Import-Excel module
.PARAMETER CellData
Data to be converted to datetime format
.PARAMETER DateOnNull
Static Date and Time returned when CellData is not provided or missing
.EXAMPLE
$ImportedExcel = Import-Excel -Path $Path -WorksheetName $WrkShtName |
foreach {
$CellObject = $_
$CellObject.DateColumn = $CellObject.DateColumn |
Format-ImportExcelDate -DateOnNull ((get-date).Date)
$CellObject
}
Command shows how import an Excel worksheet
and format cell values in the column "DateColumn"
to a .NET datetime value.
The current date/time is
returned when an imported cell is empty
.NOTES
Name: Format-ImportExcelDate
Author: Mark Curry
Keywords: Powershell,Excel,Date,Time
.LINK
https://github.com/i-ScriptALot
.LINK
https://github.com/dfinke/ImportExcel
.INPUTS
Object
.OUTPUTS
System.DateTime
#>
# Requires -Version 2.0
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline = $True, Position = 0)]
[System.Object[]]$CellData,
[Parameter(Position = 1)]
[DateTime]$DateOnNull
)
PROCESS {
Write-Debug -Message "Begin format Excel dates ,data Count is $($CellData.count)"
Write-Debug -Message "DateOnNull is $($DateOnNull)"
foreach ($Value in $CellData) {
$ValueType = ($Value).gettype().name
Write-Debug "Processing $Value with type $ValueType"
switch ($true) {
(-not $Value -and $DateOnNull) {
$DateValue = $DateOnNull
break
}
(-not $Value -and -not $DateOnNull) {
$DateValue = ''
break
}
($ValueType -eq 'Double') {
$DateValue = [datetime]::FromOADate($Value)
break
}
($ValueType -eq 'int32' -or $ValueType -eq 'int') {
$DateValue = [datetime]::FromOADate($Value)
break
}
($ValueType -eq 'DateTime') {
$DateValue = $Value
break
}
Default {
$DateValue = $Value
}
}
Write-Debug -message "Writing $DateValue to pipeline"
$DateValue
} # End foreach
} # End process block
} #End function