PowerShell:如果 B 列值为 "X",则使用 A 列前一行数据的数据填充 A 列空行

PowerShell: populate column A null rows with data from previous column A row data, if Column B value is "X"

我有一个格式奇怪的 csv 文件需要调整,我可以使用 powershell 获得一些帮助。我通常很清楚从哪里开始,但是我对如何根据具有“销售订单”的 B 列从前一行到下一行的 A 列迭代数据有点不知所措,并且只有如果 Column A 字段为 Null,则执行此操作,然后使用填充的下一行继续下行。

这是 csv 图片的样子:

这是实际数据:

,Type,Date,Due Date,Num
Inventory,,,,
2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid),,,,
,Sales Order,7/23/2021,7/23/2021,12834
Total 2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid),,,,
Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer)),,,,
,Sales Order,9/23/2021,9/23/2021,13304
Total Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer)),,,,
,Sales Order,7/23/2021,7/23/2021,12841
Total Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer)),,,,
,Sales Order,4/1/2021,4/1/2021,11807
Total Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer)),,,,
,Sales Order,6/9/2021,6/9/2021,12457
Total Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer)),,,,
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,,,,
,Sales Order,6/7/2021,6/7/2021,12425
,Sales Order,8/3/2021,8/3/2021,12939
,Sales Order,9/14/2021,9/14/2021,13218
Total Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,,,,
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...,,,,
,Sales Order,6/8/2021,6/8/2021,12454
,Sales Order,8/24/2021,8/24/2021,13102
,Sales Order,9/14/2021,9/14/2021,13220
,Sales Order,10/8/2021,10/8/2021,13415

我需要用上面的行填充 B 列中具有“销售订单”的 A 列中的空白行。 (我讨厌 quickbooks 报告格式)所以它看起来像这样:(添加的行以黄色突出显示)

数据如下所示:

,Type,Date,Due Date,Num
Inventory,,,,
2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid),,,,
2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid),Sales Order,7/23/2021,7/23/2021,12834
Total 2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid),,,,
Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer)),Sales Order,9/23/2021,9/23/2021,13304
Total Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer)),Sales Order,7/23/2021,7/23/2021,12841
Total Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer)),Sales Order,4/1/2021,4/1/2021,11807
Total Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer)),Sales Order,6/9/2021,6/9/2021,12457
Total Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer)),,,,
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,,,,
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,Sales Order,6/7/2021,6/7/2021,12425
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,Sales Order,8/3/2021,8/3/2021,12939
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,Sales Order,9/14/2021,9/14/2021,13218
Total Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,,,,
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...,,,,
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...,Sales Order,6/8/2021,6/8/2021,12454
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...,Sales Order,8/24/2021,8/24/2021,13102
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...,Sales Order,9/14/2021,9/14/2021,13220
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...,Sales Order,10/8/2021,10/8/2021,13415

附带说明一下,如果有人知道如何显示“按项目打开的销售订单”的 Quickbooks 桌面报告以更好的格式显示,那也很好。

$Data = ConvertFrom-Csv @'
,Type,Date,Due Date,Num
Inventory,,,,
2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid),,,,
,Sales Order,7/23/2021,7/23/2021,12834
Total 2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid),,,,
Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer)),,,,
,Sales Order,9/23/2021,9/23/2021,13304
Total Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer)),,,,
,Sales Order,7/23/2021,7/23/2021,12841
Total Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer)),,,,
,Sales Order,4/1/2021,4/1/2021,11807
Total Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer)),,,,
Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer)),,,,
,Sales Order,6/9/2021,6/9/2021,12457
Total Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer)),,,,
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,,,,
,Sales Order,6/7/2021,6/7/2021,12425
,Sales Order,8/3/2021,8/3/2021,12939
,Sales Order,9/14/2021,9/14/2021,13218
Total Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...,,,,
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...,,,,
,Sales Order,6/8/2021,6/8/2021,12454
,Sales Order,8/24/2021,8/24/2021,13102
,Sales Order,9/14/2021,9/14/2021,13220
,Sales Order,10/8/2021,10/8/2021,13415
'@

WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers.

$Data |ForEach-Object { $H1 = '' } {
    If ($_.H1) { $H1 = $_.H1 }
    If ($_.Type -eq 'Sales Order') { $_.H1 = $H1 }
    $_
} |Format-Table

结果:

H1                                                                                         Type        Date      Due Date  Num
--                                                                                         ----        ----      --------  ---
Inventory
2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid)
2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid)                      Sales Order 7/23/2021 7/23/2021 12834
Total 2.25 Gal Cast Iron KettlewLid (2.25 Gallon Cast Iron Kettle with Lid)
Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer))
Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer))           Sales Order 9/23/2021 9/23/2021 13304
Total Antika Chestnut Brown (Antika Chestnut Brown (87 sqft/pallet - 9.67 sqft/layer))
Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer))
Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer))                   Sales Order 7/23/2021 7/23/2021 12841
Total Antika Onyx Black (Antika Onyx Black (87 sqft/pallet - 9.67 sqft/layer))
Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer))
Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer))                   Sales Order 4/1/2021  4/1/2021  11807
Total Antika Sandlewood (Antika Sandlewood (87 sqft/pallet - 9.67 sqft/layer))
Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer))
Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer))                   Sales Order 6/9/2021  6/9/2021  12457
Total Antika Shale Grey (Antika Shale Grey (87 sqft/pallet - 9.67 sqft/layer))
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...       Sales Order 6/7/2021  6/7/2021  12425
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...       Sales Order 8/3/2021  8/3/2021  12939
Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...       Sales Order 9/14/2021 9/14/2021 13218
Total Architect Cap Champlain Grey (Architectural Cap Champlain Grey (66 linft/pallet -...
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...       Sales Order 6/8/2021  6/8/2021  12454
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...       Sales Order 8/24/2021 8/24/2021 13102
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...       Sales Order 9/14/2021 9/14/2021 13220
Architect Cap Chestnut Brown (Architectural Cap Chestnut Brown (66 linft/pallet -...       Sales Order 10/8/2021 10/8/2021 13415

说明

  • ConvertFrom-Csv 导致警告,因为第一列为空。正如警告所暗示的那样,属性 H1 用于此相关的 属性
  • ForEach-Object 支持多个脚本块作为输入。当提供两个脚本块时,第一个分配给 -Begin 参数,后一个分配给 -Proces 参数。
  • 开始块 ({ $H1 = '' }) 只是确保未设置 $H1 变量。
  • If ($_.H1) { $H1 = $_.H1 } 从当前行
  • 捕获H1(第一个单元格)
  • If ($_.Type -eq 'Sales Order') { $_.H1 = $H1 } 在当前行设置 H1(第一个单元格),这可能已在先前的迭代中捕获
  • $_ 发出当前(修改后的)对象