1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-28 15:10:24 +08:00

fix #9: float point parse value

This commit is contained in:
源文雨
2023-07-10 14:36:09 +08:00
parent 1a682f16d9
commit 9f3162a90f
10 changed files with 162 additions and 199 deletions

View File

@@ -33,3 +33,12 @@ jobs:
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build. # Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true # skip-build-cache: true
- name: Get dependencies
run: go mod tidy
- name: Build
run: go build -v ./...
- name: Test
run: go test $(go list ./...)

View File

@@ -31,3 +31,12 @@ jobs:
if: ${{ !github.head_ref }} if: ${{ !github.head_ref }}
continue-on-error: true continue-on-error: true
uses: peter-evans/create-pull-request@v4 uses: peter-evans/create-pull-request@v4
- name: Get dependencies
run: go mod tidy
- name: Build
run: go build -v ./...
- name: Test
run: go test $(go list ./...)

View File

@@ -21,6 +21,8 @@
package docx package docx
import ( import (
"fmt"
"strconv"
"unsafe" "unsafe"
) )
@@ -38,3 +40,31 @@ func StringToBytes(s string) (b []byte) {
bh.cap = sh.len bh.cap = sh.len
return b return b
} }
// GetInt64 from string
func GetInt64(s string) (int64, error) {
v, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return v, nil
}
v2, err := strconv.ParseFloat(s, 64)
if err == nil {
return int64(v2), nil
}
_, err = fmt.Sscanf(s, "%d", &v)
return v, err
}
// GetInt from string
func GetInt(s string) (int, error) {
v, err := strconv.Atoi(s)
if err == nil {
return v, nil
}
v2, err := strconv.ParseFloat(s, 64)
if err == nil {
return int(v2), nil
}
_, err = fmt.Sscanf(s, "%d", &v)
return v, err
}

View File

