package model import ( "strconv" "strings" excelize "github.com/xuri/excelize/v2" ) type MTParseT struct { QuestionNumber int `json:"question_number"` QuestionCount int `json:"questions_count"` ContentQuestion string `json:"content_question"` Answers []AnswerT `json:"answers"` } type AnswerT struct { Title string `json:"title"` Correct bool `json:"correct"` } func (data *MTType) Parse() MTParseT { var ret MTParseT ret.QuestionNumber = data.QuestionNumber ret.QuestionCount = data.QuestionCount ret.ContentQuestion = data.CurrentQuestion.Content answers := strings.Split(data.ValidAnswers, ",") for _, d := range data.CurrentAnswers { var a AnswerT a.Title = d.Title for _, as := range answers { if d.Id.String() == as { a.Correct = true } } ret.Answers = append(ret.Answers, a) } return ret } func SaveToExel(filename string, data *[]MTParseT) error { f := excelize.NewFile() defer func() { if err := f.Close(); err != nil { return } }() // Create a new sheet. f.SetCellValue("Sheet1", "A2", "№ п/п") f.SetCellValue("Sheet1", "B2", "ВОПРОСЫ") f.SetCellValue("Sheet1", "C2", "Варианты ответа") f.SetCellValue("Sheet1", "D2", "Результат") i := 3 for _, d := range *data { f.SetCellValue("Sheet1", "A"+strconv.Itoa(i), d.QuestionNumber) f.SetCellValue("Sheet1", "B"+strconv.Itoa(i), d.ContentQuestion) for _, a := range d.Answers { f.SetCellValue("Sheet1", "C"+strconv.Itoa(i), a.Title) if a.Correct { f.SetCellValue("Sheet1", "D"+strconv.Itoa(i), "ПРАВИЛЬНЫЙ ОТВЕТ") } else { f.SetCellValue("Sheet1", "D"+strconv.Itoa(i), "Ответ не верный") } i++ } i++ } // Save spreadsheet by the given path. if err := f.SaveAs(filename); err != nil { return err } return nil }