xlsx.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. // for i, row := range sheet.Rows {
  97. // if i < 20 {
  98. // continue
  99. // }
  100. // if len(row.Cells) < 20 {
  101. // continue
  102. // }
  103. // if row.Cells[2].String() == "" || row.Cells[2].String() == "ИТОГО" {
  104. // continue
  105. // }
  106. // if strings.HasPrefix(row.Cells[1].String(), "Консультации") || strings.HasPrefix(row.Cells[1].String(), "Государственная") {
  107. // continue
  108. // }
  109. // splited := strings.Split(row.Cells[1].String(), ".")
  110. // if len(splited) != 2 {
  111. // continue
  112. // }
  113. // typerow := TypeRow(IsSubTR)
  114. // switch {
  115. // case strings.Contains(row.Cells[2].String(), "цикл"):
  116. // typerow = IsCycleTR
  117. // case splited[1] == "00":
  118. // typerow = IsStartTR
  119. // case splited[0] == "ПМ":
  120. // typerow = IsPmTR
  121. // }
  122. // max, _ := row.Cells[4].Int()
  123. // selfstudy, _ := row.Cells[5].Int()
  124. // allstudy, _ := row.Cells[6].Int()
  125. // lectures, _ := row.Cells[7].Int()
  126. // labs, _ := row.Cells[8].Int()
  127. // projects, _ := row.Cells[9].Int()
  128. // c1s1, _ := row.Cells[12].Int()
  129. // c1s2, _ := row.Cells[13].Int()
  130. // c2s1, _ := row.Cells[14].Int()
  131. // c2s2, _ := row.Cells[15].Int()
  132. // c3s1, _ := row.Cells[16].Int()
  133. // c3s2, _ := row.Cells[17].Int()
  134. // c4s1, _ := row.Cells[18].Int()
  135. // c4s2, _ := row.Cells[19].Int()
  136. // certform := ""
  137. // splited = strings.Split(row.Cells[3].String(), ",")
  138. // for _, v := range splited {
  139. // if v != "-" {
  140. // certform = v
  141. // break
  142. // }
  143. // }
  144. // plan.Lines = append(plan.Lines, LineXLSX{
  145. // ID: row.Cells[1].String(),
  146. // Name: row.Cells[2].String(),
  147. // CertForm: certform,
  148. // StudyLoad: StudyLoadXLSX{
  149. // Max: max,
  150. // SelfStudy: selfstudy,
  151. // AllStudy: allstudy,
  152. // Lectures: lectures,
  153. // Labs: labs,
  154. // Projects: projects,
  155. // },
  156. // Course: [4][2]int{
  157. // [2]int{
  158. // c1s1,
  159. // c1s2,
  160. // },
  161. // [2]int{
  162. // c2s1,
  163. // c2s2,
  164. // },
  165. // [2]int{
  166. // c3s1,
  167. // c3s2,
  168. // },
  169. // [2]int{
  170. // c4s1,
  171. // c4s2,
  172. // },
  173. // },
  174. // TypeRow: typerow,
  175. // })
  176. // }
  177. listOfPlans = append(listOfPlans, plan)
  178. }
  179. return listOfPlans
  180. return nil
  181. }
  182. func CreateXLSX(filename string) (*xlsx.File, string) {
  183. file := xlsx.NewFile()
  184. _, err := file.AddSheet("Init")
  185. if err != nil {
  186. return nil, ""
  187. }
  188. err = file.Save(filename)
  189. if err != nil {
  190. return nil, ""
  191. }
  192. return file, filename
  193. }
  194. func (gen *Generator) WriteXLSX(file *xlsx.File, filename string, schedule []*Schedule, iter int) error {
  195. const (
  196. colWidth = 30
  197. rowHeight = 30
  198. MAXCOL = 3
  199. )
  200. rowsNext := uint(len(schedule)) / MAXCOL
  201. if rowsNext == 0 || uint(len(schedule)) % MAXCOL != 0 {
  202. rowsNext += 1
  203. }
  204. var (
  205. colNum = uint(NUM_TABLES + 2)
  206. row = make([]*xlsx.Row, colNum * rowsNext) // * (rowsNext + 1)
  207. cell *xlsx.Cell
  208. dayN = gen.Day
  209. day = ""
  210. )
  211. if dayN == SUNDAY {
  212. dayN = SATURDAY
  213. } else {
  214. dayN -= 1
  215. }
  216. switch dayN {
  217. case SUNDAY: day = "Sunday"
  218. case MONDAY: day = "Monday"
  219. case TUESDAY: day = "Tuesday"
  220. case WEDNESDAY: day = "Wednesday"
  221. case THURSDAY: day = "Thursday"
  222. case FRIDAY: day = "Friday"
  223. case SATURDAY: day = "Saturday"
  224. }
  225. sheet, err := file.AddSheet(day + "-" + strconv.Itoa(iter))
  226. if err != nil {
  227. return err
  228. }
  229. sheet.SetColWidth(2, int(MAXCOL)*3+1, COL_W)
  230. for r := uint(0); r < rowsNext; r++ {
  231. for i := uint(0); i < colNum; i++ {
  232. row[(r*colNum)+i] = sheet.AddRow() // (r*rowsNext)+
  233. row[(r*colNum)+i].SetHeight(ROW_H)
  234. cell = row[(r*colNum)+i].AddCell()
  235. if i == 0 {
  236. cell.Value = "Пара"
  237. continue
  238. }
  239. cell.Value = strconv.Itoa(int(i-1))
  240. }
  241. }
  242. index := uint(0)
  243. exit: for r := uint(0); r < rowsNext; r++ {
  244. for i := uint(0); i < MAXCOL; i++ {
  245. if uint(len(schedule)) <= index {
  246. break exit
  247. }
  248. savedCell := row[(r*colNum)+0].AddCell()
  249. savedCell.Value = "Группа " + schedule[index].Group
  250. cell = row[(r*colNum)+0].AddCell()
  251. cell = row[(r*colNum)+0].AddCell()
  252. savedCell.Merge(2, 0)
  253. cell = row[(r*colNum)+1].AddCell()
  254. cell.Value = "Предмет"
  255. cell = row[(r*colNum)+1].AddCell()
  256. cell.Value = "Преподаватель"
  257. cell = row[(r*colNum)+1].AddCell()
  258. cell.Value = "Кабинет"
  259. for j, trow := range schedule[index].Table {
  260. cell = row[(r*colNum)+uint(j)+2].AddCell()
  261. if trow.Subject[A] == trow.Subject[B] {
  262. cell.Value = trow.Subject[A]
  263. } else {
  264. if trow.Subject[A] != "" {
  265. cell.Value = trow.Subject[A] + " (A)"
  266. }
  267. if trow.Subject[B] != "" {
  268. cell.Value += "\n" + trow.Subject[B] + " (B)"
  269. }
  270. }
  271. cell = row[(r*colNum)+uint(j)+2].AddCell()
  272. if trow.Teacher[A] == trow.Teacher[B] {
  273. cell.Value = trow.Teacher[A]
  274. } else {
  275. if trow.Teacher[A] != "" {
  276. cell.Value = trow.Teacher[A]
  277. }
  278. if trow.Teacher[B] != "" {
  279. cell.Value += "\n" + trow.Teacher[B]
  280. }
  281. }
  282. sheet.SetColWidth(colWidthForCabinets(int(j)))
  283. cell = row[(r*colNum)+uint(j)+2].AddCell()
  284. if trow.Cabinet[A] == trow.Cabinet[B] {
  285. cell.Value = trow.Cabinet[A]
  286. } else {
  287. if trow.Cabinet[A] != "" {
  288. cell.Value = trow.Cabinet[A]
  289. }
  290. if trow.Cabinet[B] != "" {
  291. cell.Value += "\n" + trow.Cabinet[B]
  292. }
  293. }
  294. }
  295. index++
  296. }
  297. }
  298. err = file.Save(filename)
  299. if err != nil {
  300. return err
  301. }
  302. return nil
  303. }