sheetview.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2016 - 2019 The excelize Authors. All rights reserved. Use of
  2. // this source code is governed by a BSD-style license that can be found in
  3. // the LICENSE file.
  4. //
  5. // Package excelize providing a set of functions that allow you to write to
  6. // and read from XLSX files. Support reads and writes XLSX file generated by
  7. // Microsoft Excel™ 2007 and later. Support save file without losing original
  8. // charts of XLSX. This library needs Go version 1.8 or later.
  9. package excelize
  10. import "fmt"
  11. // SheetViewOption is an option of a view of a worksheet. See SetSheetViewOptions().
  12. type SheetViewOption interface {
  13. setSheetViewOption(view *xlsxSheetView)
  14. }
  15. // SheetViewOptionPtr is a writable SheetViewOption. See GetSheetViewOptions().
  16. type SheetViewOptionPtr interface {
  17. SheetViewOption
  18. getSheetViewOption(view *xlsxSheetView)
  19. }
  20. type (
  21. // DefaultGridColor is a SheetViewOption.
  22. DefaultGridColor bool
  23. // RightToLeft is a SheetViewOption.
  24. RightToLeft bool
  25. // ShowFormulas is a SheetViewOption.
  26. ShowFormulas bool
  27. // ShowGridLines is a SheetViewOption.
  28. ShowGridLines bool
  29. // ShowRowColHeaders is a SheetViewOption.
  30. ShowRowColHeaders bool
  31. // ZoomScale is a SheetViewOption.
  32. ZoomScale float64
  33. // TopLeftCell is a SheetViewOption.
  34. TopLeftCell string
  35. /* TODO
  36. // ShowWhiteSpace is a SheetViewOption.
  37. ShowWhiteSpace bool
  38. // ShowZeros is a SheetViewOption.
  39. ShowZeros bool
  40. // WindowProtection is a SheetViewOption.
  41. WindowProtection bool
  42. */
  43. )
  44. // Defaults for each option are described in XML schema for CT_SheetView
  45. func (o TopLeftCell) setSheetViewOption(view *xlsxSheetView) {
  46. view.TopLeftCell = string(o)
  47. }
  48. func (o *TopLeftCell) getSheetViewOption(view *xlsxSheetView) {
  49. *o = TopLeftCell(string(view.TopLeftCell))
  50. }
  51. func (o DefaultGridColor) setSheetViewOption(view *xlsxSheetView) {
  52. view.DefaultGridColor = boolPtr(bool(o))
  53. }
  54. func (o *DefaultGridColor) getSheetViewOption(view *xlsxSheetView) {
  55. *o = DefaultGridColor(defaultTrue(view.DefaultGridColor)) // Excel default: true
  56. }
  57. func (o RightToLeft) setSheetViewOption(view *xlsxSheetView) {
  58. view.RightToLeft = bool(o) // Excel default: false
  59. }
  60. func (o *RightToLeft) getSheetViewOption(view *xlsxSheetView) {
  61. *o = RightToLeft(view.RightToLeft)
  62. }
  63. func (o ShowFormulas) setSheetViewOption(view *xlsxSheetView) {
  64. view.ShowFormulas = bool(o) // Excel default: false
  65. }
  66. func (o *ShowFormulas) getSheetViewOption(view *xlsxSheetView) {
  67. *o = ShowFormulas(view.ShowFormulas) // Excel default: false
  68. }
  69. func (o ShowGridLines) setSheetViewOption(view *xlsxSheetView) {
  70. view.ShowGridLines = boolPtr(bool(o))
  71. }
  72. func (o *ShowGridLines) getSheetViewOption(view *xlsxSheetView) {
  73. *o = ShowGridLines(defaultTrue(view.ShowGridLines)) // Excel default: true
  74. }
  75. func (o ShowRowColHeaders) setSheetViewOption(view *xlsxSheetView) {
  76. view.ShowRowColHeaders = boolPtr(bool(o))
  77. }
  78. func (o *ShowRowColHeaders) getSheetViewOption(view *xlsxSheetView) {
  79. *o = ShowRowColHeaders(defaultTrue(view.ShowRowColHeaders)) // Excel default: true
  80. }
  81. func (o ZoomScale) setSheetViewOption(view *xlsxSheetView) {
  82. //This attribute is restricted to values ranging from 10 to 400.
  83. if float64(o) >= 10 && float64(o) <= 400 {
  84. view.ZoomScale = float64(o)
  85. }
  86. }
  87. func (o *ZoomScale) getSheetViewOption(view *xlsxSheetView) {
  88. *o = ZoomScale(view.ZoomScale)
  89. }
  90. // getSheetView returns the SheetView object
  91. func (f *File) getSheetView(sheetName string, viewIndex int) (*xlsxSheetView, error) {
  92. xlsx := f.workSheetReader(sheetName)
  93. if viewIndex < 0 {
  94. if viewIndex < -len(xlsx.SheetViews.SheetView) {
  95. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  96. }
  97. viewIndex = len(xlsx.SheetViews.SheetView) + viewIndex
  98. } else if viewIndex >= len(xlsx.SheetViews.SheetView) {
  99. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  100. }
  101. return &(xlsx.SheetViews.SheetView[viewIndex]), nil
  102. }
  103. // SetSheetViewOptions sets sheet view options.
  104. // The viewIndex may be negative and if so is counted backward (-1 is the last view).
  105. //
  106. // Available options:
  107. // DefaultGridColor(bool)
  108. // RightToLeft(bool)
  109. // ShowFormulas(bool)
  110. // ShowGridLines(bool)
  111. // ShowRowColHeaders(bool)
  112. // Example:
  113. // err = f.SetSheetViewOptions("Sheet1", -1, ShowGridLines(false))
  114. func (f *File) SetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOption) error {
  115. view, err := f.getSheetView(name, viewIndex)
  116. if err != nil {
  117. return err
  118. }
  119. for _, opt := range opts {
  120. opt.setSheetViewOption(view)
  121. }
  122. return nil
  123. }
  124. // GetSheetViewOptions gets the value of sheet view options.
  125. // The viewIndex may be negative and if so is counted backward (-1 is the last view).
  126. //
  127. // Available options:
  128. // DefaultGridColor(bool)
  129. // RightToLeft(bool)
  130. // ShowFormulas(bool)
  131. // ShowGridLines(bool)
  132. // ShowRowColHeaders(bool)
  133. // Example:
  134. // var showGridLines excelize.ShowGridLines
  135. // err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
  136. func (f *File) GetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOptionPtr) error {
  137. view, err := f.getSheetView(name, viewIndex)
  138. if err != nil {
  139. return err
  140. }
  141. for _, opt := range opts {
  142. opt.getSheetViewOption(view)
  143. }
  144. return nil
  145. }