github.com/richardwilkes/toolbox@v1.121.0/formats/xlsx/cell.go (about)

     1  // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, version 2.0. If a copy of the MPL was not distributed with
     5  // this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  //
     7  // This Source Code Form is "Incompatible With Secondary Licenses", as
     8  // defined by the Mozilla Public License, version 2.0.
     9  
    10  package xlsx
    11  
    12  import (
    13  	"strconv"
    14  	"time"
    15  )
    16  
    17  // Cell types.
    18  const (
    19  	String CellType = iota
    20  	Number
    21  	Boolean
    22  )
    23  
    24  // CellType holds an enumeration of cell types.
    25  type CellType int
    26  
    27  // Cell holds the contents of a cell.
    28  type Cell struct {
    29  	Value string
    30  	Type  CellType
    31  }
    32  
    33  func (c *Cell) String() string {
    34  	return c.Value
    35  }
    36  
    37  // Integer returns the value of this cell as an integer.
    38  func (c *Cell) Integer() int {
    39  	v, err := strconv.Atoi(c.Value)
    40  	if err != nil {
    41  		v = int(c.Float())
    42  	}
    43  	return v
    44  }
    45  
    46  // Float returns the value of this cell as an float.
    47  func (c *Cell) Float() float64 {
    48  	f, err := strconv.ParseFloat(c.Value, 64)
    49  	if err != nil {
    50  		return 0
    51  	}
    52  	return f
    53  }
    54  
    55  // Boolean returns the value of this cell as a boolean.
    56  func (c *Cell) Boolean() bool {
    57  	return c.Value != "0"
    58  }
    59  
    60  // Time returns the value of this cell as a time.Time.
    61  func (c *Cell) Time() time.Time {
    62  	return timeFromExcelTime(c.Float())
    63  }