Support to set summary columns to appear to the right of detail in an outline

- Simplify calculation engine code
- Update documentation for the functions
- Update dependencies module
This commit is contained in:
xuri 2022-10-08 22:08:06 +08:00
parent 57051326d0
commit b1e776ee33
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
11 changed files with 96 additions and 77 deletions

10
calc.go
View File

@ -1132,7 +1132,7 @@ func calcLe(rOpd, lOpd formulaArg, opdStack *Stack) error {
return nil return nil
} }
// calcG evaluate greater than or equal arithmetic operations. // calcG evaluate greater than arithmetic operations.
func calcG(rOpd, lOpd formulaArg, opdStack *Stack) error { func calcG(rOpd, lOpd formulaArg, opdStack *Stack) error {
if rOpd.Type == ArgNumber && lOpd.Type == ArgNumber { if rOpd.Type == ArgNumber && lOpd.Type == ArgNumber {
opdStack.Push(newBoolFormulaArg(lOpd.Number > rOpd.Number)) opdStack.Push(newBoolFormulaArg(lOpd.Number > rOpd.Number))
@ -1287,13 +1287,15 @@ func calculate(opdStack *Stack, opt efp.Token) error {
func (f *File) parseOperatorPrefixToken(optStack, opdStack *Stack, token efp.Token) (err error) { func (f *File) parseOperatorPrefixToken(optStack, opdStack *Stack, token efp.Token) (err error) {
if optStack.Len() == 0 { if optStack.Len() == 0 {
optStack.Push(token) optStack.Push(token)
} else { return
}
tokenPriority := getPriority(token) tokenPriority := getPriority(token)
topOpt := optStack.Peek().(efp.Token) topOpt := optStack.Peek().(efp.Token)
topOptPriority := getPriority(topOpt) topOptPriority := getPriority(topOpt)
if tokenPriority > topOptPriority { if tokenPriority > topOptPriority {
optStack.Push(token) optStack.Push(token)
} else { return
}
for tokenPriority <= topOptPriority { for tokenPriority <= topOptPriority {
optStack.Pop() optStack.Pop()
if err = calculate(opdStack, topOpt); err != nil { if err = calculate(opdStack, topOpt); err != nil {
@ -1307,8 +1309,6 @@ func (f *File) parseOperatorPrefixToken(optStack, opdStack *Stack, token efp.Tok
break break
} }
optStack.Push(token) optStack.Push(token)
}
}
return return
} }

View File

@ -444,7 +444,10 @@ func (f *File) UpdateLinkedValue() error {
// AddVBAProject provides the method to add vbaProject.bin file which contains // AddVBAProject provides the method to add vbaProject.bin file which contains
// functions and/or macros. The file extension should be .xlsm. For example: // functions and/or macros. The file extension should be .xlsm. For example:
// //
// if err := f.SetSheetPrOptions("Sheet1", excelize.CodeName("Sheet1")); err != nil { // codeName := "Sheet1"
// if err := f.SetSheetProps("Sheet1", &excelize.SheetPropsOptions{
// CodeName: &codeName,
// }); err != nil {
// fmt.Println(err) // fmt.Println(err)
// } // }
// if err := f.AddVBAProject("vbaProject.bin"); err != nil { // if err := f.AddVBAProject("vbaProject.bin"); err != nil {

4
go.mod
View File

@ -10,9 +10,9 @@ require (
github.com/stretchr/testify v1.7.1 github.com/stretchr/testify v1.7.1
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 github.com/xuri/efp v0.0.0-20220603152613-6918739fd470
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b golang.org/x/net v0.0.0-20221004154528-8021a29435af
golang.org/x/text v0.3.7 golang.org/x/text v0.3.7
gopkg.in/yaml.v3 v3.0.0 // indirect gopkg.in/yaml.v3 v3.0.0 // indirect
) )

8
go.sum
View File

@ -17,13 +17,13 @@ github.com/xuri/efp v0.0.0-20220603152613-6918739fd470 h1:6932x8ltq1w4utjmfMPVj0
github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI= github.com/xuri/efp v0.0.0-20220603152613-6918739fd470/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 h1:OAmKAfT06//esDdpi/DZ8Qsdt4+M5+ltca05dA5bG2M= github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22 h1:OAmKAfT06//esDdpi/DZ8Qsdt4+M5+ltca05dA5bG2M=
github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ= github.com/xuri/nfp v0.0.0-20220409054826-5e722a1d9e22/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM= golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b h1:huxqepDufQpLLIRXiVkTvnxrzJlpwmIWAObmcCcUFr0=
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 h1:LRtI4W37N+KFebI/qV0OFiLUv4GLOWeEW5hn/KEJvxE= golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 h1:LRtI4W37N+KFebI/qV0OFiLUv4GLOWeEW5hn/KEJvxE=
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM= golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY= golang.org/x/net v0.0.0-20221004154528-8021a29435af h1:wv66FM3rLZGPdxpYL+ApnDe2HzHcTFta3z5nsc13wI4=
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20221004154528-8021a29435af/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -109,7 +109,7 @@ type PivotTableField struct {
// f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), rand.Intn(5000)) // f.SetCellValue("Sheet1", fmt.Sprintf("D%d", row), rand.Intn(5000))
// f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), region[rand.Intn(4)]) // f.SetCellValue("Sheet1", fmt.Sprintf("E%d", row), region[rand.Intn(4)])
// } // }
// if err := f.AddPivotTable(&excelize.PivotTableOption{ // if err := f.AddPivotTable(&excelize.PivotTableOptions{
// DataRange: "Sheet1!$A$1:$E$31", // DataRange: "Sheet1!$A$1:$E$31",
// PivotTableRange: "Sheet1!$G$2:$M$34", // PivotTableRange: "Sheet1!$G$2:$M$34",
// Rows: []excelize.PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}}, // Rows: []excelize.PivotTableField{{Data: "Month", DefaultSubtotal: true}, {Data: "Year"}},

View File

@ -1039,7 +1039,7 @@ func attrValToBool(name string, attrs []xml.Attr) (val bool, err error) {
// //
// For example: // For example:
// //
// err := f.SetHeaderFooter("Sheet1", &excelize.FormatHeaderFooter{ // err := f.SetHeaderFooter("Sheet1", &excelize.HeaderFooterOptions{
// DifferentFirst: true, // DifferentFirst: true,
// DifferentOddEven: true, // DifferentOddEven: true,
// OddHeader: "&R&P", // OddHeader: "&R&P",
@ -1109,7 +1109,7 @@ func (f *File) SetHeaderFooter(sheet string, settings *HeaderFooterOptions) erro
// specified, will be using the XOR algorithm as default. For example, protect // specified, will be using the XOR algorithm as default. For example, protect
// Sheet1 with protection settings: // Sheet1 with protection settings:
// //
// err := f.ProtectSheet("Sheet1", &excelize.FormatSheetProtection{ // err := f.ProtectSheet("Sheet1", &excelize.SheetProtectionOptions{
// AlgorithmName: "SHA-512", // AlgorithmName: "SHA-512",
// Password: "password", // Password: "password",
// EditScenarios: false, // EditScenarios: false,

View File

@ -106,41 +106,55 @@ func (f *File) GetPageMargins(sheet string) (PageLayoutMarginsOptions, error) {
return opts, err return opts, err
} }
// setSheetProps set worksheet format properties by given options. // prepareSheetPr sheetPr element if which not exist.
func (ws *xlsxWorksheet) setSheetProps(opts *SheetPropsOptions) { func (ws *xlsxWorksheet) prepareSheetPr() {
prepareSheetPr := func(ws *xlsxWorksheet) {
if ws.SheetPr == nil { if ws.SheetPr == nil {
ws.SheetPr = new(xlsxSheetPr) ws.SheetPr = new(xlsxSheetPr)
} }
} }
preparePageSetUpPr := func(ws *xlsxWorksheet) {
prepareSheetPr(ws) // setSheetOutlinePr set worksheet outline properties by given options.
if ws.SheetPr.PageSetUpPr == nil { func (ws *xlsxWorksheet) setSheetOutlineProps(opts *SheetPropsOptions) {
ws.SheetPr.PageSetUpPr = new(xlsxPageSetUpPr)
}
}
prepareOutlinePr := func(ws *xlsxWorksheet) { prepareOutlinePr := func(ws *xlsxWorksheet) {
prepareSheetPr(ws) ws.prepareSheetPr()
if ws.SheetPr.OutlinePr == nil { if ws.SheetPr.OutlinePr == nil {
ws.SheetPr.OutlinePr = new(xlsxOutlinePr) ws.SheetPr.OutlinePr = new(xlsxOutlinePr)
} }
} }
if opts.OutlineSummaryBelow != nil {
prepareOutlinePr(ws)
ws.SheetPr.OutlinePr.SummaryBelow = opts.OutlineSummaryBelow
}
if opts.OutlineSummaryRight != nil {
prepareOutlinePr(ws)
ws.SheetPr.OutlinePr.SummaryRight = opts.OutlineSummaryRight
}
}
// setSheetProps set worksheet format properties by given options.
func (ws *xlsxWorksheet) setSheetProps(opts *SheetPropsOptions) {
preparePageSetUpPr := func(ws *xlsxWorksheet) {
ws.prepareSheetPr()
if ws.SheetPr.PageSetUpPr == nil {
ws.SheetPr.PageSetUpPr = new(xlsxPageSetUpPr)
}
}
prepareTabColor := func(ws *xlsxWorksheet) { prepareTabColor := func(ws *xlsxWorksheet) {
prepareSheetPr(ws) ws.prepareSheetPr()
if ws.SheetPr.TabColor == nil { if ws.SheetPr.TabColor == nil {
ws.SheetPr.TabColor = new(xlsxTabColor) ws.SheetPr.TabColor = new(xlsxTabColor)
} }
} }
if opts.CodeName != nil { if opts.CodeName != nil {
prepareSheetPr(ws) ws.prepareSheetPr()
ws.SheetPr.CodeName = *opts.CodeName ws.SheetPr.CodeName = *opts.CodeName
} }
if opts.EnableFormatConditionsCalculation != nil { if opts.EnableFormatConditionsCalculation != nil {
prepareSheetPr(ws) ws.prepareSheetPr()
ws.SheetPr.EnableFormatConditionsCalculation = opts.EnableFormatConditionsCalculation ws.SheetPr.EnableFormatConditionsCalculation = opts.EnableFormatConditionsCalculation
} }
if opts.Published != nil { if opts.Published != nil {
prepareSheetPr(ws) ws.prepareSheetPr()
ws.SheetPr.Published = opts.Published ws.SheetPr.Published = opts.Published
} }
if opts.AutoPageBreaks != nil { if opts.AutoPageBreaks != nil {
@ -151,10 +165,7 @@ func (ws *xlsxWorksheet) setSheetProps(opts *SheetPropsOptions) {
preparePageSetUpPr(ws) preparePageSetUpPr(ws)
ws.SheetPr.PageSetUpPr.FitToPage = *opts.FitToPage ws.SheetPr.PageSetUpPr.FitToPage = *opts.FitToPage
} }
if opts.OutlineSummaryBelow != nil { ws.setSheetOutlineProps(opts)
prepareOutlinePr(ws)
ws.SheetPr.OutlinePr.SummaryBelow = *opts.OutlineSummaryBelow
}
if opts.TabColorIndexed != nil { if opts.TabColorIndexed != nil {
prepareTabColor(ws) prepareTabColor(ws)
ws.SheetPr.TabColor.Indexed = *opts.TabColorIndexed ws.SheetPr.TabColor.Indexed = *opts.TabColorIndexed
@ -237,7 +248,8 @@ func (f *File) GetSheetProps(sheet string) (SheetPropsOptions, error) {
opts.FitToPage = boolPtr(ws.SheetPr.PageSetUpPr.FitToPage) opts.FitToPage = boolPtr(ws.SheetPr.PageSetUpPr.FitToPage)
} }
if ws.SheetPr.OutlinePr != nil { if ws.SheetPr.OutlinePr != nil {
opts.OutlineSummaryBelow = boolPtr(ws.SheetPr.OutlinePr.SummaryBelow) opts.OutlineSummaryBelow = ws.SheetPr.OutlinePr.SummaryBelow
opts.OutlineSummaryRight = ws.SheetPr.OutlinePr.SummaryRight
} }
if ws.SheetPr.TabColor != nil { if ws.SheetPr.TabColor != nil {
opts.TabColorIndexed = intPtr(ws.SheetPr.TabColor.Indexed) opts.TabColorIndexed = intPtr(ws.SheetPr.TabColor.Indexed)

View File

@ -61,25 +61,26 @@ func TestSetSheetProps(t *testing.T) {
assert.True(t, ok) assert.True(t, ok)
ws.(*xlsxWorksheet).SheetPr = nil ws.(*xlsxWorksheet).SheetPr = nil
ws.(*xlsxWorksheet).SheetFormatPr = nil ws.(*xlsxWorksheet).SheetFormatPr = nil
baseColWidth := uint8(8) baseColWidth, enable := uint8(8), boolPtr(true)
expected := SheetPropsOptions{ expected := SheetPropsOptions{
CodeName: stringPtr("code"), CodeName: stringPtr("code"),
EnableFormatConditionsCalculation: boolPtr(true), EnableFormatConditionsCalculation: enable,
Published: boolPtr(true), Published: enable,
AutoPageBreaks: boolPtr(true), AutoPageBreaks: enable,
FitToPage: boolPtr(true), FitToPage: enable,
TabColorIndexed: intPtr(1), TabColorIndexed: intPtr(1),
TabColorRGB: stringPtr("#FFFF00"), TabColorRGB: stringPtr("#FFFF00"),
TabColorTheme: intPtr(1), TabColorTheme: intPtr(1),
TabColorTint: float64Ptr(1), TabColorTint: float64Ptr(1),
OutlineSummaryBelow: boolPtr(true), OutlineSummaryBelow: enable,
OutlineSummaryRight: enable,
BaseColWidth: &baseColWidth, BaseColWidth: &baseColWidth,
DefaultColWidth: float64Ptr(10), DefaultColWidth: float64Ptr(10),
DefaultRowHeight: float64Ptr(10), DefaultRowHeight: float64Ptr(10),
CustomHeight: boolPtr(true), CustomHeight: enable,
ZeroHeight: boolPtr(true), ZeroHeight: enable,
ThickTop: boolPtr(true), ThickTop: enable,
ThickBottom: boolPtr(true), ThickBottom: enable,
} }
assert.NoError(t, f.SetSheetProps("Sheet1", &expected)) assert.NoError(t, f.SetSheetProps("Sheet1", &expected))
opts, err := f.GetSheetProps("Sheet1") opts, err := f.GetSheetProps("Sheet1")
@ -87,7 +88,7 @@ func TestSetSheetProps(t *testing.T) {
assert.Equal(t, expected, opts) assert.Equal(t, expected, opts)
ws.(*xlsxWorksheet).SheetPr = nil ws.(*xlsxWorksheet).SheetPr = nil
assert.NoError(t, f.SetSheetProps("Sheet1", &SheetPropsOptions{FitToPage: boolPtr(true)})) assert.NoError(t, f.SetSheetProps("Sheet1", &SheetPropsOptions{FitToPage: enable}))
ws.(*xlsxWorksheet).SheetPr = nil ws.(*xlsxWorksheet).SheetPr = nil
assert.NoError(t, f.SetSheetProps("Sheet1", &SheetPropsOptions{TabColorRGB: stringPtr("#FFFF00")})) assert.NoError(t, f.SetSheetProps("Sheet1", &SheetPropsOptions{TabColorRGB: stringPtr("#FFFF00")}))
ws.(*xlsxWorksheet).SheetPr = nil ws.(*xlsxWorksheet).SheetPr = nil

View File

@ -365,7 +365,7 @@ func (f *File) addSparklineGroupByStyle(ID int) *xlsxX14SparklineGroup {
// Excel 2007, but they won't be displayed. For example, add a grouped // Excel 2007, but they won't be displayed. For example, add a grouped
// sparkline. Changes are applied to all three: // sparkline. Changes are applied to all three:
// //
// err := f.AddSparkline("Sheet1", &excelize.SparklineOption{ // err := f.AddSparkline("Sheet1", &excelize.SparklineOptions{
// Location: []string{"A1", "A2", "A3"}, // Location: []string{"A1", "A2", "A3"},
// Range: []string{"Sheet2!A1:J1", "Sheet2!A2:J2", "Sheet2!A3:J3"}, // Range: []string{"Sheet2!A1:J1", "Sheet2!A2:J2", "Sheet2!A3:J3"},
// Markers: true, // Markers: true,

View File

@ -40,12 +40,12 @@ type StreamWriter struct {
// NewStreamWriter return stream writer struct by given worksheet name for // NewStreamWriter return stream writer struct by given worksheet name for
// generate new worksheet with large amounts of data. Note that after set // generate new worksheet with large amounts of data. Note that after set
// rows, you must call the 'Flush' method to end the streaming writing // rows, you must call the 'Flush' method to end the streaming writing process
// process and ensure that the order of line numbers is ascending, the common // and ensure that the order of line numbers is ascending, the normal mode
// API and stream API can't be work mixed to writing data on the worksheets, // functions and stream mode functions can't be work mixed to writing data on
// you can't get cell value when in-memory chunks data over 16MB. For // the worksheets, you can't get cell value when in-memory chunks data over
// example, set data for worksheet of size 102400 rows x 50 columns with // 16MB. For example, set data for worksheet of size 102400 rows x 50 columns
// numbers and style: // with numbers and style:
// //
// file := excelize.NewFile() // file := excelize.NewFile()
// streamWriter, err := file.NewStreamWriter("Sheet1") // streamWriter, err := file.NewStreamWriter("Sheet1")

View File

@ -250,9 +250,9 @@ type xlsxSheetPr struct {
// adjust the direction of grouper controls. // adjust the direction of grouper controls.
type xlsxOutlinePr struct { type xlsxOutlinePr struct {
ApplyStyles *bool `xml:"applyStyles,attr"` ApplyStyles *bool `xml:"applyStyles,attr"`
SummaryBelow bool `xml:"summaryBelow,attr"` SummaryBelow *bool `xml:"summaryBelow,attr"`
SummaryRight bool `xml:"summaryRight,attr"` SummaryRight *bool `xml:"summaryRight,attr"`
ShowOutlineSymbols bool `xml:"showOutlineSymbols,attr"` ShowOutlineSymbols *bool `xml:"showOutlineSymbols,attr"`
} }
// xlsxPageSetUpPr expresses page setup properties of the worksheet. // xlsxPageSetUpPr expresses page setup properties of the worksheet.
@ -989,6 +989,9 @@ type SheetPropsOptions struct {
// OutlineSummaryBelow indicating whether summary rows appear below detail // OutlineSummaryBelow indicating whether summary rows appear below detail
// in an outline, when applying an outline. // in an outline, when applying an outline.
OutlineSummaryBelow *bool `json:"outline_summary_below,omitempty"` OutlineSummaryBelow *bool `json:"outline_summary_below,omitempty"`
// OutlineSummaryRight indicating whether summary columns appear to the
// right of detail in an outline, when applying an outline.
OutlineSummaryRight *bool `json:"outline_summary_right,omitempty"`
// BaseColWidth specifies the number of characters of the maximum digit // BaseColWidth specifies the number of characters of the maximum digit
// width of the normal style's font. This value does not include margin // width of the normal style's font. This value does not include margin
// padding or extra padding for grid lines. It is only the number of // padding or extra padding for grid lines. It is only the number of