xlsx.go 7.7 KB

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