Browse Source

Remove off by one error

Ryan Hollis 6 years ago
parent
commit
afaa7a788b
5 changed files with 27 additions and 27 deletions
  1. 7 17
      col.go
  2. 10 1
      data_validation_test.go
  3. 1 1
      lib.go
  4. 4 4
      sheet.go
  5. 5 4
      xmlWorksheet.go

+ 7 - 17
col.go

@@ -4,7 +4,6 @@ package xlsx
 const ColWidth = 9.5
 const Excel2006MaxRowCount = 1048576
 const Excel2006MaxRowIndex = Excel2006MaxRowCount - 1
-const Excel2006MinRowIndex = 1
 
 type Col struct {
 	Min            int
@@ -52,19 +51,11 @@ func (c *Col) SetStyle(style *Style) {
 	c.style = style
 }
 
-// SetDataValidation set data validation with start,end ; start or end  = 0  equal all column
+// SetDataValidation set data validation with zero based start and end.
+// Set end to -1 for all rows.
 func (c *Col) SetDataValidation(dd *xlsxCellDataValidation, start, end int) {
-
-	if 0 == start {
-		start = Excel2006MinRowIndex
-	} else {
-		start = start + 1
-	}
-
-	if 0 == end {
-		end = Excel2006MinRowIndex
-	} else if end < Excel2006MaxRowCount {
-		end = end + 1
+	if end < 0 {
+		end = Excel2006MaxRowIndex
 	}
 
 	dd.minRow = start
@@ -99,11 +90,10 @@ func (c *Col) SetDataValidation(dd *xlsxCellDataValidation, start, end int) {
 	}
 	tmpDD = append(tmpDD, dd)
 	c.DataValidation = tmpDD
-
 }
 
-// SetDataValidationWithStart set data validation with start
+// SetDataValidationWithStart set data validation with a zero basd start row.
+// This will apply to the rest of the rest of the column.
 func (c *Col) SetDataValidationWithStart(dd *xlsxCellDataValidation, start int) {
-	//2006 excel all row 1048576
-	c.SetDataValidation(dd, start, Excel2006MaxRowIndex)
+	c.SetDataValidation(dd, start, -1)
 }

+ 10 - 1
data_validation_test.go

@@ -220,7 +220,16 @@ func (d *DataValidationSuite) TestDataValidation(t *C) {
 	t.Assert(err, IsNil)
 	title = "col d range"
 	dd.SetInput(&title, &msg)
-	sheet.Col(3).SetDataValidation(dd, 3, Excel2006MaxRowCount)
+	sheet.Col(3).SetDataValidation(dd, 3, Excel2006MaxRowIndex)
+
+	dd = NewXlsxCellDataValidation(true)
+	err = dd.SetDropList([]string{"d", "d1", "d2"})
+	t.Assert(err, IsNil)
+	title = "col d range"
+	dd.SetInput(&title, &msg)
+	sheet.Col(3).SetDataValidation(dd, 4, -1)
+	maxRow := sheet.Col(3).DataValidation[len(sheet.Col(3).DataValidation)-1].maxRow
+	t.Assert(maxRow, Equals, Excel2006MaxRowIndex)
 
 	dest := &bytes.Buffer{}
 	err = file.Write(dest)

+ 1 - 1
lib.go

@@ -722,7 +722,7 @@ func readSheetFromFile(sc chan *indexedSheet, index int, rsheet xlsxSheet, fi *F
 	sheet.SheetFormat.OutlineLevelCol = worksheet.SheetFormatPr.OutlineLevelCol
 	sheet.SheetFormat.OutlineLevelRow = worksheet.SheetFormatPr.OutlineLevelRow
 	if nil != worksheet.DataValidations {
-		for _, dd := range worksheet.DataValidations.DataValidattion {
+		for _, dd := range worksheet.DataValidations.DataValidation {
 			sqrefArr := strings.Split(dd.Sqref, " ")
 			for _, sqref := range sqrefArr {
 				parts := strings.Split(sqref, cellRangeChar)

+ 4 - 4
sheet.go

@@ -289,10 +289,10 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 				} else {
 					dd.Sqref = colName + RowIndexToString(dd.minRow) + cellRangeChar + colName + RowIndexToString(dd.maxRow)
 				}
-				worksheet.DataValidations.DataValidattion = append(worksheet.DataValidations.DataValidattion, dd)
+				worksheet.DataValidations.DataValidation = append(worksheet.DataValidations.DataValidation, dd)
 
 			}
-			worksheet.DataValidations.Count = len(worksheet.DataValidations.DataValidattion)
+			worksheet.DataValidations.Count = len(worksheet.DataValidations.DataValidation)
 		}
 	}
 
@@ -368,8 +368,8 @@ func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxW
 					worksheet.DataValidations = &xlsxCellDataValidations{}
 				}
 				cell.DataValidation.Sqref = xC.R
-				worksheet.DataValidations.DataValidattion = append(worksheet.DataValidations.DataValidattion, cell.DataValidation)
-				worksheet.DataValidations.Count = len(worksheet.DataValidations.DataValidattion)
+				worksheet.DataValidations.DataValidation = append(worksheet.DataValidations.DataValidation, cell.DataValidation)
+				worksheet.DataValidations.Count = len(worksheet.DataValidations.DataValidation)
 			}
 
 			if cell.HMerge > 0 || cell.VMerge > 0 {

+ 5 - 4
xmlWorksheet.go

@@ -226,8 +226,8 @@ type xlsxSheetData struct {
 
 // xlsxCellDataValidations  excel cell data validation
 type xlsxCellDataValidations struct {
-	DataValidattion []*xlsxCellDataValidation `xml:"dataValidation"`
-	Count           int                       `xml:"count,attr"`
+	DataValidation []*xlsxCellDataValidation `xml:"dataValidation"`
+	Count          int                       `xml:"count,attr"`
 }
 
 // xlsxCellDataValidation
@@ -272,8 +272,9 @@ type xlsxCellDataValidation struct {
 	// The second formula in the DataValidation dropdown. It is used as a bounds for 'between' and
 	// 'notBetween' relational operators only.
 	Formula2 string `xml:"formula2,omitempty"`
-	minRow   int    //`xml:"-"`
-	maxRow   int    //`xml:"-"`
+	// minRow and maxRow are zero indexed
+	minRow int //`xml:"-"`
+	maxRow int //`xml:"-"`
 	//minCol         int     `xml:"-"` //spare
 	//maxCol         int     `xml:"-"` //spare
 }