fix: address code review issues for Access support
- Fix import_vba_access() to actually save database via DoCmd.Save() - Update stale module docstring in extract_vba.py - Validate file extensions in get_file_type() instead of silently defaulting to excel - Scan both Excel/ and Access/ directories in interactive mode Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
VBA代码提取工具
|
VBA代码提取工具
|
||||||
从xlsm文件中提取模块和类模块代码,分类保存到VBA文件夹
|
从Excel(.xlsm)或Access(.accdb)文件中提取VBA代码,分类保存到VBA文件夹
|
||||||
自动清理Attribute信息并生成元数据JSON文件
|
自动清理Attribute信息
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@@ -42,11 +42,16 @@ def get_file_type(file_path: Path) -> str:
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
'access' 或 'excel'
|
'access' 或 'excel'
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: 不支持的文件扩展名
|
||||||
"""
|
"""
|
||||||
ext = file_path.suffix.lower()
|
ext = file_path.suffix.lower()
|
||||||
if ext in ACCESS_EXTENSIONS:
|
if ext in ACCESS_EXTENSIONS:
|
||||||
return 'access'
|
return 'access'
|
||||||
return 'excel'
|
if ext in EXCEL_EXTENSIONS:
|
||||||
|
return 'excel'
|
||||||
|
raise ValueError(f"不支持的文件类型: {ext}(支持: .xlsm, .xls, .xlsb, .accdb, .mdb)")
|
||||||
|
|
||||||
|
|
||||||
class VBAExtractor:
|
class VBAExtractor:
|
||||||
@@ -461,15 +466,14 @@ def main():
|
|||||||
print()
|
print()
|
||||||
else:
|
else:
|
||||||
# 交互模式:查找支持的文件
|
# 交互模式:查找支持的文件
|
||||||
excel_dir = Path("Excel")
|
all_files = []
|
||||||
if not excel_dir.exists():
|
for scan_dir in [Path("Excel"), Path("Access")]:
|
||||||
print("错误: 未找到Excel文件夹")
|
if scan_dir.exists():
|
||||||
return
|
for ext in ["*.xlsm", "*.accdb", "*.mdb"]:
|
||||||
|
all_files.extend(scan_dir.glob(ext))
|
||||||
|
|
||||||
# 同时扫描 Excel 和 Access 文件
|
|
||||||
all_files = list(excel_dir.glob("*.xlsm")) + list(excel_dir.glob("*.accdb")) + list(excel_dir.glob("*.mdb"))
|
|
||||||
if not all_files:
|
if not all_files:
|
||||||
print("错误: Excel文件夹中没有.xlsm或.accdb文件")
|
print("错误: 未找到.xlsm或.accdb文件(请检查Excel/或Access/文件夹)")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 如果有多个文件,让用户选择
|
# 如果有多个文件,让用户选择
|
||||||
|
|||||||
@@ -50,11 +50,16 @@ def get_file_type(file_path: Path) -> str:
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
'access' 或 'excel'
|
'access' 或 'excel'
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: 不支持的文件扩展名
|
||||||
"""
|
"""
|
||||||
ext = file_path.suffix.lower()
|
ext = file_path.suffix.lower()
|
||||||
if ext in ACCESS_EXTENSIONS:
|
if ext in ACCESS_EXTENSIONS:
|
||||||
return 'access'
|
return 'access'
|
||||||
return 'excel'
|
if ext in EXCEL_EXTENSIONS:
|
||||||
|
return 'excel'
|
||||||
|
raise ValueError(f"不支持的文件类型: {ext}(支持: .xlsm, .xls, .xlsb, .accdb, .mdb)")
|
||||||
|
|
||||||
|
|
||||||
class VBAImporter:
|
class VBAImporter:
|
||||||
@@ -411,8 +416,7 @@ class VBAImporter:
|
|||||||
|
|
||||||
print("\n正在保存...")
|
print("\n正在保存...")
|
||||||
try:
|
try:
|
||||||
import time
|
access.DoCmd.Save()
|
||||||
time.sleep(1) # 等待 VBE 完成
|
|
||||||
print("已保存更改。")
|
print("已保存更改。")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"保存时出错: {e}")
|
print(f"保存时出错: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user