sheetview.go 4.8 KB

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