xlsx.go 7.7 KB

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