-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheet.go
More file actions
48 lines (42 loc) · 1007 Bytes
/
sheet.go
File metadata and controls
48 lines (42 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package sheet
import (
"errors"
"io"
"os"
"path/filepath"
)
// Info holds the heading row and row count parsed by the reader
type Info struct {
ColCount int
RowCount int
}
// Specification configures a SheetReader for the nuances of a particular source
type Specification struct {
SheetName string
SheetIndex int
HeaderRowIndex uint16
DataRowStartIndex uint16
}
func readerFor(ext string) (Reader, error) {
switch ext {
case ".xls":
return &xlsReader{}, nil
}
return nil, errors.New("No parser available for extension " + ext)
}
// ParseFile returns Info for a spreadsheet
func ParseFile(path string, s Specification) (i *Info, err error) {
var parser Reader
parser, err = readerFor(filepath.Ext(path))
var f *os.File
f, err = os.Open(path)
if err != nil {
return
}
defer f.Close()
return parser.Read(f, s)
}
// Reader is implemented by parsers that can read a spreadsheet
type Reader interface {
Read(r io.Reader, s Specification) (si *Info, err error)
}