refactor: improve BOM library matching logic with column-name-based lookup
All checks were successful
NTFY Notification / notify (push) Successful in 3s

- Add BOM library column name constants (名称, 编码, 数量, etc.)
- Add GetBOMConditionFields() and IsConditionField() utility functions
- Refactor ExtractMaterialInfo to use column names instead of hardcoded positions
- Refactor ExtractSingleSubComponent to use column names for 接头/弹性元件
- Refactor ExtractComponentInfo to use column names
- Improve MatchAllMaterialTypes to iterate all worksheets dynamically
- Add debug logging for troubleshooting

This fix resolves issues where worksheets with non-standard column counts
(like 表壳, 罩壳) could not extract material information properly.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-02-12 13:59:48 +08:00
parent c204a8878e
commit 867b1979af
4 changed files with 181 additions and 104 deletions

View File

@@ -375,12 +375,11 @@ End Function
' 输出:
' Collection - 所有匹配到的物料集合
'
' 遍历的工作表:
' - 接头
' - 弹性元件
' - 机芯
' - 部件(特殊处理)
' - 边
' 逻辑:
' - 遍历BOM库中的所有工作表
' - 对每个工作表尝试匹配
' - "部件"工作表特殊处理调用M08_ComponentProcessor
' - 其他工作表使用M07_BOMMatcher匹配
' ------------------------------------------------------------------------------
Private Function MatchAllMaterialTypes( _
ByVal params As Object, _
@@ -392,70 +391,70 @@ Private Function MatchAllMaterialTypes( _
Dim allMaterials As Collection
Set allMaterials = New Collection
' 需要遍历的工作表列
Dim sheetNames As Variant
sheetNames = Array(BOMLIB_SHEET_JOINT, BOMLIB_SHEET_ELEMENT, _
BOMLIB_SHEET_MOVEMENT, BOMLIB_SHEET_EDGE)
Dim i As Long
For i = LBound(sheetNames) To UBound(sheetNames)
' 遍历BOM库中的所有工作
Dim ws As Worksheet
For Each ws In bomWb.Worksheets
Dim sheetName As String
sheetName = sheetNames(i)
sheetName = ws.Name
' 获取工作表
Dim ws As Worksheet
On Error Resume Next
Set ws = bomWb.Sheets(sheetName)
On Error GoTo ErrorHandler
Debug.Print "=== 处理工作表: [" & sheetName & "] ==="
If ws Is Nothing Then
' 工作表不存在,跳过
GoTo NextSheet
End If
' "部件"工作表特殊处理
If sheetName = BOMLIB_SHEET_COMPONENT Then
Debug.Print " -> 使用部件处理逻辑"
' 匹配记录
Dim matchResult As Object
Set matchResult = M07_BOMMatcher.MatchBOMRecord(ws, params)
Dim componentMaterials As Collection
Set componentMaterials = M08_ComponentProcessor.ProcessComponentRecord( _
ws, params, logger)
' 如果匹配成功,提取物料信息
If matchResult("success") Then
Dim rowNum As Long
rowNum = matchResult("rowNums")(1)
Debug.Print " -> 返回物料数: " & componentMaterials.count
Dim headerMap As Object
Set headerMap = M07_BOMMatcher.BuildWorksheetHeaderMap(ws)
Dim materialInfo As Object
Set materialInfo = M07_BOMMatcher.ExtractMaterialInfo(ws, rowNum, headerMap)
If Not materialInfo Is Nothing Then
allMaterials.Add materialInfo
End If
Dim compMat As Variant
For Each compMat In componentMaterials
Debug.Print " [" & compMat("materialType") & "] 名称=[" & compMat("materialName") & "] 编码=[" & compMat("materialCode") & "]"
allMaterials.Add compMat
Next compMat
Else
' 匹配失败,记录错误(但不中断处理)
logger.Record 0, "M09.MatchAllMaterialTypes", "BOMMatchError", _
sheetName & " " & matchResult("message"), ""
' 其他工作表使用标准匹配逻辑
' 匹配记录
Dim matchResult As Object
Set matchResult = M07_BOMMatcher.MatchBOMRecord(ws, params)
Debug.Print " -> 匹配结果: " & matchResult("success") & ", 行数: " & matchResult("rowCount")
' 如果匹配成功,提取物料信息
If matchResult("success") Then
Dim rowNum As Long
rowNum = matchResult("rowNums")(1)
Debug.Print " -> 匹配行号: " & rowNum
Dim headerMap As Object
Set headerMap = M07_BOMMatcher.BuildWorksheetHeaderMap(ws)
Debug.Print " -> 表头映射键数: " & headerMap.count
Dim materialInfo As Object
Set materialInfo = M07_BOMMatcher.ExtractMaterialInfo(ws, rowNum, headerMap)
Debug.Print " -> 提取结果: 名称=[" & materialInfo("materialName") & "] 编码=[" & materialInfo("materialCode") & "] 数量=[" & materialInfo("materialQty") & "]"
If Not materialInfo Is Nothing Then
allMaterials.Add materialInfo
Debug.Print " -> 已添加到物料集合"
Else
Debug.Print " -> ERROR: materialInfo为Nothing"
End If
Else
' 匹配失败,记录错误(但不中断处理)
If matchResult("rowCount") = 0 Then
logger.Record 0, "M09.MatchAllMaterialTypes", "BOMMatchError", _
sheetName & " " & matchResult("message"), ""
Debug.Print " -> 未匹配到记录"
End If
End If
End If
NextSheet:
Next i
' 特殊处理"部件"工作表
Dim wsComponent As Worksheet
On Error Resume Next
Set wsComponent = bomWb.Sheets(BOMLIB_SHEET_COMPONENT)
On Error GoTo ErrorHandler
If Not wsComponent Is Nothing Then
Dim componentMaterials As Collection
Set componentMaterials = M08_ComponentProcessor.ProcessComponentRecord( _
wsComponent, params, logger)
Dim compMat As Variant
For Each compMat In componentMaterials
allMaterials.Add compMat
Next compMat
End If
Debug.Print ""
Next ws
Set MatchAllMaterialTypes = allMaterials
Exit Function