Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add error return value

exported functions:

GetMergeCells
ProtectSheet
UnprotectSheet
UpdateLinkedValue
GetMergeCells
SetSheetVisible

inner functions:

workSheetReader
copySheet
  • Loading branch information
xuri committed Apr 15, 2019
1 parent c423617 commit f2df344
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 146 deletions.
10 changes: 6 additions & 4 deletions adjust.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,20 @@ const (
// adjustDataValidations, adjustProtectedCells
//
func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int) error {
xlsx := f.workSheetReader(sheet)

xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
if dir == rows {
f.adjustRowDimensions(xlsx, num, offset)
} else {
f.adjustColDimensions(xlsx, num, offset)
}
f.adjustHyperlinks(xlsx, sheet, dir, num, offset)
if err := f.adjustMergeCells(xlsx, dir, num, offset); err != nil {
if err = f.adjustMergeCells(xlsx, dir, num, offset); err != nil {
return err
}
if err := f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
if err = f.adjustAutoFilter(xlsx, dir, num, offset); err != nil {
return err
}

Expand Down
5 changes: 3 additions & 2 deletions adjust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestAdjustAutoFilter(t *testing.T) {

func TestAdjustHelper(t *testing.T) {
f := NewFile()
f.NewSheet("Sheet2")
f.Sheet["xl/worksheets/sheet1.xml"] = &xlsxWorksheet{
MergeCells: &xlsxMergeCells{
Cells: []*xlsxMergeCell{
Expand All @@ -61,6 +62,6 @@ func TestAdjustHelper(t *testing.T) {
},
}
// testing adjustHelper with illegal cell coordinates.
assert.EqualError(t, f.adjustHelper("sheet1", rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.adjustHelper("sheet2", rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
assert.EqualError(t, f.adjustHelper("Sheet1", rows, 0, 0), `cannot convert cell "A" to coordinates: invalid cell name "A"`)
assert.EqualError(t, f.adjustHelper("Sheet2", rows, 0, 0), `cannot convert cell "B" to coordinates: invalid cell name "B"`)
}
102 changes: 66 additions & 36 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,63 +69,67 @@ func (f *File) GetCellValue(sheet, axis string) (string, error) {
// Note that default date format is m/d/yy h:mm of time.Time type value. You can
// set numbers format by SetCellStyle() method.
func (f *File) SetCellValue(sheet, axis string, value interface{}) error {
var err error
switch v := value.(type) {
case int:
f.SetCellInt(sheet, axis, v)
err = f.SetCellInt(sheet, axis, v)
case int8:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case int16:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case int32:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case int64:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case uint:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case uint8:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case uint16:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case uint32:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case uint64:
f.SetCellInt(sheet, axis, int(v))
err = f.SetCellInt(sheet, axis, int(v))
case float32:
f.SetCellFloat(sheet, axis, float64(v), -1, 32)
err = f.SetCellFloat(sheet, axis, float64(v), -1, 32)
case float64:
f.SetCellFloat(sheet, axis, v, -1, 64)
err = f.SetCellFloat(sheet, axis, v, -1, 64)
case string:
f.SetCellStr(sheet, axis, v)
err = f.SetCellStr(sheet, axis, v)
case []byte:
f.SetCellStr(sheet, axis, string(v))
err = f.SetCellStr(sheet, axis, string(v))
case time.Duration:
f.SetCellDefault(sheet, axis, strconv.FormatFloat(v.Seconds()/86400.0, 'f', -1, 32))
f.setDefaultTimeStyle(sheet, axis, 21)
err = f.SetCellDefault(sheet, axis, strconv.FormatFloat(v.Seconds()/86400.0, 'f', -1, 32))
err = f.setDefaultTimeStyle(sheet, axis, 21)
case time.Time:
excelTime, err := timeToExcelTime(v)
if err != nil {
return err
}
if excelTime > 0 {
f.SetCellDefault(sheet, axis, strconv.FormatFloat(excelTime, 'f', -1, 64))
f.setDefaultTimeStyle(sheet, axis, 22)
err = f.SetCellDefault(sheet, axis, strconv.FormatFloat(excelTime, 'f', -1, 64))
err = f.setDefaultTimeStyle(sheet, axis, 22)
} else {
f.SetCellStr(sheet, axis, v.Format(time.RFC3339Nano))
err = f.SetCellStr(sheet, axis, v.Format(time.RFC3339Nano))
}
case bool:
f.SetCellBool(sheet, axis, v)
err = f.SetCellBool(sheet, axis, v)
case nil:
f.SetCellStr(sheet, axis, "")
err = f.SetCellStr(sheet, axis, "")
default:
f.SetCellStr(sheet, axis, fmt.Sprintf("%v", value))
err = f.SetCellStr(sheet, axis, fmt.Sprintf("%v", value))
}
return nil
return err
}

// SetCellInt provides a function to set int type value of a cell by given
// worksheet name, cell coordinates and cell value.
func (f *File) SetCellInt(sheet, axis string, value int) error {
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
cellData, col, _, err := f.prepareCell(xlsx, sheet, axis)
if err != nil {
return err
Expand All @@ -139,7 +143,10 @@ func (f *File) SetCellInt(sheet, axis string, value int) error {
// SetCellBool provides a function to set bool type value of a cell by given
// worksheet name, cell name and cell value.
func (f *File) SetCellBool(sheet, axis string, value bool) error {
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
cellData, col, _, err := f.prepareCell(xlsx, sheet, axis)
if err != nil {
return err
Expand All @@ -164,7 +171,10 @@ func (f *File) SetCellBool(sheet, axis string, value bool) error {
// f.SetCellFloat("Sheet1", "A1", float64(x), 2, 32)
//
func (f *File) SetCellFloat(sheet, axis string, value float64, prec, bitSize int) error {
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
cellData, col, _, err := f.prepareCell(xlsx, sheet, axis)
if err != nil {
return err
Expand All @@ -178,7 +188,10 @@ func (f *File) SetCellFloat(sheet, axis string, value float64, prec, bitSize int
// SetCellStr provides a function to set string type value of a cell. Total
// number of characters that a cell can contain 32767 characters.
func (f *File) SetCellStr(sheet, axis, value string) error {
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
cellData, col, _, err := f.prepareCell(xlsx, sheet, axis)
if err != nil {
return err
Expand All @@ -203,7 +216,10 @@ func (f *File) SetCellStr(sheet, axis, value string) error {
// SetCellDefault provides a function to set string type value of a cell as
// default format without escaping the cell.
func (f *File) SetCellDefault(sheet, axis, value string) error {
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
cellData, col, _, err := f.prepareCell(xlsx, sheet, axis)
if err != nil {
return err
Expand Down Expand Up @@ -231,7 +247,10 @@ func (f *File) GetCellFormula(sheet, axis string) (string, error) {
// SetCellFormula provides a function to set cell formula by given string and
// worksheet name.
func (f *File) SetCellFormula(sheet, axis, formula string) error {
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
cellData, _, _, err := f.prepareCell(xlsx, sheet, axis)
if err != nil {
return err
Expand Down Expand Up @@ -264,8 +283,11 @@ func (f *File) GetCellHyperLink(sheet, axis string) (bool, string, error) {
return false, "", err
}

xlsx := f.workSheetReader(sheet)
axis, err := f.mergeCellsParser(xlsx, axis)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return false, "", err
}
axis, err = f.mergeCellsParser(xlsx, axis)
if err != nil {
return false, "", err
}
Expand Down Expand Up @@ -302,8 +324,11 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error {
return err
}

xlsx := f.workSheetReader(sheet)
axis, err := f.mergeCellsParser(xlsx, axis)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
axis, err = f.mergeCellsParser(xlsx, axis)
if err != nil {
return err
}
Expand Down Expand Up @@ -367,7 +392,10 @@ func (f *File) MergeCell(sheet, hcell, vcell string) error {
hcell, _ = CoordinatesToCellName(hcol, hrow)
vcell, _ = CoordinatesToCellName(vcol, vrow)

xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
if xlsx.MergeCells != nil {
ref := hcell + ":" + vcell
cells := make([]*xlsxMergeCell, 0, len(xlsx.MergeCells.Cells))
Expand Down Expand Up @@ -446,8 +474,10 @@ func (f *File) prepareCell(xlsx *xlsxWorksheet, sheet, cell string) (*xlsxC, int
// getCellStringFunc does common value extraction workflow for all GetCell*
// methods. Passed function implements specific part of required logic.
func (f *File) getCellStringFunc(sheet, axis string, fn func(x *xlsxWorksheet, c *xlsxC) (string, bool, error)) (string, error) {
xlsx := f.workSheetReader(sheet)
var err error
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return "", err
}
axis, err = f.mergeCellsParser(xlsx, axis)
if err != nil {
return "", err
Expand Down
5 changes: 4 additions & 1 deletion chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,10 @@ func (f *File) AddChart(sheet, cell, format string) error {
return err
}
// Read sheet data.
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
// Add first picture for given sheet, create xl/drawings/ and xl/drawings/_rels/ folder.
drawingID := f.countDrawings() + 1
chartID := f.countCharts() + 1
Expand Down
37 changes: 29 additions & 8 deletions col.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func (f *File) GetColVisible(sheet, col string) (bool, error) {
return visible, err
}

xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return false, err
}
if xlsx.Cols == nil {
return visible, err
}
Expand Down Expand Up @@ -61,7 +64,10 @@ func (f *File) SetColVisible(sheet, col string, visible bool) error {
Hidden: !visible,
CustomWidth: true,
}
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
if xlsx.Cols == nil {
cols := xlsxCols{}
cols.Col = append(cols.Col, colData)
Expand Down Expand Up @@ -93,7 +99,10 @@ func (f *File) GetColOutlineLevel(sheet, col string) (uint8, error) {
if err != nil {
return level, err
}
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return 0, err
}
if xlsx.Cols == nil {
return level, err
}
Expand Down Expand Up @@ -123,7 +132,10 @@ func (f *File) SetColOutlineLevel(sheet, col string, level uint8) error {
OutlineLevel: level,
CustomWidth: true,
}
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
if xlsx.Cols == nil {
cols := xlsxCols{}
cols.Col = append(cols.Col, colData)
Expand Down Expand Up @@ -162,7 +174,10 @@ func (f *File) SetColWidth(sheet, startcol, endcol string, width float64) error
min, max = max, min
}

xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
col := xlsxCol{
Min: min,
Max: max,
Expand Down Expand Up @@ -288,7 +303,7 @@ func (f *File) positionObjectPixels(sheet string, col, row, x1, y1, width, heigh
// getColWidth provides a function to get column width in pixels by given
// sheet name and column index.
func (f *File) getColWidth(sheet string, col int) int {
xlsx := f.workSheetReader(sheet)
xlsx, _ := f.workSheetReader(sheet)
if xlsx.Cols != nil {
var width float64
for _, v := range xlsx.Cols.Col {
Expand All @@ -311,7 +326,10 @@ func (f *File) GetColWidth(sheet, col string) (float64, error) {
if err != nil {
return defaultColWidthPixels, err
}
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return defaultColWidthPixels, err
}
if xlsx.Cols != nil {
var width float64
for _, v := range xlsx.Cols.Col {
Expand Down Expand Up @@ -355,7 +373,10 @@ func (f *File) RemoveCol(sheet, col string) error {
return err
}

xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
for rowIdx := range xlsx.SheetData.Row {
rowData := &xlsx.SheetData.Row[rowIdx]
for colIdx := range rowData.C {
Expand Down
5 changes: 4 additions & 1 deletion comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ func (f *File) AddComment(sheet, cell, format string) error {
return err
}
// Read sheet data.
xlsx := f.workSheetReader(sheet)
xlsx, err := f.workSheetReader(sheet)
if err != nil {
return err
}
commentID := f.countComments() + 1
drawingVML := "xl/drawings/vmlDrawing" + strconv.Itoa(commentID) + ".vml"
sheetRelationshipsComments := "../comments" + strconv.Itoa(commentID) + ".xml"
Expand Down
Loading

0 comments on commit f2df344

Please sign in to comment.