Skip to content

Commit

Permalink
Standardize input and output
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Fyne committed Feb 16, 2017
1 parent 0833a9d commit d9f4a2f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
34 changes: 24 additions & 10 deletions excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"archive/zip"
"bytes"
"encoding/xml"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
)
Expand All @@ -19,20 +22,31 @@ type File struct {
// OpenFile take the name of an XLSX file and returns a populated XLSX file
// struct for it.
func OpenFile(filename string) (*File, error) {
var f *zip.ReadCloser
var err error
file := make(map[string]string)
c := make(map[string]bool)
sheetCount := 0
f, err = zip.OpenReader(filename)
file, err := os.Open(filename)
if err != nil {
return &File{}, err
return nil, err
}
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return OpenReader(bytes.NewReader(b), int64(len(b)))
}

// OpenReader take an io.Reader and return a populated XLSX file.
func OpenReader(r io.ReaderAt, size int64) (*File, error) {
zr, err := zip.NewReader(r, size)
if err != nil {
return nil, err
}
file, sheetCount, err := ReadZipReader(zr)
if err != nil {
return nil, err
}
file, sheetCount, _ = ReadZip(f)
return &File{
checked: c,
checked: make(map[string]bool),
XLSX: file,
Path: filename,
Path: "",
SheetCount: sheetCount,
}, nil
}
Expand Down
52 changes: 23 additions & 29 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package excelize
import (
"archive/zip"
"bytes"
"fmt"
"io"
"os"
)

Expand All @@ -29,53 +31,45 @@ func CreateFile() *File {

// Save provides function to override the xlsx file with origin path.
func (f *File) Save() error {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
for path, content := range f.XLSX {
f, err := w.Create(path)
if err != nil {
return err
}
_, err = f.Write([]byte(content))
if err != nil {
return err
}
}
err := w.Close()
if err != nil {
return err
}
file, err := os.OpenFile(f.Path, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
if f.Path == "" {
return fmt.Errorf("No path defined for file, consider File.WriteTo or File.Write")
}
buf.WriteTo(file)
return err
return f.WriteTo(f.Path)
}

// WriteTo provides function to create or update to an xlsx file at the provided
// path.
func (f *File) WriteTo(name string) error {
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
return err
}
defer file.Close()
return f.Write(file)
}

// Write provides function to write to an io.Writer.
func (f *File) Write(w io.Writer) error {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
zw := zip.NewWriter(buf)
for path, content := range f.XLSX {
f, err := w.Create(path)
fi, err := zw.Create(path)
if err != nil {
return err
}
_, err = f.Write([]byte(content))
_, err = fi.Write([]byte(content))
if err != nil {
return err
}
}
err := w.Close()
err := zw.Close()
if err != nil {
return err
}
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {

if _, err := buf.WriteTo(w); err != nil {
return err
}
buf.WriteTo(file)
return err

return nil
}
8 changes: 0 additions & 8 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"math"
)

// ReadZip takes a pointer to a zip.ReadCloser and returns a xlsx.File struct
// populated with its contents. In most cases ReadZip is not used directly, but
// is called internally by OpenFile.
func ReadZip(f *zip.ReadCloser) (map[string]string, int, error) {
defer f.Close()
return ReadZipReader(&f.Reader)
}

// ReadZipReader can be used to read an XLSX in memory without touching the
// filesystem.
func ReadZipReader(r *zip.Reader) (map[string]string, int, error) {
Expand Down

0 comments on commit d9f4a2f

Please sign in to comment.