xlsx.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package schedule
  2. import (
  3. // "fmt"
  4. "strings"
  5. "strconv"
  6. "github.com/tealeg/xlsx"
  7. )
  8. func ImportXLSX(filename string) []*PlanXLSX {
  9. var (
  10. listOfPlans []*PlanXLSX
  11. )
  12. xlFile, err := xlsx.OpenFile(filename)
  13. if err != nil {
  14. return nil
  15. }
  16. for _, sheet := range xlFile.Sheets {
  17. var plan = new(PlanXLSX)
  18. x, _ := sheet.Cell(5, 1)
  19. plan.ProgramCode = x.String()
  20. namecode, _ := sheet.Cell(5,6)
  21. plan.NameCode = strings.Split(namecode.String(), " ")[0]
  22. pof, _ := sheet.Cell(7,6)
  23. plan.PeriodOfStudy = pof.String()
  24. spl, _ := sheet.Cell(9,1)
  25. splited := strings.Split(spl.String(), " ")
  26. plan.GroupNumber = strings.Split(splited[len(splited)-1], "-")[0]
  27. sheet.ForEachRow(func(row *Row) error {
  28. splited := strings.Split(row.Cells[1].String(), ".")
  29. if i >= 20 && len(row.Cells) >= 20 && len(row.Cells)>=20 && !(row.Cells[2].String() == "" || row.Cells[2].String() == "ИТОГО") && !(strings.HasPrefix(row.Cells[1].String(), "Консультации") || strings.HasPrefix(row.Cells[1].String(), "Государственная")) && len(splited) == 2{
  30. typerow := TypeRow(IsSubTR)
  31. switch {
  32. case strings.Contains(row.Cells[2].String(), "цикл"):
  33. typerow = IsCycleTR
  34. case splited[1] == "00":
  35. typerow = IsStartTR
  36. case splited[0] == "ПМ":
  37. typerow = IsPmTR
  38. }
  39. max, _ := row.Cells[4].Int()
  40. selfstudy, _ := row.Cells[5].Int()
  41. allstudy, _ := row.Cells[6].Int()
  42. lectures, _ := row.Cells[7].Int()
  43. labs, _ := row.Cells[8].Int()
  44. projects, _ := row.Cells[9].Int()
  45. c1s1, _ := row.Cells[12].Int()
  46. c1s2, _ := row.Cells[13].Int()
  47. c2s1, _ := row.Cells[14].Int()
  48. c2s2, _ := row.Cells[15].Int()
  49. c3s1, _ := row.Cells[16].Int()
  50. c3s2, _ := row.Cells[17].Int()
  51. c4s1, _ := row.Cells[18].Int()
  52. c4s2, _ := row.Cells[19].Int()
  53. certform := ""
  54. splited = strings.Split(row.Cells[3].String(), ",")
  55. for _, v := range splited {
  56. if v != "-" {
  57. certform = v
  58. break
  59. }
  60. }
  61. plan.Lines = append(plan.Lines, LineXLSX{
  62. ID: row.Cells[1].String(),
  63. Name: row.Cells[2].String(),
  64. CertForm: certform,
  65. StudyLoad: StudyLoadXLSX{
  66. Max: max,
  67. SelfStudy: selfstudy,
  68. AllStudy: allstudy,
  69. Lectures: lectures,
  70. Labs: labs,
  71. Projects: projects,
  72. },
  73. Course: [4][2]int{
  74. [2]int{
  75. c1s1,
  76. c1s2,
  77. },
  78. [2]int{
  79. c2s1,
  80. c2s2,
  81. },
  82. [2]int{
  83. c3s1,
  84. c3s2,
  85. },
  86. [2]int{
  87. c4s1,
  88. c4s2,
  89. },
  90. },
  91. TypeRow: typerow,
  92. })
  93. }
  94. })
  95. listOfPlans = append(listOfPlans, plan)
  96. }
  97. return listOfPlans
  98. }
  99. func CreateXLSX(filename string) (*xlsx.File, string) {
  100. file := xlsx.NewFile()
  101. _, err := file.AddSheet("Init")
  102. if err != nil {
  103. return nil, ""
  104. }
  105. err = file.Save(filename)
  106. if err != nil {
  107. return nil, ""
  108. }
  109. return file, filename
  110. }
  111. func (gen *Generator) WriteXLSX(file *xlsx.File, filename string, schedule []*Schedule, iter int) error {
  112. const (
  113. colWidth = 30
  114. rowHeight = 30
  115. MAXCOL = 3
  116. )
  117. rowsNext := uint(len(schedule)) / MAXCOL
  118. if rowsNext == 0 || uint(len(schedule)) % MAXCOL != 0 {
  119. rowsNext += 1
  120. }
  121. var (
  122. colNum = uint(NUM_TABLES + 2)
  123. row = make([]*xlsx.Row, colNum * rowsNext) // * (rowsNext + 1)
  124. cell *xlsx.Cell
  125. dayN = gen.Day
  126. day = ""
  127. )
  128. if dayN == SUNDAY {
  129. dayN = SATURDAY
  130. } else {
  131. dayN -= 1
  132. }
  133. switch dayN {
  134. case SUNDAY: day = "Sunday"
  135. case MONDAY: day = "Monday"
  136. case TUESDAY: day = "Tuesday"
  137. case WEDNESDAY: day = "Wednesday"
  138. case THURSDAY: day = "Thursday"
  139. case FRIDAY: day = "Friday"
  140. case SATURDAY: day = "Saturday"
  141. }
  142. sheet, err := file.AddSheet(day + "-" + strconv.Itoa(iter))
  143. if err != nil {
  144. return err
  145. }
  146. sheet.SetColWidth(2, int(MAXCOL)*3+1, COL_W)
  147. for r := uint(0); r < rowsNext; r++ {
  148. for i := uint(0); i < colNum; i++ {
  149. row[(r*colNum)+i] = sheet.AddRow() // (r*rowsNext)+
  150. row[(r*colNum)+i].SetHeight(ROW_H)
  151. cell = row[(r*colNum)+i].AddCell()
  152. if i == 0 {
  153. cell.Value = "Пара"
  154. continue
  155. }
  156. cell.Value = strconv.Itoa(int(i-1))
  157. }
  158. }
  159. index := uint(0)
  160. exit: for r := uint(0); r < rowsNext; r++ {
  161. for i := uint(0); i < MAXCOL; i++ {
  162. if uint(len(schedule)) <= index {
  163. break exit
  164. }
  165. savedCell := row[(r*colNum)+0].AddCell()
  166. savedCell.Value = "Группа " + schedule[index].Group
  167. cell = row[(r*colNum)+0].AddCell()
  168. cell = row[(r*colNum)+0].AddCell()
  169. savedCell.Merge(2, 0)
  170. cell = row[(r*colNum)+1].AddCell()
  171. cell.Value = "Предмет"
  172. cell = row[(r*colNum)+1].AddCell()
  173. cell.Value = "Преподаватель"
  174. cell = row[(r*colNum)+1].AddCell()
  175. cell.Value = "Кабинет"
  176. for j, trow := range schedule[index].Table {
  177. cell = row[(r*colNum)+uint(j)+2].AddCell()
  178. if trow.Subject[A] == trow.Subject[B] {
  179. cell.Value = trow.Subject[A]
  180. } else {
  181. if trow.Subject[A] != "" {
  182. cell.Value = trow.Subject[A] + " (A)"
  183. }
  184. if trow.Subject[B] != "" {
  185. cell.Value += "\n" + trow.Subject[B] + " (B)"
  186. }
  187. }
  188. cell = row[(r*colNum)+uint(j)+2].AddCell()
  189. if trow.Teacher[A] == trow.Teacher[B] {
  190. cell.Value = trow.Teacher[A]
  191. } else {
  192. if trow.Teacher[A] != "" {
  193. cell.Value = trow.Teacher[A]
  194. }
  195. if trow.Teacher[B] != "" {
  196. cell.Value += "\n" + trow.Teacher[B]
  197. }
  198. }
  199. sheet.SetColWidth(colWidthForCabinets(int(j)))
  200. cell = row[(r*colNum)+uint(j)+2].AddCell()
  201. if trow.Cabinet[A] == trow.Cabinet[B] {
  202. cell.Value = trow.Cabinet[A]
  203. } else {
  204. if trow.Cabinet[A] != "" {
  205. cell.Value = trow.Cabinet[A]
  206. }
  207. if trow.Cabinet[B] != "" {
  208. cell.Value += "\n" + trow.Cabinet[B]
  209. }
  210. }
  211. }
  212. index++
  213. }
  214. }
  215. err = file.Save(filename)
  216. if err != nil {
  217. return err
  218. }
  219. return nil
  220. }