test_quick.py•2.58 kB
#!/usr/bin/env python3
"""
快速测试Excel MCP功能
"""
import sys
import os
from pathlib import Path
# 添加src目录到Python路径
src_path = Path(__file__).parent / "src"
sys.path.insert(0, str(src_path))
from excel_mcp.server import (
initialize_components,
create_excel_file,
write_excel_file,
read_excel_file,
get_excel_info
)
def quick_test():
"""快速功能测试"""
print("🚀 Excel MCP快速功能测试")
print("=" * 50)
# 初始化组件
initialize_components()
# 测试文件路径
test_file = "data/quick_test.xlsx"
# 1. 创建Excel文件
print("\n1. 创建Excel文件...")
result = create_excel_file(test_file, ["测试数据", "统计"])
if result.get("success"):
print(" ✅ Excel文件创建成功")
else:
print(f" ❌ 创建失败: {result.get('error')}")
return
# 2. 写入测试数据
print("\n2. 写入测试数据...")
test_data = [
["产品名称", "单价", "数量", "总价"],
["苹果", 3.5, 50, "=B2*C2"],
["香蕉", 2.8, 80, "=B3*C3"],
["橙子", 4.2, 30, "=B4*C4"]
]
result = write_excel_file(test_file, test_data, "测试数据", "A1")
if result.get("success"):
print(" ✅ 数据写入成功")
else:
print(f" ❌ 写入失败: {result.get('error')}")
return
# 3. 读取数据验证
print("\n3. 读取数据验证...")
result = read_excel_file(test_file, "测试数据")
if result.get("success"):
print(" ✅ 数据读取成功")
print(f" 📊 读取到 {len(result['data'])} 行数据")
for i, row in enumerate(result['data'][:3]): # 显示前3行
print(f" 第{i+1}行: {row}")
else:
print(f" ❌ 读取失败: {result.get('error')}")
return
# 4. 获取文件信息
print("\n4. 获取文件信息...")
result = get_excel_info(test_file)
if result.get("success"):
print(" ✅ 文件信息获取成功")
print(f" 📋 工作表: {result['sheet_names']}")
print(f" 📁 文件大小: {result.get('file_size', 'unknown')} 字节")
else:
print(f" ❌ 获取信息失败: {result.get('error')}")
print("\n🎉 所有测试完成!Excel MCP服务器运行正常。")
print("\n💡 提示:现在你可以在Claude Desktop中使用Excel功能了!")
print(" 例如:'请读取 data/quick_test.xlsx 文件的内容'")
if __name__ == "__main__":
quick_test()