added multi img result render
This commit is contained in:
parent
21941cee89
commit
b25bf04498
137
tools/tools.go
137
tools/tools.go
@ -137,8 +137,8 @@ func handleAnswer(file *excelize.File, sheet, cell, s3Prefix string, answer mode
|
|||||||
noAccept := make(map[string]struct{})
|
noAccept := make(map[string]struct{})
|
||||||
todoMap := make(map[string]string)
|
todoMap := make(map[string]string)
|
||||||
|
|
||||||
if tipe != "Text" && question.Type == model.TypeImages || question.Type == model.TypeVarImages {
|
if tipe != "Text" && (question.Type == model.TypeImages || question.Type == model.TypeVarImages) {
|
||||||
handleImage(file, sheet, cell, answer.Content, count, row, noAccept, todoMap)
|
handleImage(file, sheet, cell, answer.Content, count, row, noAccept, todoMap, question.Title)
|
||||||
} else if question.Type == model.TypeFile {
|
} else if question.Type == model.TypeFile {
|
||||||
handleFile(file, sheet, cell, answer.Content, s3Prefix, noAccept)
|
handleFile(file, sheet, cell, answer.Content, s3Prefix, noAccept)
|
||||||
} else {
|
} else {
|
||||||
@ -154,32 +154,129 @@ func handleAnswer(file *excelize.File, sheet, cell, s3Prefix string, answer mode
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleImage(file *excelize.File, sheet, cell, content string, count, row int, noAccept map[string]struct{}, todoMap map[string]string) {
|
func handleImage(file *excelize.File, sheet, cell, content string, count, row int, noAccept map[string]struct{}, todoMap map[string]string, questionTitle string) {
|
||||||
var res model.ImageContent
|
multiImgArr := strings.Split(content, "`,`")
|
||||||
err := json.Unmarshal([]byte(content), &res)
|
if len(multiImgArr) > 1 {
|
||||||
if err != nil {
|
var descriptions []string
|
||||||
res.Image = content
|
mediaSheet := "Media"
|
||||||
}
|
flag, err := file.GetSheetIndex(mediaSheet)
|
||||||
urle := ExtractImageURL(res.Image)
|
if err != nil {
|
||||||
urlData := strings.Split(urle, " ")
|
fmt.Println(err.Error())
|
||||||
if len(urlData) == 1 {
|
}
|
||||||
u, err := url.Parse(urle)
|
if flag == -1 {
|
||||||
if err == nil && u.Scheme != "" && u.Host != "" {
|
_, _ = file.NewSheet(mediaSheet)
|
||||||
picture, err := downloadImage(urle)
|
err = file.SetCellValue(mediaSheet, "A1", "Вопрос")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50)
|
}
|
||||||
file.SetRowHeight(sheet, row, 150)
|
|
||||||
if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil {
|
mediaRow := row
|
||||||
|
for i, imgContent := range multiImgArr {
|
||||||
|
if i == 0 && len(imgContent) > 1 && imgContent[0] == '`' {
|
||||||
|
imgContent = imgContent[1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
if i == len(multiImgArr)-1 && len(imgContent) > 1 && imgContent[len(imgContent)-1] == '`' {
|
||||||
|
imgContent = imgContent[:len(imgContent)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
var res model.ImageContent
|
||||||
|
err := json.Unmarshal([]byte(imgContent), &res)
|
||||||
|
if err != nil {
|
||||||
|
res.Image = imgContent
|
||||||
|
}
|
||||||
|
|
||||||
|
// чек на пустой дескрипшен, есмли пустой то отмечаем как вариант ответа номер по i
|
||||||
|
if res.Description != "" {
|
||||||
|
descriptions = append(descriptions, res.Description)
|
||||||
|
} else {
|
||||||
|
descriptions = append(descriptions, fmt.Sprintf("Вариант ответа №%d", i+1))
|
||||||
|
}
|
||||||
|
|
||||||
|
urle := ExtractImageURL(res.Image)
|
||||||
|
urlData := strings.Split(urle, " ")
|
||||||
|
if len(urlData) == 1 {
|
||||||
|
u, err := url.Parse(urle)
|
||||||
|
if err == nil && u.Scheme != "" && u.Host != "" {
|
||||||
|
picture, err := downloadImage(urle)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
err = file.SetCellValue(mediaSheet, "A"+strconv.Itoa(mediaRow), questionTitle)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
col := ToAlphaString(i + 2)
|
||||||
|
err = file.SetColWidth(mediaSheet, col, col, 50)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
err = file.SetRowHeight(mediaSheet, mediaRow, 150)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
if err := file.AddPictureFromBytes(mediaSheet, col+strconv.Itoa(mediaRow), picture); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
noAccept[content] = struct{}{}
|
||||||
|
} else {
|
||||||
|
todoMap[content] = cell
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
todoMap[imgContent] = cell
|
||||||
|
}
|
||||||
|
|
||||||
|
descriptionsStr := strings.Join(descriptions, "`,`")
|
||||||
|
linkText := fmt.Sprintf("%s, Приложение: %s!A%d", descriptionsStr, mediaSheet, mediaRow)
|
||||||
|
|
||||||
|
if err := file.SetCellValue(sheet, cell, linkText); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
}
|
}
|
||||||
noAccept[content] = struct{}{}
|
if err := file.SetCellHyperLink(sheet, cell, fmt.Sprintf("%s!A%d", mediaSheet, mediaRow), "Location", excelize.HyperlinkOpts{
|
||||||
|
Display: &linkText,
|
||||||
|
}); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if len(content) > 1 && content[0] == '`' && content[len(content)-1] == '`' {
|
||||||
|
content = content[1 : len(content)-1]
|
||||||
|
}
|
||||||
|
var res model.ImageContent
|
||||||
|
err := json.Unmarshal([]byte(content), &res)
|
||||||
|
if err != nil {
|
||||||
|
res.Image = content
|
||||||
|
}
|
||||||
|
urle := ExtractImageURL(res.Image)
|
||||||
|
urlData := strings.Split(urle, " ")
|
||||||
|
if len(urlData) == 1 {
|
||||||
|
u, err := url.Parse(urle)
|
||||||
|
if err == nil && u.Scheme != "" && u.Host != "" {
|
||||||
|
picture, err := downloadImage(urle)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
err = file.SetColWidth(sheet, ToAlphaString(count), ToAlphaString(count), 50)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
err = file.SetRowHeight(sheet, row, 150)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
if err := file.AddPictureFromBytes(sheet, cell, picture); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
noAccept[content] = struct{}{}
|
||||||
|
} else {
|
||||||
|
todoMap[content] = cell
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
todoMap[content] = cell
|
todoMap[content] = cell
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
todoMap[content] = cell
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user