|
| 1 | +package excelize_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mohae/deepcopy" |
| 9 | + |
| 10 | + "github.com/360EntSecGroup-Skylar/excelize" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = []excelize.SheetPrOption{ |
| 14 | + excelize.CodeName("hello"), |
| 15 | + excelize.EnableFormatConditionsCalculation(false), |
| 16 | + excelize.Published(false), |
| 17 | + excelize.FitToPage(true), |
| 18 | + excelize.AutoPageBreaks(true), |
| 19 | +} |
| 20 | + |
| 21 | +var _ = []excelize.SheetPrOptionPtr{ |
| 22 | + (*excelize.CodeName)(nil), |
| 23 | + (*excelize.EnableFormatConditionsCalculation)(nil), |
| 24 | + (*excelize.Published)(nil), |
| 25 | + (*excelize.FitToPage)(nil), |
| 26 | + (*excelize.AutoPageBreaks)(nil), |
| 27 | +} |
| 28 | + |
| 29 | +func ExampleFile_SetSheetPrOptions() { |
| 30 | + xl := excelize.NewFile() |
| 31 | + const sheet = "Sheet1" |
| 32 | + |
| 33 | + if err := xl.SetSheetPrOptions(sheet, |
| 34 | + excelize.CodeName("code"), |
| 35 | + excelize.EnableFormatConditionsCalculation(false), |
| 36 | + excelize.Published(false), |
| 37 | + excelize.FitToPage(true), |
| 38 | + excelize.AutoPageBreaks(true), |
| 39 | + ); err != nil { |
| 40 | + panic(err) |
| 41 | + } |
| 42 | + // Output: |
| 43 | +} |
| 44 | + |
| 45 | +func ExampleFile_GetSheetPrOptions() { |
| 46 | + xl := excelize.NewFile() |
| 47 | + const sheet = "Sheet1" |
| 48 | + |
| 49 | + var ( |
| 50 | + codeName excelize.CodeName |
| 51 | + enableFormatConditionsCalculation excelize.EnableFormatConditionsCalculation |
| 52 | + published excelize.Published |
| 53 | + fitToPage excelize.FitToPage |
| 54 | + autoPageBreaks excelize.AutoPageBreaks |
| 55 | + ) |
| 56 | + |
| 57 | + if err := xl.GetSheetPrOptions(sheet, |
| 58 | + &codeName, |
| 59 | + &enableFormatConditionsCalculation, |
| 60 | + &published, |
| 61 | + &fitToPage, |
| 62 | + &autoPageBreaks, |
| 63 | + ); err != nil { |
| 64 | + panic(err) |
| 65 | + } |
| 66 | + fmt.Println("Defaults:") |
| 67 | + fmt.Printf("- codeName: %q\n", codeName) |
| 68 | + fmt.Println("- enableFormatConditionsCalculation:", enableFormatConditionsCalculation) |
| 69 | + fmt.Println("- published:", published) |
| 70 | + fmt.Println("- fitToPage:", fitToPage) |
| 71 | + fmt.Println("- autoPageBreaks:", autoPageBreaks) |
| 72 | + // Output: |
| 73 | + // Defaults: |
| 74 | + // - codeName: "" |
| 75 | + // - enableFormatConditionsCalculation: true |
| 76 | + // - published: true |
| 77 | + // - fitToPage: false |
| 78 | + // - autoPageBreaks: false |
| 79 | +} |
| 80 | + |
| 81 | +func TestSheetPrOptions(t *testing.T) { |
| 82 | + const sheet = "Sheet1" |
| 83 | + for _, test := range []struct { |
| 84 | + container excelize.SheetPrOptionPtr |
| 85 | + nonDefault excelize.SheetPrOption |
| 86 | + }{ |
| 87 | + {new(excelize.CodeName), excelize.CodeName("xx")}, |
| 88 | + {new(excelize.EnableFormatConditionsCalculation), excelize.EnableFormatConditionsCalculation(false)}, |
| 89 | + {new(excelize.Published), excelize.Published(false)}, |
| 90 | + {new(excelize.FitToPage), excelize.FitToPage(true)}, |
| 91 | + {new(excelize.AutoPageBreaks), excelize.AutoPageBreaks(true)}, |
| 92 | + } { |
| 93 | + opt := test.nonDefault |
| 94 | + t.Logf("option %T", opt) |
| 95 | + |
| 96 | + def := deepcopy.Copy(test.container).(excelize.SheetPrOptionPtr) |
| 97 | + val1 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr) |
| 98 | + val2 := deepcopy.Copy(def).(excelize.SheetPrOptionPtr) |
| 99 | + |
| 100 | + xl := excelize.NewFile() |
| 101 | + // Get the default value |
| 102 | + if err := xl.GetSheetPrOptions(sheet, def); err != nil { |
| 103 | + t.Fatalf("%T: %s", opt, err) |
| 104 | + } |
| 105 | + // Get again and check |
| 106 | + if err := xl.GetSheetPrOptions(sheet, val1); err != nil { |
| 107 | + t.Fatalf("%T: %s", opt, err) |
| 108 | + } |
| 109 | + if !reflect.DeepEqual(val1, def) { |
| 110 | + t.Fatalf("%T: value should not have changed", opt) |
| 111 | + } |
| 112 | + // Set the same value |
| 113 | + if err := xl.SetSheetPrOptions(sheet, val1); err != nil { |
| 114 | + t.Fatalf("%T: %s", opt, err) |
| 115 | + } |
| 116 | + // Get again and check |
| 117 | + if err := xl.GetSheetPrOptions(sheet, val1); err != nil { |
| 118 | + t.Fatalf("%T: %s", opt, err) |
| 119 | + } |
| 120 | + if !reflect.DeepEqual(val1, def) { |
| 121 | + t.Fatalf("%T: value should not have changed", opt) |
| 122 | + } |
| 123 | + |
| 124 | + // Set a different value |
| 125 | + if err := xl.SetSheetPrOptions(sheet, test.nonDefault); err != nil { |
| 126 | + t.Fatalf("%T: %s", opt, err) |
| 127 | + } |
| 128 | + if err := xl.GetSheetPrOptions(sheet, val1); err != nil { |
| 129 | + t.Fatalf("%T: %s", opt, err) |
| 130 | + } |
| 131 | + // Get again and compare |
| 132 | + if err := xl.GetSheetPrOptions(sheet, val2); err != nil { |
| 133 | + t.Fatalf("%T: %s", opt, err) |
| 134 | + } |
| 135 | + if !reflect.DeepEqual(val2, val1) { |
| 136 | + t.Fatalf("%T: value should not have changed", opt) |
| 137 | + } |
| 138 | + // Value should not be the same as the default |
| 139 | + if reflect.DeepEqual(val1, def) { |
| 140 | + t.Fatalf("%T: value should have changed from default", opt) |
| 141 | + } |
| 142 | + |
| 143 | + // Restore the default value |
| 144 | + if err := xl.SetSheetPrOptions(sheet, def); err != nil { |
| 145 | + t.Fatalf("%T: %s", opt, err) |
| 146 | + } |
| 147 | + if err := xl.GetSheetPrOptions(sheet, val1); err != nil { |
| 148 | + t.Fatalf("%T: %s", opt, err) |
| 149 | + } |
| 150 | + if !reflect.DeepEqual(val1, def) { |
| 151 | + t.Fatalf("%T: value should now be the same as default", opt) |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments