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

@@ -351,9 +351,9 @@ End Function
' 键值对: "materialName"->物料名称, "materialCode"->物料编码,
' "materialQty"->物料数量, "materialType"->物料类型(工作表名)
'
' 注意:
' - 默认查找"物料名称"、"物料编码"、"物料数量"列
' - 如果列名不同,可以根据实际情况调整
' 逻辑:
' - 通过列名查找物料信息列(不依赖列位置)
' - 支持"名称"、"编码"、"数量"列
' ------------------------------------------------------------------------------
Public Function ExtractMaterialInfo( _
ByVal ws As Worksheet, _
@@ -364,28 +364,38 @@ Public Function ExtractMaterialInfo( _
Dim materialInfo As Object
Set materialInfo = CreateObject("Scripting.Dictionary")
' 默认物料信息列名
materialInfo("materialType") = ws.Name
' 查找物料名称
If headerMap.Exists("物料名称") Then
materialInfo("materialName") = Trim(CStr(ws.Cells(rowNum, headerMap("物料名称")).Value))
' 通过列名查找物料信息
Dim nameKey As String, codeKey As String, qtyKey As String
nameKey = LCase(BOMLIB_COL_NAME)
codeKey = LCase(BOMLIB_COL_CODE)
qtyKey = LCase(BOMLIB_COL_QTY)
' 提取物料名称
If headerMap.Exists(nameKey) Then
Dim nameCol As Long
nameCol = headerMap(nameKey)
materialInfo("materialName") = Trim(CStr(ws.Cells(rowNum, nameCol).Value))
Else
materialInfo("materialName") = ""
End If
' 查找物料编码
If headerMap.Exists("物料编码") Then
materialInfo("materialCode") = Trim(CStr(ws.Cells(rowNum, headerMap("物料编码")).Value))
' 提取物料编码
If headerMap.Exists(codeKey) Then
Dim codeCol As Long
codeCol = headerMap(codeKey)
materialInfo("materialCode") = Trim(CStr(ws.Cells(rowNum, codeCol).Value))
Else
materialInfo("materialCode") = ""
End If
' 查找物料数量
If headerMap.Exists("物料数量") Then
' 提取物料数量
If headerMap.Exists(qtyKey) Then
Dim qtyCol As Long
qtyCol = headerMap(qtyKey)
Dim qtyValue As Variant
qtyValue = ws.Cells(rowNum, headerMap("物料数量")).Value
qtyValue = ws.Cells(rowNum, qtyCol).Value
If IsNumeric(qtyValue) Then
materialInfo("materialQty") = CLng(qtyValue)
Else