@@ -137,28 +137,19 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "distT": case "distT":
r.DistT, err = strconv.ParseInt(attr.Value, 10, 64) r.DistT, err = GetInt64(attr.Value)
if err != nil {
return
}
case "distB": case "distB":
r.DistB, err = strconv.ParseInt(attr.Value, 10, 64) r.DistB, err = GetInt64(attr.Value)
if err != nil {
return
}
case "distL": case "distL":
r.DistL, err = strconv.ParseInt(attr.Value, 10, 64) r.DistL, err = GetInt64(attr.Value)
if err != nil {
return
}
case "distR": case "distR":
r.DistR, err = strconv.ParseInt(attr.Value, 10, 64) r.DistR, err = GetInt64(attr.Value)
if err != nil {
return
}
default: default:
// ignore other attributes // ignore other attributes
} }
if err != nil {
return err
}
} }
for { for {
t, err := d.Token() t, err := d.Token()
@@ -176,9 +167,9 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
for _, v := range tt.Attr { for _, v := range tt.Attr {
switch v.Name.Local { switch v.Name.Local {
case "cx": case "cx":
r.Extent.CX, err = strconv.ParseInt(v.Value, 10, 64) r.Extent.CX, err = GetInt64(v.Value)
case "cy": case "cy":
r.Extent.CY, err = strconv.ParseInt(v.Value, 10, 64) r.Extent.CY, err = GetInt64(v.Value)
} }
if err != nil { if err != nil {
return err return err
@@ -189,13 +180,13 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
for _, v := range tt.Attr { for _, v := range tt.Attr {
switch v.Name.Local { switch v.Name.Local {
case "l": case "l":
r.EffectExtent.L, err = strconv.ParseInt(v.Value, 10, 64) r.EffectExtent.L, err = GetInt64(v.Value)
case "t": case "t":
r.EffectExtent.T, err = strconv.ParseInt(v.Value, 10, 64) r.EffectExtent.T, err = GetInt64(v.Value)
case "r": case "r":
r.EffectExtent.R, err = strconv.ParseInt(v.Value, 10, 64) r.EffectExtent.R, err = GetInt64(v.Value)
case "b": case "b":
r.EffectExtent.B, err = strconv.ParseInt(v.Value, 10, 64) r.EffectExtent.B, err = GetInt64(v.Value)
} }
if err != nil { if err != nil {
return err return err
@@ -365,15 +356,12 @@ func (r *WPExtent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "cx": case "cx":
r.CX, err = strconv.ParseInt(attr.Value, 10, 64) r.CX, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "cy": case "cy":
r.CY, err = strconv.ParseInt(attr.Value, 10, 64) r.CY, err = GetInt64(attr.Value)
if err != nil { }
return err if err != nil {
} return err
} }
} }
// Consume the end element // Consume the end element
@@ -396,25 +384,16 @@ func (r *WPEffectExtent) UnmarshalXML(d *xml.Decoder, start xml.StartElement) er
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "l": case "l":
r.L, err = strconv.ParseInt(attr.Value, 10, 64) r.L, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "t": case "t":
r.T, err = strconv.ParseInt(attr.Value, 10, 64) r.T, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "r": case "r":
r.R, err = strconv.ParseInt(attr.Value, 10, 64) r.R, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "b": case "b":
r.B, err = strconv.ParseInt(attr.Value, 10, 64) r.B, err = GetInt64(attr.Value)
if err != nil { }
return err if err != nil {
} return err
} }
} }
// Consume the end element // Consume the end element
@@ -434,7 +413,7 @@ func (r *WPDocPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "id": case "id":
id, err := strconv.Atoi(attr.Value) id, err := GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
@@ -475,7 +454,7 @@ func (w *WPCNvGraphicFramePr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
if v == "" { if v == "" {
continue continue
} }
w.Locks.NoChangeAspect, err = strconv.Atoi(v) w.Locks.NoChangeAspect, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }
@@ -706,7 +685,7 @@ func (p *PICNonVisualPicProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElem
if v == "" { if v == "" {
continue continue
} }
p.NonVisualDrawingProperties.ID, err = strconv.Atoi(v) p.NonVisualDrawingProperties.ID, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }
@@ -755,7 +734,7 @@ func (p *PicCNvPicPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
if v == "" { if v == "" {
continue continue
} }
value.NoChangeAspect, err = strconv.Atoi(v) value.NoChangeAspect, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }
@@ -857,7 +836,7 @@ func (a *ABlip) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if v == "" { if v == "" {
continue continue
} }
value.Amount, err = strconv.Atoi(v) value.Amount, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }
@@ -990,23 +969,17 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "rot": case "rot":
a.Rot, err = strconv.ParseInt(attr.Value, 10, 64) a.Rot, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "flipH": case "flipH":
a.FlipH, err = strconv.Atoi(attr.Value) a.FlipH, err = GetInt(attr.Value)
if err != nil {
return err
}
case "flipV": case "flipV":
a.FlipV, err = strconv.Atoi(attr.Value) a.FlipV, err = GetInt(attr.Value)
if err != nil {
return err
}
default: default:
// ignore other attributes // ignore other attributes
} }
if err != nil {
return err
}
} }
for { for {
t, err := d.Token() t, err := d.Token()
@@ -1023,9 +996,9 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
for _, v := range tt.Attr { for _, v := range tt.Attr {
switch v.Name.Local { switch v.Name.Local {
case "x": case "x":
a.Off.X, err = strconv.ParseInt(v.Value, 10, 64) a.Off.X, err = GetInt64(v.Value)
case "y": case "y":
a.Off.Y, err = strconv.ParseInt(v.Value, 10, 64) a.Off.Y, err = GetInt64(v.Value)
} }
if err != nil { if err != nil {
return err return err
@@ -1035,9 +1008,9 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
for _, v := range tt.Attr { for _, v := range tt.Attr {
switch v.Name.Local { switch v.Name.Local {
case "cx": case "cx":
a.Ext.CX, err = strconv.ParseInt(v.Value, 10, 64) a.Ext.CX, err = GetInt64(v.Value)
case "cy": case "cy":
a.Ext.CY, err = strconv.ParseInt(v.Value, 10, 64) a.Ext.CY, err = GetInt64(v.Value)
} }
if err != nil { if err != nil {
return err return err
@@ -1048,9 +1021,9 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
for _, v := range tt.Attr { for _, v := range tt.Attr {
switch v.Name.Local { switch v.Name.Local {
case "x": case "x":
value.X, err = strconv.ParseInt(v.Value, 10, 64) value.X, err = GetInt64(v.Value)
case "y": case "y":
value.Y, err = strconv.ParseInt(v.Value, 10, 64) value.Y, err = GetInt64(v.Value)
} }
if err != nil { if err != nil {
return err return err
@@ -1062,9 +1035,9 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
for _, v := range tt.Attr { for _, v := range tt.Attr {
switch v.Name.Local { switch v.Name.Local {
case "cx": case "cx":
value.CX, err = strconv.ParseInt(v.Value, 10, 64) value.CX, err = GetInt64(v.Value)
case "cy": case "cy":
value.CY, err = strconv.ParseInt(v.Value, 10, 64) value.CY, err = GetInt64(v.Value)
} }
if err != nil { if err != nil {
return err return err
@@ -1228,55 +1201,28 @@ func (r *WPAnchor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
for _, tt := range start.Attr { for _, tt := range start.Attr {
switch tt.Name.Local { switch tt.Name.Local {
case "distT": case "distT":
r.DistT, err = strconv.ParseInt(tt.Value, 10, 64) r.DistT, err = GetInt64(tt.Value)
if err != nil {
return err
}
case "distB": case "distB":
r.DistB, err = strconv.ParseInt(tt.Value, 10, 64) r.DistB, err = GetInt64(tt.Value)
if err != nil {
return err
}
case "distL": case "distL":
r.DistL, err = strconv.ParseInt(tt.Value, 10, 64) r.DistL, err = GetInt64(tt.Value)
if err != nil {
return err
}
case "distR": case "distR":
r.DistR, err = strconv.ParseInt(tt.Value, 10, 64) r.DistR, err = GetInt64(tt.Value)
if err != nil {
return err
}
case "simplePos": case "simplePos":
r.SimplePos, err = strconv.Atoi(tt.Value) r.SimplePos, err = GetInt(tt.Value)
if err != nil {
return err
}
case "relativeHeight": case "relativeHeight":
r.RelativeHeight, err = strconv.Atoi(tt.Value) r.RelativeHeight, err = GetInt(tt.Value)
if err != nil {
return err
}
case "behindDoc": case "behindDoc":
r.BehindDoc, err = strconv.Atoi(tt.Value) r.BehindDoc, err = GetInt(tt.Value)
if err != nil {
return err
}
case "locked": case "locked":
r.Locked, err = strconv.Atoi(tt.Value) r.Locked, err = GetInt(tt.Value)
if err != nil {
return err
}
case "layoutInCell": case "layoutInCell":
r.LayoutInCell, err = strconv.Atoi(tt.Value) r.LayoutInCell, err = GetInt(tt.Value)
if err != nil {
return err
}
case "allowOverlap": case "allowOverlap":
r.AllowOverlap, err = strconv.Atoi(tt.Value) r.AllowOverlap, err = GetInt(tt.Value)
if err != nil { }
return err if err != nil {
} return err
} }
} }
for { for {
@@ -1295,9 +1241,9 @@ func (r *WPAnchor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
for _, v := range tt.Attr { for _, v := range tt.Attr {
switch v.Name.Local { switch v.Name.Local {
case "x": case "x":
r.SimplePosXY.X, err = strconv.ParseInt(v.Value, 10, 64) r.SimplePosXY.X, err = GetInt64(v.Value)
case "y": case "y":
r.SimplePosXY.Y, err = strconv.ParseInt(v.Value, 10, 64) r.SimplePosXY.Y, err = GetInt64(v.Value)
} }
if err != nil { if err != nil {
return err return err

View File

@@ -23,7 +23,6 @@ package docx
import ( import (
"encoding/xml" "encoding/xml"
"io" "io"
"strconv"
"strings" "strings"
) )
@@ -263,7 +262,7 @@ func (r *NonVisualProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElemen
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "id": case "id":
r.ID, err = strconv.Atoi(attr.Value) r.ID, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
@@ -295,22 +294,22 @@ func (s *Spacing) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err erro
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "val": case "val":
s.Val, err = strconv.Atoi(attr.Value) s.Val, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
case "beforeLines": case "beforeLines":
s.BeforeLines, err = strconv.Atoi(attr.Value) s.BeforeLines, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
case "before": case "before":
s.Before, err = strconv.Atoi(attr.Value) s.Before, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
case "line": case "line":
s.Line, err = strconv.Atoi(attr.Value) s.Line, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
@@ -345,7 +344,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
i.LeftChars, err = strconv.Atoi(attr.Value) i.LeftChars, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
@@ -353,7 +352,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
i.Left, err = strconv.Atoi(attr.Value) i.Left, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
@@ -361,7 +360,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
i.FirstLineChars, err = strconv.Atoi(attr.Value) i.FirstLineChars, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
@@ -369,7 +368,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
i.FirstLine, err = strconv.Atoi(attr.Value) i.FirstLine, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
@@ -377,7 +376,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
i.HangingChars, err = strconv.Atoi(attr.Value) i.HangingChars, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }
@@ -385,7 +384,7 @@ func (i *Ind) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
i.Hanging, err = strconv.Atoi(attr.Value) i.Hanging, err = GetInt(attr.Value)
if err != nil { if err != nil {
return return
} }

View File

@@ -24,7 +24,6 @@ import (
"encoding/xml" "encoding/xml"
"io" "io"
"reflect" "reflect"
"strconv"
"strings" "strings"
) )
@@ -95,7 +94,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
if v == "" { if v == "" {
continue continue
} }
value.Val, err = strconv.ParseInt(v, 10, 64) value.Val, err = GetInt64(v)
if err != nil { if err != nil {
return err return err
} }
@@ -117,7 +116,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
if v == "" { if v == "" {
continue continue
} }
value.Val, err = strconv.Atoi(v) value.Val, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }
@@ -128,7 +127,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
if v == "" { if v == "" {
continue continue
} }
value.Val, err = strconv.Atoi(v) value.Val, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }
@@ -139,7 +138,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
if v == "" { if v == "" {
continue continue
} }
value.Val, err = strconv.Atoi(v) value.Val, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }
@@ -150,7 +149,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
if v == "" { if v == "" {
continue continue
} }
value.Val, err = strconv.Atoi(v) value.Val, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -24,7 +24,6 @@ import (
"encoding/xml" "encoding/xml"
"io" "io"
"reflect" "reflect"
"strconv"
"strings" "strings"
) )
@@ -289,7 +288,7 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
if v == "" { if v == "" {
continue continue
} }
value.Val, err = strconv.ParseInt(v, 10, 64) value.Val, err = GetInt64(v)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -23,7 +23,6 @@ package docx
import ( import (
"encoding/xml" "encoding/xml"
"io" "io"
"strconv"
"strings" "strings"
) )
@@ -148,7 +147,7 @@ func (w *WPSCNvSpPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err e
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "txBox": case "txBox":
w.TxBox, err = strconv.Atoi(attr.Value) w.TxBox, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
@@ -197,7 +196,7 @@ func (l *ASPLocks) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "noChangeArrowheads": case "noChangeArrowheads":
l.NoChangeArrowheads, err = strconv.Atoi(attr.Value) l.NoChangeArrowheads, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
@@ -226,12 +225,12 @@ func (r *ABlipFill) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err er
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "dpi": case "dpi":
r.DPI, err = strconv.Atoi(attr.Value) r.DPI, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
case "rotWithShape": case "rotWithShape":
r.RotWithShape, err = strconv.Atoi(attr.Value) r.RotWithShape, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
@@ -300,25 +299,13 @@ func (t *ATile) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "tx": case "tx":
t.TX, err = strconv.ParseInt(attr.Value, 10, 64) t.TX, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "ty": case "ty":
t.TY, err = strconv.ParseInt(attr.Value, 10, 64) t.TY, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "sx": case "sx":
t.SX, err = strconv.ParseInt(attr.Value, 10, 64) t.SX, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "sy": case "sy":
t.SY, err = strconv.ParseInt(attr.Value, 10, 64) t.SY, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "flip": case "flip":
t.Flip = attr.Value t.Flip = attr.Value
case "algn": case "algn":
@@ -326,6 +313,9 @@ func (t *ATile) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
default: default:
// ignore other attributes // ignore other attributes
} }
if err != nil {
return err
}
} }
// Consume the end element // Consume the end element
_, err = d.Token() _, err = d.Token()
@@ -354,10 +344,7 @@ func (l *ALine) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "w": case "w":
l.W, err = strconv.ParseInt(attr.Value, 10, 64) l.W, err = GetInt64(attr.Value)
if err != nil {
return err
}
case "cap": case "cap":
l.Cap = attr.Value l.Cap = attr.Value
case "cmpd": case "cmpd":
@@ -367,6 +354,9 @@ func (l *ALine) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)
default: default:
// ignore other attributes // ignore other attributes
} }
if err != nil {
return err
}
} }
for { for {
t, err := d.Token() t, err := d.Token()
@@ -640,25 +630,25 @@ func (r *WPSBodyPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "rot": case "rot":
r.Rot, _ = strconv.Atoi(attr.Value) r.Rot, _ = GetInt(attr.Value)
case "vert": case "vert":
r.Vert = attr.Value r.Vert = attr.Value
case "wrap": case "wrap":
r.Wrap = attr.Value r.Wrap = attr.Value
case "lIns": case "lIns":
r.LIns, _ = strconv.ParseInt(attr.Value, 10, 64) r.LIns, _ = GetInt64(attr.Value)
case "tIns": case "tIns":
r.TIns, _ = strconv.ParseInt(attr.Value, 10, 64) r.TIns, _ = GetInt64(attr.Value)
case "rIns": case "rIns":
r.RIns, _ = strconv.ParseInt(attr.Value, 10, 64) r.RIns, _ = GetInt64(attr.Value)
case "bIns": case "bIns":
r.BIns, _ = strconv.ParseInt(attr.Value, 10, 64) r.BIns, _ = GetInt64(attr.Value)
case "anchor": case "anchor":
r.Anchor = attr.Value r.Anchor = attr.Value
case "anchorCtr": case "anchorCtr":
r.AnchorCtr, _ = strconv.Atoi(attr.Value) r.AnchorCtr, _ = GetInt(attr.Value)
case "upright": case "upright":
r.Upright, _ = strconv.Atoi(attr.Value) r.Upright, _ = GetInt(attr.Value)
default: default:
// ignore other attributes // ignore other attributes
} }

View File

@@ -23,7 +23,6 @@ package docx
import ( import (
"encoding/xml" "encoding/xml"
"io" "io"
"strconv"
"strings" "strings"
) )
@@ -203,12 +202,12 @@ func (tp *WTablePositioningProperties) UnmarshalXML(d *xml.Decoder, start xml.St
for _, attr := range start.Attr { for _, attr := range start.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "leftFromText": case "leftFromText":
tp.LeftFromText, err = strconv.Atoi(attr.Value) tp.LeftFromText, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
case "rightFromText": case "rightFromText":
tp.RightFromText, err = strconv.Atoi(attr.Value) tp.RightFromText, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
@@ -221,12 +220,12 @@ func (tp *WTablePositioningProperties) UnmarshalXML(d *xml.Decoder, start xml.St
case "tblpYSpec": case "tblpYSpec":
tp.TblpYSpec = attr.Value tp.TblpYSpec = attr.Value
case "tblpX": case "tblpX":
tp.TblpX, err = strconv.Atoi(attr.Value) tp.TblpX, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
case "tblpY": case "tblpY":
tp.TblpY, err = strconv.Atoi(attr.Value) tp.TblpY, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
@@ -277,13 +276,9 @@ func (t *WTableWidth) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err
} }
switch attr.Name.Local { switch attr.Name.Local {
case "w": case "w":
t.W, err = strconv.ParseInt(attr.Value, 10, 64) t.W, err = GetInt64(attr.Value)
if err != nil { if err != nil {
w, err := strconv.ParseFloat(attr.Value, 64) return err
if err != nil {
return err
}
t.W = int64(w)
} }
case "type": case "type":
t.Type = attr.Value t.Type = attr.Value
@@ -390,13 +385,9 @@ func (g *WGridCol) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err
} }
switch attr.Name.Local { switch attr.Name.Local {
case "w": case "w":
g.W, err = strconv.ParseInt(attr.Value, 10, 64) g.W, err = GetInt64(attr.Value)
if err != nil { if err != nil {
w, err := strconv.ParseFloat(attr.Value, 64) return err
if err != nil {
return err
}
g.W = int64(w)
} }
default: default:
// ignore other attributes // ignore other attributes
@@ -496,13 +487,9 @@ func (t *WTableRowProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
for _, attr := range tt.Attr { for _, attr := range tt.Attr {
switch attr.Name.Local { switch attr.Name.Local {
case "val": case "val":
th.Val, err = strconv.ParseInt(attr.Value, 10, 64) th.Val, err = GetInt64(attr.Value)
if err != nil { if err != nil {
w, err := strconv.ParseFloat(attr.Value, 64) return err
if err != nil {
return err
}
th.Val = int64(w)
} }
case "hRule": case "hRule":
th.Rule = attr.Value th.Rule = attr.Value
@@ -623,13 +610,9 @@ func (r *WTableCellProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement)
if v == "" { if v == "" {
continue continue
} }
r.TableCellWidth.W, err = strconv.ParseInt(v, 10, 64) r.TableCellWidth.W, err = GetInt64(v)
if err != nil { if err != nil {
w, err := strconv.ParseFloat(v, 64) return err
if err != nil {
return err
}
r.TableCellWidth.W = int64(w)
} }
r.TableCellWidth.Type = getAtt(tt.Attr, "type") r.TableCellWidth.Type = getAtt(tt.Attr, "type")
case "vMerge": case "vMerge":
@@ -640,7 +623,7 @@ func (r *WTableCellProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement)
if v == "" { if v == "" {
continue continue
} }
r.GridSpan.Val, err = strconv.Atoi(v) r.GridSpan.Val, err = GetInt(v)
if err != nil { if err != nil {
return err return err
} }
@@ -805,7 +788,7 @@ func (t *WTableBorder) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
sz, err := strconv.Atoi(attr.Value) sz, err := GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }
@@ -814,7 +797,7 @@ func (t *WTableBorder) UnmarshalXML(d *xml.Decoder, start xml.StartElement) erro
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
space, err := strconv.Atoi(attr.Value) space, err := GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -24,7 +24,6 @@ import (
"encoding/xml" "encoding/xml"
"io" "io"
"reflect" "reflect"
"strconv"
"strings" "strings"
) )
@@ -78,7 +77,7 @@ func (t *Tab) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if attr.Value == "" { if attr.Value == "" {
continue continue
} }
t.Position, err = strconv.Atoi(attr.Value) t.Position, err = GetInt(attr.Value)
if err != nil { if err != nil {
return err return err
} }