xlsx.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.Rows[5].Cells[1].String()
  19. // plan.NameCode = strings.Split(sheet.Rows[5].Cells[6].String(), " ")[0]
  20. // plan.PeriodOfStudy = sheet.Rows[7].Cells[6].String()
  21. // splited := strings.Split(sheet.Rows[9].Cells[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. }
  108. func CreateXLSX(filename string) (*xlsx.File, string) {
  109. file := xlsx.NewFile()
  110. _, err := file.AddSheet("Init")
  111. if err != nil {
  112. return nil, ""
  113. }
  114. err = file.Save(filename)
  115. if err != nil {
  116. return nil, ""
  117. }
  118. return file, filename
  119. }
  120. func (gen *Generator) WriteXLSX(file *xlsx.File, filename string, schedule []*Schedule, iter int) error {
  121. const (
  122. colWidth = 30
  123. rowHeight = 30
  124. MAXCOL = 3
  125. )
  126. rowsNext := uint(len(schedule)) / MAXCOL
  127. if rowsNext == 0 || uint(len(schedule)) % MAXCOL != 0 {
  128. rowsNext += 1
  129. }
  130. var (
  131. colNum = uint(NUM_TABLES + 2)
  132. row = make([]*xlsx.Row, colNum * rowsNext) // * (rowsNext + 1)
  133. cell *xlsx.Cell
  134. dayN = gen.Day
  135. day = ""
  136. )
  137. if dayN == SUNDAY {
  138. dayN = SATURDAY
  139. } else {
  140. dayN -= 1
  141. }
  142. switch dayN {
  143. case SUNDAY: day = "Sunday"
  144. case MONDAY: day = "Monday"
  145. case TUESDAY: day = "Tuesday"
  146. case WEDNESDAY: day = "Wednesday"
  147. case THURSDAY: day = "Thursday"
  148. case FRIDAY: day = "Friday"
  149. case SATURDAY: day = "Saturday"
  150. }
  151. sheet, err := file.AddSheet(day + "-" + strconv.Itoa(iter))
  152. if err != nil {
  153. return err
  154. }
  155. sheet.SetColWidth(2, int(MAXCOL)*3+1, COL_W)
  156. for r := uint(0); r < rowsNext; r++ {
  157. for i := uint(0); i < colNum; i++ {
  158. row[(r*colNum)+i] = sheet.AddRow() // (r*rowsNext)+
  159. row[(r*colNum)+i].SetHeight(ROW_H)
  160. cell = row[(r*colNum)+i].AddCell()
  161. if i == 0 {
  162. cell.Value = "Пара"
  163. continue
  164. }
  165. cell.Value = strconv.Itoa(int(i-1))
  166. }
  167. }
  168. index := uint(0)
  169. exit: for r := uint(0); r < rowsNext; r++ {
  170. for i := uint(0); i < MAXCOL; i++ {
  171. if uint(len(schedule)) <= index {
  172. break exit
  173. }
  174. savedCell := row[(r*colNum)+0].AddCell()
  175. savedCell.Value = "Группа " + schedule[index].Group
  176. cell = row[(r*colNum)+0].AddCell()
  177. cell = row[(r*colNum)+0].AddCell()
  178. savedCell.Merge(2, 0)
  179. cell = row[(r*colNum)+1].AddCell()
  180. cell.Value = "Предмет"
  181. cell = row[(r*colNum)+1].AddCell()
  182. cell.Value = "Преподаватель"
  183. cell = row[(r*colNum)+1].AddCell()
  184. cell.Value = "Кабинет"
  185. for j, trow := range schedule[index].Table {
  186. cell = row[(r*colNum)+uint(j)+2].AddCell()
  187. if trow.Subject[A] == trow.Subject[B] {
  188. cell.Value = trow.Subject[A]
  189. } else {
  190. if trow.Subject[A] != "" {
  191. cell.Value = trow.Subject[A] + " (A)"
  192. }
  193. if trow.Subject[B] != "" {
  194. cell.Value += "\n" + trow.Subject[B] + " (B)"
  195. }
  196. }
  197. cell = row[(r*colNum)+uint(j)+2].AddCell()
  198. if trow.Teacher[A] == trow.Teacher[B] {
  199. cell.Value = trow.Teacher[A]
  200. } else {
  201. if trow.Teacher[A] != "" {
  202. cell.Value = trow.Teacher[A]
  203. }
  204. if trow.Teacher[B] != "" {
  205. cell.Value += "\n" + trow.Teacher[B]
  206. }
  207. }
  208. sheet.SetColWidth(colWidthForCabinets(int(j)))
  209. cell = row[(r*colNum)+uint(j)+2].AddCell()
  210. if trow.Cabinet[A] == trow.Cabinet[B] {
  211. cell.Value = trow.Cabinet[A]
  212. } else {
  213. if trow.Cabinet[A] != "" {
  214. cell.Value = trow.Cabinet[A]
  215. }
  216. if trow.Cabinet[B] != "" {
  217. cell.Value += "\n" + trow.Cabinet[B]
  218. }
  219. }
  220. }
  221. index++
  222. }
  223. }
  224. err = file.Save(filename)
  225. if err != nil {
  226. return err
  227. }
  228. return nil
  229. }