You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.6 KiB
Go

package utils
import (
"docDemo/model"
"errors"
"fmt"
"gitee.ltd/lxh/logger/log"
"github.com/xuri/excelize/v2"
"strings"
)
var AZ = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
type excel struct{}
func ExcelUtils() *excel {
return &excel{}
}
// ParseQuestion 转换Excel为量表问题答案信息
func (e excel) ParseQuestion(ef *excelize.File) (es []model.Question, err error) {
// 获取指定sheet
rows, err := ef.GetRows("question")
if err != nil {
return
}
log.Debugf("读取成功,共%d行%d列数据", len(rows), len(rows[0]))
// 循环行,处理为结构体,不处理第一行,因为是标题
for i, row := range rows[1:] {
// 如果问题出现空行,终止解析
if row[0] == "" {
break
}
// 创建一个问题结构体
var q model.Question
q.QuestionSerial = i + 1
q.Question = row[0]
// 开始解析答案
var answers []model.AnswerEntity
for j, a := range row[1:] {
// 如果答案出现空行,终止解析
if a == "" {
break
}
// 拆开答案,获取答案和分值
if !strings.Contains(a, "|") {
log.Errorf("异常数据: %v", a)
err = errors.New(fmt.Sprintf("模板文件格式错误(%v行%v列),答案和分值必须以|隔开", i+2, j+2))
return
}
answerInfo := strings.Split(a, "|")
answers = append(answers, model.AnswerEntity{
Code: AZ[j],
Answer: answerInfo[0],
Score: StrToInt(answerInfo[1]),
})
}
q.Answers = answers
es = append(es, q)
}
return
}