github.com/shakinm/xlsReader@v0.9.12/xls/xls_test.go (about)

     1  package xls
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestGetWorkBook(t *testing.T) {
     9  
    10  	wb, err := OpenFile("./../testfie/small_1_sheet.xls")
    11  
    12  	if err != nil {
    13  		t.Error("Error: ", err)
    14  	}
    15  
    16  	s, err := wb.GetSheet(0)
    17  	cells, _ := s.GetRow(2)
    18  
    19  	for k := range cells.cols {
    20  		c, _ := cells.GetCol(k)
    21  
    22  		formatIndex := wb.GetXFbyIndex(c.GetXFIndex())
    23  		format := wb.GetFormatByIndex(formatIndex.GetFormatIndex())
    24  
    25  		if err == nil {
    26  			switch k {
    27  			case 0:
    28  				if c.GetInt64() != 3 {
    29  					t.Error("Expected 3, got ", c.GetInt64())
    30  				}
    31  			case 1:
    32  				if c.GetInt64() != 4 {
    33  					t.Error("Expected 4, got ", c.GetInt64())
    34  				}
    35  			case 2:
    36  				if c.GetFloat64() != 2.1 {
    37  					t.Error("Expected 2.1, got ", c.GetFloat64())
    38  				}
    39  			case 3:
    40  				if c.GetString() != "String 3" {
    41  					t.Error("Expected 'String 3', got ", c.GetString())
    42  				}
    43  			case 4:
    44  				if c.GetString() != "https://github.com/shakinm/xlsReader" {
    45  					t.Error("Expected 'https://github.com/shakinm/xlsReader', got ", c.GetString())
    46  				}
    47  			case 5:
    48  				if c.GetString() != "" {
    49  					t.Error("Expected '', got ", c.GetString())
    50  				}
    51  			case 6:
    52  				if c.GetString() != "" {
    53  					t.Error("Expected '', got ", c.GetString())
    54  				}
    55  			case 7:
    56  				if c.GetString() != "String 3" {
    57  					t.Error("Expected 'String 3', got ", c.GetString())
    58  				}
    59  			case 8:
    60  				if c.GetInt64() != 3 {
    61  					t.Error("Expected 3, got ", c.GetInt64())
    62  				}
    63  			case 9: // bool
    64  				if c.GetInt64() != 1 {
    65  					t.Error("Expected 1, got ", c.GetInt64())
    66  				}
    67  				if c.GetString() != "TRUE" {
    68  					t.Error("Expected 'TRUE', got ", c.GetString())
    69  				}
    70  			case 10: //date
    71  				if format.GetFormatString(c) != "9/3/19" {
    72  					t.Error("Expected '9/3/19', got ", format.GetFormatString(c))
    73  				}
    74  			case 11: //dateTime
    75  				if format.GetFormatString(c) != "09/03/2019 13:12:59" {
    76  					t.Error("Expected '09/03/2019 13:12:59', got ", format.GetFormatString(c))
    77  				}
    78  			case 12:
    79  				if format.GetFormatString(c) != "55.00%" {
    80  					t.Error("Expected '55.00%', got ", format.GetFormatString(c))
    81  				}
    82  			case 13:
    83  				if format.GetFormatString(c) != "#DIV/0!" {
    84  					t.Error("Expected '#DIV/0!', got ", format.GetFormatString(c))
    85  				}
    86  			}
    87  
    88  		}
    89  	}
    90  }
    91  
    92  func TestMiniFatWorkBook(t *testing.T) {
    93  	wb, err := OpenFile("./../testfie/table.xls")
    94  
    95  	if err != nil {
    96  		t.Error("Error: ", err)
    97  	}
    98  
    99  	for i := 0; i <= wb.GetNumberSheets()-1; i++ {
   100  
   101  		sheet, _ := wb.GetSheet(i)
   102  		if sheet.GetRows() != nil {
   103  			for _, row := range sheet.GetRows() {
   104  				if row  != nil {
   105  
   106  					for _, col := range row.GetCols() {
   107  
   108  					//	fmt.Println(col.GetString())
   109  						xf := col.GetXFIndex()
   110  						//fmt.Println(xf)
   111  						style := wb.GetXFbyIndex(xf)
   112  						//fmt.Println(style)
   113  						formatIdx := style.GetFormatIndex()
   114  						//fmt.Println(formatIdx)
   115  						format := wb.GetFormatByIndex(formatIdx)
   116  						//fmt.Println(format)
   117  
   118  						fstr := format.GetFormatString(col)
   119  						fmt.Println(fstr)
   120  
   121  					}
   122  				}
   123  
   124  			}
   125  		}
   126  
   127  	}
   128  }