已验证 提交 9f862304 编写于 作者: xurime's avatar xurime

Optimize code, fix golint issues

上级 e77c462d
...@@ -104,13 +104,13 @@ func TestAdjustCalcChain(t *testing.T) { ...@@ -104,13 +104,13 @@ func TestAdjustCalcChain(t *testing.T) {
func TestCoordinatesToAreaRef(t *testing.T) { func TestCoordinatesToAreaRef(t *testing.T) {
f := NewFile() f := NewFile()
ref, err := f.coordinatesToAreaRef([]int{}) _, err := f.coordinatesToAreaRef([]int{})
assert.EqualError(t, err, "coordinates length must be 4") assert.EqualError(t, err, "coordinates length must be 4")
ref, err = f.coordinatesToAreaRef([]int{1, -1, 1, 1}) _, err = f.coordinatesToAreaRef([]int{1, -1, 1, 1})
assert.EqualError(t, err, "invalid cell coordinates [1, -1]") assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
ref, err = f.coordinatesToAreaRef([]int{1, 1, 1, -1}) _, err = f.coordinatesToAreaRef([]int{1, 1, 1, -1})
assert.EqualError(t, err, "invalid cell coordinates [1, -1]") assert.EqualError(t, err, "invalid cell coordinates [1, -1]")
ref, err = f.coordinatesToAreaRef([]int{1, 1, 1, 1}) ref, err := f.coordinatesToAreaRef([]int{1, 1, 1, 1})
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, ref, "A1:A1") assert.EqualValues(t, ref, "A1:A1")
} }
...@@ -499,26 +499,33 @@ func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string { ...@@ -499,26 +499,33 @@ func (f *File) getSheetRelationshipsTargetByID(sheet, rID string) string {
func (f *File) GetPicture(sheet, cell string) (string, []byte, error) { func (f *File) GetPicture(sheet, cell string) (string, []byte, error) {
col, row, err := CellNameToCoordinates(cell) col, row, err := CellNameToCoordinates(cell)
if err != nil { if err != nil {
return "", []byte{}, err return "", nil, err
} }
col-- col--
row-- row--
xlsx, err := f.workSheetReader(sheet) xlsx, err := f.workSheetReader(sheet)
if err != nil { if err != nil {
return "", []byte{}, err return "", nil, err
} }
if xlsx.Drawing == nil { if xlsx.Drawing == nil {
return "", []byte{}, err return "", nil, err
} }
target := f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID) target := f.getSheetRelationshipsTargetByID(sheet, xlsx.Drawing.RID)
drawingXML := strings.Replace(target, "..", "xl", -1) drawingXML := strings.Replace(target, "..", "xl", -1)
_, ok := f.XLSX[drawingXML]
if !ok {
return "", nil, err
}
drawingRelationships := strings.Replace( drawingRelationships := strings.Replace(
strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1) strings.Replace(target, "../drawings", "xl/drawings/_rels", -1), ".xml", ".xml.rels", -1)
wsDr, _ := f.drawingParser(drawingXML) return f.getPicture(row, col, drawingXML, drawingRelationships)
}
// getPicture provides a function to get picture base name and raw content
// embed in XLSX by given coordinates and drawing relationships.
func (f *File) getPicture(row, col int, drawingXML, drawingRelationships string) (string, []byte, error) {
wsDr, _ := f.drawingParser(drawingXML)
for _, anchor := range wsDr.TwoCellAnchor { for _, anchor := range wsDr.TwoCellAnchor {
if anchor.From != nil && anchor.Pic != nil { if anchor.From != nil && anchor.Pic != nil {
if anchor.From.Col == col && anchor.From.Row == row { if anchor.From.Col == col && anchor.From.Row == row {
...@@ -528,16 +535,12 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) { ...@@ -528,16 +535,12 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) {
if ok { if ok {
return filepath.Base(xlsxWorkbookRelation.Target), return filepath.Base(xlsxWorkbookRelation.Target),
[]byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target,
"..", "xl", -1)]), err "..", "xl", -1)]), nil
} }
} }
} }
} }
_, ok := f.XLSX[drawingXML]
if !ok {
return "", nil, err
}
decodeWsDr := decodeWsDr{} decodeWsDr := decodeWsDr{}
_ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr) _ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(drawingXML)), &decodeWsDr)
for _, anchor := range decodeWsDr.TwoCellAnchor { for _, anchor := range decodeWsDr.TwoCellAnchor {
...@@ -548,12 +551,12 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) { ...@@ -548,12 +551,12 @@ func (f *File) GetPicture(sheet, cell string) (string, []byte, error) {
xlsxWorkbookRelation := f.getDrawingRelationships(drawingRelationships, decodeTwoCellAnchor.Pic.BlipFill.Blip.Embed) xlsxWorkbookRelation := f.getDrawingRelationships(drawingRelationships, decodeTwoCellAnchor.Pic.BlipFill.Blip.Embed)
_, ok := supportImageTypes[filepath.Ext(xlsxWorkbookRelation.Target)] _, ok := supportImageTypes[filepath.Ext(xlsxWorkbookRelation.Target)]
if ok { if ok {
return filepath.Base(xlsxWorkbookRelation.Target), []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, "..", "xl", -1)]), err return filepath.Base(xlsxWorkbookRelation.Target), []byte(f.XLSX[strings.Replace(xlsxWorkbookRelation.Target, "..", "xl", -1)]), nil
} }
} }
} }
} }
return "", []byte{}, err return "", nil, nil
} }
// getDrawingRelationships provides a function to get drawing relationships // getDrawingRelationships provides a function to get drawing relationships
......
...@@ -21,7 +21,7 @@ import ( ...@@ -21,7 +21,7 @@ import (
// GetRows return all the rows in a sheet by given worksheet name (case // GetRows return all the rows in a sheet by given worksheet name (case
// sensitive). For example: // sensitive). For example:
// //
// rows, err := f.GetRows("Sheet1") // rows, err := f.GetRows("Sheet1")
// for _, row := range rows { // for _, row := range rows {
// for _, colCell := range row { // for _, colCell := range row {
// fmt.Print(colCell, "\t") // fmt.Print(colCell, "\t")
...@@ -160,7 +160,7 @@ func (err ErrSheetNotExist) Error() string { ...@@ -160,7 +160,7 @@ func (err ErrSheetNotExist) Error() string {
// //
// rows, err := f.Rows("Sheet1") // rows, err := f.Rows("Sheet1")
// for rows.Next() { // for rows.Next() {
// row, err := rows.Columns() // row, err := rows.Columns()
// for _, colCell := range row { // for _, colCell := range row {
// fmt.Print(colCell, "\t") // fmt.Print(colCell, "\t")
// } // }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册