sheetview.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2016 - 2018 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. /* TODO
  34. // ShowWhiteSpace is a SheetViewOption.
  35. ShowWhiteSpace bool
  36. // ShowZeros is a SheetViewOption.
  37. ShowZeros bool
  38. // WindowProtection is a SheetViewOption.
  39. WindowProtection bool
  40. */
  41. )
  42. // Defaults for each option are described in XML schema for CT_SheetView
  43. func (o DefaultGridColor) setSheetViewOption(view *xlsxSheetView) {
  44. view.DefaultGridColor = boolPtr(bool(o))
  45. }
  46. func (o *DefaultGridColor) getSheetViewOption(view *xlsxSheetView) {
  47. *o = DefaultGridColor(defaultTrue(view.DefaultGridColor)) // Excel default: true
  48. }
  49. func (o RightToLeft) setSheetViewOption(view *xlsxSheetView) {
  50. view.RightToLeft = bool(o) // Excel default: false
  51. }
  52. func (o *RightToLeft) getSheetViewOption(view *xlsxSheetView) {
  53. *o = RightToLeft(view.RightToLeft)
  54. }
  55. func (o ShowFormulas) setSheetViewOption(view *xlsxSheetView) {
  56. view.ShowFormulas = bool(o) // Excel default: false
  57. }
  58. func (o *ShowFormulas) getSheetViewOption(view *xlsxSheetView) {
  59. *o = ShowFormulas(view.ShowFormulas) // Excel default: false
  60. }
  61. func (o ShowGridLines) setSheetViewOption(view *xlsxSheetView) {
  62. view.ShowGridLines = boolPtr(bool(o))
  63. }
  64. func (o *ShowGridLines) getSheetViewOption(view *xlsxSheetView) {
  65. *o = ShowGridLines(defaultTrue(view.ShowGridLines)) // Excel default: true
  66. }
  67. func (o ShowRowColHeaders) setSheetViewOption(view *xlsxSheetView) {
  68. view.ShowRowColHeaders = boolPtr(bool(o))
  69. }
  70. func (o *ShowRowColHeaders) getSheetViewOption(view *xlsxSheetView) {
  71. *o = ShowRowColHeaders(defaultTrue(view.ShowRowColHeaders)) // Excel default: true
  72. }
  73. func (o ZoomScale) setSheetViewOption(view *xlsxSheetView) {
  74. //This attribute is restricted to values ranging from 10 to 400.
  75. if float64(o) >= 10 && float64(o) <= 400 {
  76. view.ZoomScale = float64(o)
  77. }
  78. }
  79. func (o *ZoomScale) getSheetViewOption(view *xlsxSheetView) {
  80. *o = ZoomScale(view.ZoomScale)
  81. }
  82. // getSheetView returns the SheetView object
  83. func (f *File) getSheetView(sheetName string, viewIndex int) (*xlsxSheetView, error) {
  84. xlsx := f.workSheetReader(sheetName)
  85. if viewIndex < 0 {
  86. if viewIndex < -len(xlsx.SheetViews.SheetView) {
  87. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  88. }
  89. viewIndex = len(xlsx.SheetViews.SheetView) + viewIndex
  90. } else if viewIndex >= len(xlsx.SheetViews.SheetView) {
  91. return nil, fmt.Errorf("view index %d out of range", viewIndex)
  92. }
  93. return &(xlsx.SheetViews.SheetView[viewIndex]), nil
  94. }
  95. // SetSheetViewOptions sets sheet view options.
  96. // The viewIndex may be negative and if so is counted backward (-1 is the last view).
  97. //
  98. // Available options:
  99. // DefaultGridColor(bool)
  100. // RightToLeft(bool)
  101. // ShowFormulas(bool)
  102. // ShowGridLines(bool)
  103. // ShowRowColHeaders(bool)
  104. // Example:
  105. // err = f.SetSheetViewOptions("Sheet1", -1, ShowGridLines(false))
  106. func (f *File) SetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOption) error {
  107. view, err := f.getSheetView(name, viewIndex)
  108. if err != nil {
  109. return err
  110. }
  111. for _, opt := range opts {
  112. opt.setSheetViewOption(view)
  113. }
  114. return nil
  115. }
  116. // GetSheetViewOptions gets the value of sheet view options.
  117. // The viewIndex may be negative and if so is counted backward (-1 is the last view).
  118. //
  119. // Available options:
  120. // DefaultGridColor(bool)
  121. // RightToLeft(bool)
  122. // ShowFormulas(bool)
  123. // ShowGridLines(bool)
  124. // ShowRowColHeaders(bool)
  125. // Example:
  126. // var showGridLines excelize.ShowGridLines
  127. // err = f.GetSheetViewOptions("Sheet1", -1, &showGridLines)
  128. func (f *File) GetSheetViewOptions(name string, viewIndex int, opts ...SheetViewOptionPtr) error {
  129. view, err := f.getSheetView(name, viewIndex)
  130. if err != nil {
  131. return err
  132. }
  133. for _, opt := range opts {
  134. opt.getSheetViewOption(view)
  135. }
  136. return nil
  137. }