Skip to content

Commit

Permalink
Breaking change for data validation and fixed #1117
Browse files Browse the repository at this point in the history
- Remove second useless parameter `isCurrentSheet` of the function `SetSqrefDropList`
- Fix missing page setup of worksheet after re-saving the spreadsheet
  • Loading branch information
xuri committed Jan 11, 2022
1 parent 891e5ba commit b96329c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
12 changes: 4 additions & 8 deletions datavalidation.go
Expand Up @@ -168,16 +168,12 @@ func (dd *DataValidation) SetRange(f1, f2 interface{}, t DataValidationType, o D
//
// dvRange := excelize.NewDataValidation(true)
// dvRange.Sqref = "A7:B8"
// dvRange.SetSqrefDropList("$E$1:$E$3", true)
// dvRange.SetSqrefDropList("$E$1:$E$3")
// f.AddDataValidation("Sheet1", dvRange)
//
func (dd *DataValidation) SetSqrefDropList(sqref string, isCurrentSheet bool) error {
if isCurrentSheet {
dd.Formula1 = fmt.Sprintf("<formula1>%s</formula1>", sqref)
dd.Type = convDataValidationType(typeList)
return nil
}
return fmt.Errorf("cross-sheet sqref cell are not supported")
func (dd *DataValidation) SetSqrefDropList(sqref string) {
dd.Formula1 = fmt.Sprintf("<formula1>%s</formula1>", sqref)
dd.Type = convDataValidationType(typeList)
}

// SetSqref provides function to set data validation range in drop list.
Expand Down
7 changes: 2 additions & 5 deletions datavalidation_test.go
Expand Up @@ -81,15 +81,12 @@ func TestDataValidationError(t *testing.T) {
dvRange := NewDataValidation(true)
dvRange.SetSqref("A7:B8")
dvRange.SetSqref("A7:B8")
assert.NoError(t, dvRange.SetSqrefDropList("$E$1:$E$3", true))

err := dvRange.SetSqrefDropList("$E$1:$E$3", false)
assert.EqualError(t, err, "cross-sheet sqref cell are not supported")
dvRange.SetSqrefDropList("$E$1:$E$3")

assert.NoError(t, f.AddDataValidation("Sheet1", dvRange))

dvRange = NewDataValidation(true)
err = dvRange.SetDropList(make([]string, 258))
err := dvRange.SetDropList(make([]string, 258))
if dvRange.Formula1 != "" {
t.Errorf("data validation error. Formula1 must be empty!")
return
Expand Down
18 changes: 9 additions & 9 deletions sheet.go
Expand Up @@ -1335,49 +1335,49 @@ func (o *PageLayoutOrientation) getPageLayout(ps *xlsxPageSetUp) {

// setPageLayout provides a method to set the paper size for the worksheet.
func (p PageLayoutPaperSize) setPageLayout(ps *xlsxPageSetUp) {
ps.PaperSize = int(p)
ps.PaperSize = intPtr(int(p))
}

// getPageLayout provides a method to get the paper size for the worksheet.
func (p *PageLayoutPaperSize) getPageLayout(ps *xlsxPageSetUp) {
// Excel default: 1
if ps == nil || ps.PaperSize == 0 {
if ps == nil || ps.PaperSize == nil {
*p = 1
return
}
*p = PageLayoutPaperSize(ps.PaperSize)
*p = PageLayoutPaperSize(*ps.PaperSize)
}

// setPageLayout provides a method to set the fit to height for the worksheet.
func (p FitToHeight) setPageLayout(ps *xlsxPageSetUp) {
if int(p) > 0 {
ps.FitToHeight = int(p)
ps.FitToHeight = intPtr(int(p))
}
}

// getPageLayout provides a method to get the fit to height for the worksheet.
func (p *FitToHeight) getPageLayout(ps *xlsxPageSetUp) {
if ps == nil || ps.FitToHeight == 0 {
if ps == nil || ps.FitToHeight == nil {
*p = 1
return
}
*p = FitToHeight(ps.FitToHeight)
*p = FitToHeight(*ps.FitToHeight)
}

// setPageLayout provides a method to set the fit to width for the worksheet.
func (p FitToWidth) setPageLayout(ps *xlsxPageSetUp) {
if int(p) > 0 {
ps.FitToWidth = int(p)
ps.FitToWidth = intPtr(int(p))
}
}

// getPageLayout provides a method to get the fit to width for the worksheet.
func (p *FitToWidth) getPageLayout(ps *xlsxPageSetUp) {
if ps == nil || ps.FitToWidth == 0 {
if ps == nil || ps.FitToWidth == nil {
*p = 1
return
}
*p = FitToWidth(ps.FitToWidth)
*p = FitToWidth(*ps.FitToWidth)
}

// setPageLayout provides a method to set the scale for the worksheet.
Expand Down
8 changes: 4 additions & 4 deletions xmlWorksheet.go
Expand Up @@ -112,16 +112,16 @@ type xlsxPageSetUp struct {
Draft bool `xml:"draft,attr,omitempty"`
Errors string `xml:"errors,attr,omitempty"`
FirstPageNumber string `xml:"firstPageNumber,attr,omitempty"`
FitToHeight int `xml:"fitToHeight,attr,omitempty"`
FitToWidth int `xml:"fitToWidth,attr,omitempty"`
FitToHeight *int `xml:"fitToHeight,attr"`
FitToWidth *int `xml:"fitToWidth,attr,omitempty"`
HorizontalDPI int `xml:"horizontalDpi,attr,omitempty"`
RID string `xml:"http://schemas.openxmlformats.org/officeDocument/2006/relationships id,attr,omitempty"`
Orientation string `xml:"orientation,attr,omitempty"`
PageOrder string `xml:"pageOrder,attr,omitempty"`
PaperHeight string `xml:"paperHeight,attr,omitempty"`
PaperSize int `xml:"paperSize,attr,omitempty"`
PaperSize *int `xml:"paperSize,attr,omitempty"`
PaperWidth string `xml:"paperWidth,attr,omitempty"`
Scale int `xml:"scale,attr,omitempty"`
Scale int `xml:"scale,attr"`
UseFirstPageNumber bool `xml:"useFirstPageNumber,attr,omitempty"`
UsePrinterDefaults bool `xml:"usePrinterDefaults,attr,omitempty"`
VerticalDPI int `xml:"verticalDpi,attr,omitempty"`
Expand Down

0 comments on commit b96329c

Please sign in to comment.