Skip to content

Commit 8023ce2

Browse files
committed
fix: improve error message when uploading Excel file with incorrect column count
1 parent d916e29 commit 8023ce2

File tree

9 files changed

+62
-6
lines changed

9 files changed

+62
-6
lines changed

backend/apps/data_training/api/data_training.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from common.core.config import settings
1818
from common.core.deps import SessionDep, CurrentUser, Trans
1919
from common.utils.data_format import DataFormat
20+
from common.utils.excel import get_excel_column_count
2021

2122
router = APIRouter(tags=["DataTraining"], prefix="/system/data-training")
2223

@@ -136,6 +137,9 @@ def inner():
136137

137138
for sheet_name in sheet_names:
138139

140+
if get_excel_column_count(save_path, sheet_name) < len(use_cols):
141+
raise Exception(trans("i18n_excel_import.col_num_not_match"))
142+
139143
df = pd.read_excel(
140144
save_path,
141145
sheet_name=sheet_name,

backend/apps/terminology/api/terminology.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from common.core.config import settings
1818
from common.core.deps import SessionDep, CurrentUser, Trans
1919
from common.utils.data_format import DataFormat
20+
from common.utils.excel import get_excel_column_count
2021

2122
router = APIRouter(tags=["Terminology"], prefix="/system/terminology")
2223

@@ -133,6 +134,9 @@ def inner():
133134

134135
for sheet_name in sheet_names:
135136

137+
if get_excel_column_count(save_path, sheet_name) < len(use_cols):
138+
raise Exception(trans("i18n_excel_import.col_num_not_match"))
139+
136140
df = pd.read_excel(
137141
save_path,
138142
sheet_name=sheet_name,

backend/common/utils/excel.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pandas as pd
2+
3+
def get_excel_column_count(file_path, sheet_name):
4+
"""获取Excel文件的列数"""
5+
df_temp = pd.read_excel(
6+
file_path,
7+
sheet_name=sheet_name,
8+
engine='calamine',
9+
header=0,
10+
nrows=0
11+
)
12+
return len(df_temp.columns)

backend/locales/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,8 @@
8383
},
8484
"i18n_excel_export": {
8585
"data_is_empty": "Form data is empty, unable to export data"
86+
},
87+
"i18n_excel_import": {
88+
"col_num_not_match": "Number of columns in Excel does not match"
8689
}
8790
}

backend/locales/ko-KR.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,8 @@
8383
},
8484
"i18n_excel_export": {
8585
"data_is_empty": "폼 데이터가 비어 있어 데이터를 내보낼 수 없습니다"
86+
},
87+
"i18n_excel_import": {
88+
"col_num_not_match": "Excel 열 개수가 일치하지 않습니다"
8689
}
8790
}

backend/locales/zh-CN.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,8 @@
8383
},
8484
"i18n_excel_export": {
8585
"data_is_empty": "表单数据为空,无法导出数据"
86+
},
87+
"i18n_excel_import": {
88+
"col_num_not_match": "EXCEL列数量不匹配"
8689
}
8790
}

frontend/src/views/system/professional/index.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import IconOpeDelete from '@/assets/svg/icon_delete.svg'
1111
import icon_searchOutline_outlined from '@/assets/svg/icon_search-outline_outlined.svg'
1212
import EmptyBackground from '@/views/dashboard/common/EmptyBackground.vue'
1313
import { useI18n } from 'vue-i18n'
14-
import { cloneDeep } from 'lodash-es'
14+
import { cloneDeep, endsWith, startsWith } from 'lodash-es'
1515
import { genFileId, type UploadInstance, type UploadProps, type UploadRawFile } from 'element-plus'
1616
import { useCache } from '@/utils/useCache.ts'
1717
import { settingsApi } from '@/api/setting.ts'
@@ -164,9 +164,18 @@ const onSuccess = (response: any) => {
164164
}
165165
}
166166
167-
const onError = () => {
167+
const onError = (err: Error) => {
168168
uploadLoading.value = false
169169
uploadRef.value!.clearFiles()
170+
let msg = err.message
171+
if (startsWith(msg, '"') && endsWith(msg, '"')) {
172+
msg = msg.slice(1, msg.length - 1)
173+
}
174+
ElMessage({
175+
message: msg,
176+
type: 'error',
177+
showClose: true,
178+
})
170179
}
171180
172181
const exportExcel = () => {

frontend/src/views/system/prompt/index.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import icon_searchOutline_outlined from '@/assets/svg/icon_search-outline_outlin
1313
import EmptyBackground from '@/views/dashboard/common/EmptyBackground.vue'
1414
import { useClipboard } from '@vueuse/core'
1515
import { useI18n } from 'vue-i18n'
16-
import { cloneDeep } from 'lodash-es'
16+
import { cloneDeep, endsWith, startsWith } from 'lodash-es'
1717
import { genFileId, type UploadInstance, type UploadProps, type UploadRawFile } from 'element-plus'
1818
import { settingsApi } from '@/api/setting.ts'
1919
import { useCache } from '@/utils/useCache.ts'
@@ -167,9 +167,18 @@ const onSuccess = (response: any) => {
167167
}
168168
}
169169
170-
const onError = () => {
170+
const onError = (err: Error) => {
171171
uploadLoading.value = false
172172
uploadRef.value!.clearFiles()
173+
let msg = err.message
174+
if (startsWith(msg, '"') && endsWith(msg, '"')) {
175+
msg = msg.slice(1, msg.length - 1)
176+
}
177+
ElMessage({
178+
message: msg,
179+
type: 'error',
180+
showClose: true,
181+
})
173182
}
174183
175184
const exportExcel = () => {

frontend/src/views/system/training/index.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import EmptyBackground from '@/views/dashboard/common/EmptyBackground.vue'
1515
import { useClipboard } from '@vueuse/core'
1616
import { useUserStore } from '@/stores/user'
1717
import { useI18n } from 'vue-i18n'
18-
import { cloneDeep } from 'lodash-es'
18+
import { cloneDeep, endsWith, startsWith } from 'lodash-es'
1919
import { getAdvancedApplicationList } from '@/api/embedded.ts'
2020
import { genFileId } from 'element-plus'
2121
@@ -172,9 +172,18 @@ const onSuccess = (response: any) => {
172172
}
173173
}
174174
175-
const onError = () => {
175+
const onError = (err: Error) => {
176176
uploadLoading.value = false
177177
uploadRef.value!.clearFiles()
178+
let msg = err.message
179+
if (startsWith(msg, '"') && endsWith(msg, '"')) {
180+
msg = msg.slice(1, msg.length - 1)
181+
}
182+
ElMessage({
183+
message: msg,
184+
type: 'error',
185+
showClose: true,
186+
})
178187
}
179188
180189
const exportExcel = () => {

0 commit comments

Comments
 (0)