【发布时间】:2020-12-22 06:24:50
【问题描述】:
我有一个包含 2 个日期列的电子表格,每行中的任何一个都只有一个日期值。有两列对于我的目的来说是多余的,所以我想运行一个 powershell 脚本来检查 Date2 列中的每个单元格,如果为空,请将同一行上的 Date1 值复制到 Date2 列。那是我无法解决的部分;要检查 Date2 列中的所有单元格,如果为空白,请复制相邻的 Date1 值。 完成后,我将删除 Date1 列。 注意:Date2 列有更多日期,因此将其用作要保留的主要列(较少复制/粘贴)。 TIA
Example Test.xlsx:
A B
1 | Date1 | Date2 |
2 | 01/01/1900 | |
3 | 01/01/1900 | |
4 | | 01/01/1900 |
5 | | 01/01/1900 |
6 | 01/01/1900 | |
7 | | 01/01/1900 |
$Excel = New-Object -Com Excel.Application -Property @{Visible = $false} # Start Excel and hide the window
$Excel.DisplayAlerts = $False # Disable comfirmation prompts
$Workbook = $Excel.Workbooks.Open(C:\Temp\Test.xlsx) # Open spreadsheet file
#This is the part I'm stuck on:
#If cell in column Date2 is empty, then copy the adjacent Date1 value to the Date2 cell - This is done until at the end of the columns
[void]$Workbook.Sheets.Item("Test").Cells.Item(1, 1).EntireColumn.Delete() # Deleting "Date1" column
$Workbook.Save() # Save changes
$Workbook.Close($true) # Close workbook
$Excel.Quit() # Quit Excel
[Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) # Release COM
【问题讨论】:
标签: excel powershell if-statement cell copy-paste