mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-09 01:36:57 +08:00
fix: improve error message when uploading Excel file with incorrect column count
This commit is contained in:
@@ -17,6 +17,7 @@ from apps.data_training.models.data_training_model import DataTrainingInfo
|
||||
from common.core.config import settings
|
||||
from common.core.deps import SessionDep, CurrentUser, Trans
|
||||
from common.utils.data_format import DataFormat
|
||||
from common.utils.excel import get_excel_column_count
|
||||
|
||||
router = APIRouter(tags=["DataTraining"], prefix="/system/data-training")
|
||||
|
||||
@@ -136,6 +137,9 @@ async def upload_excel(trans: Trans, current_user: CurrentUser, file: UploadFile
|
||||
|
||||
for sheet_name in sheet_names:
|
||||
|
||||
if get_excel_column_count(save_path, sheet_name) < len(use_cols):
|
||||
raise Exception(trans("i18n_excel_import.col_num_not_match"))
|
||||
|
||||
df = pd.read_excel(
|
||||
save_path,
|
||||
sheet_name=sheet_name,
|
||||
|
||||
@@ -17,6 +17,7 @@ from apps.terminology.models.terminology_model import TerminologyInfo
|
||||
from common.core.config import settings
|
||||
from common.core.deps import SessionDep, CurrentUser, Trans
|
||||
from common.utils.data_format import DataFormat
|
||||
from common.utils.excel import get_excel_column_count
|
||||
|
||||
router = APIRouter(tags=["Terminology"], prefix="/system/terminology")
|
||||
|
||||
@@ -133,6 +134,9 @@ async def upload_excel(trans: Trans, current_user: CurrentUser, file: UploadFile
|
||||
|
||||
for sheet_name in sheet_names:
|
||||
|
||||
if get_excel_column_count(save_path, sheet_name) < len(use_cols):
|
||||
raise Exception(trans("i18n_excel_import.col_num_not_match"))
|
||||
|
||||
df = pd.read_excel(
|
||||
save_path,
|
||||
sheet_name=sheet_name,
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import pandas as pd
|
||||
|
||||
def get_excel_column_count(file_path, sheet_name):
|
||||
"""获取Excel文件的列数"""
|
||||
df_temp = pd.read_excel(
|
||||
file_path,
|
||||
sheet_name=sheet_name,
|
||||
engine='calamine',
|
||||
header=0,
|
||||
nrows=0
|
||||
)
|
||||
return len(df_temp.columns)
|
||||
@@ -83,5 +83,8 @@
|
||||
},
|
||||
"i18n_excel_export": {
|
||||
"data_is_empty": "Form data is empty, unable to export data"
|
||||
},
|
||||
"i18n_excel_import": {
|
||||
"col_num_not_match": "Number of columns in Excel does not match"
|
||||
}
|
||||
}
|
||||
@@ -83,5 +83,8 @@
|
||||
},
|
||||
"i18n_excel_export": {
|
||||
"data_is_empty": "폼 데이터가 비어 있어 데이터를 내보낼 수 없습니다"
|
||||
},
|
||||
"i18n_excel_import": {
|
||||
"col_num_not_match": "Excel 열 개수가 일치하지 않습니다"
|
||||
}
|
||||
}
|
||||
@@ -83,5 +83,8 @@
|
||||
},
|
||||
"i18n_excel_export": {
|
||||
"data_is_empty": "表单数据为空,无法导出数据"
|
||||
},
|
||||
"i18n_excel_import": {
|
||||
"col_num_not_match": "EXCEL列数量不匹配"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user