refactor: replace CODE field source from column C to column I (66代码)
All checks were successful
NTFY Notification / notify (push) Successful in 14s

- Add COL_IDX_CODE66 constant for column I in M04_Config.bas
- Extend data reading range to include column I in M02_DataIO.ReadSourceData
- Use .Text property for column I to preserve leading zeros
- Update baseInfo array to use column I instead of column C in M01_Main.bas
- Set output code column to text format to prevent numeric conversion

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-02-24 12:44:31 +08:00
parent 53b3568412
commit 541bbfe0ad
3 changed files with 37 additions and 6 deletions

View File

@@ -95,8 +95,8 @@ Public Sub RunBOMConversion()
catData.Add strCat, New Collection
End If
' 基础信息: 代号, 名称, 数量
baseInfo = Array(arrData(i, M04_Config.COL_IDX_CODE), _
' 基础信息: 66代码, 名称, 数量使用I列的66代码作为编码
baseInfo = Array(arrData(i, M04_Config.COL_IDX_CODE66), _
arrData(i, M04_Config.COL_IDX_NAME), _
arrData(i, M04_Config.COL_IDX_QTY))

View File

@@ -6,7 +6,12 @@ Option Explicit
Public Function ReadSourceData(ws As Worksheet) As Variant
Dim lastRow As Long
' 查找C列最后一行
Dim i As Long, r As Long
Dim arrRaw As Variant
Dim arrResult() As Variant
Dim cell As Range
' 查找C列最后一行保持原有逻辑使用代号列判断
lastRow = ws.Cells(ws.Rows.count, M04_Config.COL_IDX_CODE).End(xlUp).row
If lastRow < M04_Config.SRC_START_ROW Then
@@ -14,8 +19,28 @@ Public Function ReadSourceData(ws As Worksheet) As Variant
Exit Function
End If
' 读取整张表的数据区域 (假设列数不超过20列足以覆盖到类别列)
ReadSourceData = ws.Range(ws.Cells(M04_Config.SRC_START_ROW, 1), ws.Cells(lastRow, M04_Config.COL_IDX_CAT)).Value
' 第一步:使用数组方式读取整个数据区域(高性能)
arrRaw = ws.Range(ws.Cells(M04_Config.SRC_START_ROW, 1), ws.Cells(lastRow, M04_Config.COL_IDX_CODE66)).Value
' 第二步对66代码列第9列使用.Text属性重新读取以保留前导0
' 因为.Value会将"00123"转换为123而.Text会保留显示的"00123"
ReDim arrResult(1 To UBound(arrRaw, 1), 1 To UBound(arrRaw, 2))
' 复制原始数据
For r = 1 To UBound(arrRaw, 1)
For i = 1 To UBound(arrRaw, 2)
If i = M04_Config.COL_IDX_CODE66 Then
' 对于66代码列I列使用.Text属性读取显示文本
Set cell = ws.Cells(M04_Config.SRC_START_ROW + r - 1, M04_Config.COL_IDX_CODE66)
arrResult(r, i) = cell.Text
Else
' 其他列使用原始值
arrResult(r, i) = arrRaw(r, i)
End If
Next i
Next r
ReadSourceData = arrResult
End Function
Public Sub WriteCategoryToNewBook(catData As Object)
@@ -105,9 +130,14 @@ Public Sub WriteCategoryToNewBook(catData As Object)
finalArr(r + 1, colOffset + 3) = baseInfo(2) ' Qty
Next r
' 4. 写入Excel
' 4 设置编码列为文本格式(防止数字编码被转换为科学计数法或丢失前导零)
ws.Columns(colOffset + 2).NumberFormat = "@"
' 4.1 写入Excel
ws.Range("A1").Resize(UBound(finalArr, 1), UBound(finalArr, 2)).Value = finalArr
ws.Range("A1").Resize(1, totalCols).Font.Bold = True
ws.Columns.AutoFit
End If
Next catName

View File

@@ -10,6 +10,7 @@ Public Const COL_IDX_NAME As Long = 4 ' 名称 (D列)
Public Const COL_IDX_QTY As Long = 5 ' 数量 (E列)
Public Const COL_IDX_COND As Long = 6 ' 选择条件 (F列)
Public Const COL_IDX_CAT As Long = 8 ' 类别 (H列)
Public Const COL_IDX_CODE66 As Long = 9 ' 66代码 (I列) - NEW
Public Const SRC_START_ROW As Long = 4 ' 数据起始行
' ------------------------------------------------------------------------------