@@ -420,9 +420,15 @@ func (f *File) DeleteFormControl(sheet, cell string) error {
420420 for i , sp := range vml .Shape {
421421 var shapeVal decodeShapeVal
422422 if err = xml .Unmarshal ([]byte (fmt .Sprintf ("<shape>%s</shape>" , sp .Val )), & shapeVal ); err == nil &&
423- shapeVal .ClientData .ObjectType != "Note" && shapeVal .ClientData .Column == col - 1 && shapeVal .ClientData .Row == row - 1 {
424- vml .Shape = append (vml .Shape [:i ], vml .Shape [i + 1 :]... )
425- break
423+ shapeVal .ClientData .ObjectType != "Note" && shapeVal .ClientData .Anchor != "" {
424+ leftCol , topRow , err := extractAnchorCell (shapeVal .ClientData .Anchor )
425+ if err != nil {
426+ return err
427+ }
428+ if leftCol == col - 1 && topRow == row - 1 {
429+ vml .Shape = append (vml .Shape [:i ], vml .Shape [i + 1 :]... )
430+ break
431+ }
426432 }
427433 }
428434 f .VMLDrawing [drawingVML ] = vml
@@ -454,7 +460,7 @@ func (f *File) decodeVMLDrawingReader(path string) (*decodeVmlDrawing, error) {
454460 c , ok := f .Pkg .Load (path )
455461 if ok && c != nil {
456462 f .DecodeVMLDrawing [path ] = new (decodeVmlDrawing )
457- if err := f .xmlNewDecoder (bytes .NewReader (namespaceStrictToTransitional (c .([]byte )))).
463+ if err := f .xmlNewDecoder (bytes .NewReader (bytesReplace ( namespaceStrictToTransitional (c .([]byte )), [] byte ( "<br> \r \n " ), [] byte ( "<br></br> \r \n " ), - 1 ))).
458464 Decode (f .DecodeVMLDrawing [path ]); err != nil && err != io .EOF {
459465 return nil , err
460466 }
@@ -574,6 +580,9 @@ func formCtrlText(opts *vmlOptions) []vmlFont {
574580 if run .Font .Underline == "single" {
575581 fnt .Content = "<u>" + fnt .Content + "</u>"
576582 }
583+ if run .Font .Underline == "double" {
584+ fnt .Content = "<u class=\" font1\" >" + fnt .Content + "</u>"
585+ }
577586 if run .Font .Italic {
578587 fnt .Content = "<i>" + fnt .Content + "</i>"
579588 }
@@ -765,8 +774,8 @@ func (f *File) addFormCtrlShape(preset formCtrlPreset, col, row int, anchor stri
765774 ObjectType : preset .objectType ,
766775 Anchor : anchor ,
767776 AutoFill : preset .autoFill ,
768- Row : row - 1 ,
769- Column : col - 1 ,
777+ Row : intPtr ( row - 1 ) ,
778+ Column : intPtr ( col - 1 ) ,
770779 TextHAlign : preset .textHAlign ,
771780 TextVAlign : preset .textVAlign ,
772781 NoThreeD : preset .noThreeD ,
@@ -885,8 +894,8 @@ func (f *File) addDrawingVML(dataID int, drawingVML string, opts *vmlOptions) er
885894}
886895
887896// GetFormControls retrieves all form controls in a worksheet by a given
888- // worksheet name. Note that, this function does not support getting the width,
889- // height, text, rich text, and format currently.
897+ // worksheet name. Note that, this function does not support getting the width
898+ // and height of the form controls currently.
890899func (f * File ) GetFormControls (sheet string ) ([]FormControl , error ) {
891900 var formControls []FormControl
892901 // Read sheet data
@@ -949,9 +958,18 @@ func extractFormControl(clientData string) (FormControl, error) {
949958 return formControl , err
950959 }
951960 for formCtrlType , preset := range formCtrlPresets {
952- if shapeVal .ClientData .ObjectType == preset .objectType {
961+ if shapeVal .ClientData .ObjectType == preset .objectType && shapeVal .ClientData .Anchor != "" {
962+ formControl .Paragraph = extractVMLFont (shapeVal .TextBox .Div .Font )
963+ if len (formControl .Paragraph ) > 0 && formControl .Paragraph [0 ].Font == nil {
964+ formControl .Text = formControl .Paragraph [0 ].Text
965+ formControl .Paragraph = formControl .Paragraph [1 :]
966+ }
953967 formControl .Type = formCtrlType
954- if formControl .Cell , err = CoordinatesToCellName (shapeVal .ClientData .Column + 1 , shapeVal .ClientData .Row + 1 ); err != nil {
968+ col , row , err := extractAnchorCell (shapeVal .ClientData .Anchor )
969+ if err != nil {
970+ return formControl , err
971+ }
972+ if formControl .Cell , err = CoordinatesToCellName (col + 1 , row + 1 ); err != nil {
955973 return formControl , err
956974 }
957975 formControl .Macro = shapeVal .ClientData .FmlaMacro
@@ -967,3 +985,79 @@ func extractFormControl(clientData string) (FormControl, error) {
967985 }
968986 return formControl , err
969987}
988+
989+ // extractAnchorCell extract left-top cell coordinates from given VML anchor
990+ // comma-separated list values.
991+ func extractAnchorCell (anchor string ) (int , int , error ) {
992+ var (
993+ leftCol , topRow int
994+ err error
995+ pos = strings .Split (anchor , "," )
996+ )
997+ if len (pos ) != 8 {
998+ return leftCol , topRow , ErrParameterInvalid
999+ }
1000+ leftCol , err = strconv .Atoi (strings .TrimSpace (pos [0 ]))
1001+ if err != nil {
1002+ return leftCol , topRow , ErrColumnNumber
1003+ }
1004+ topRow , err = strconv .Atoi (strings .TrimSpace (pos [2 ]))
1005+ return leftCol , topRow , err
1006+ }
1007+
1008+ // extractVMLFont extract rich-text and font format from given VML font element.
1009+ func extractVMLFont (font []decodeVMLFont ) []RichTextRun {
1010+ var runs []RichTextRun
1011+ extractU := func (u * decodeVMLFontU , run * RichTextRun ) {
1012+ if u == nil {
1013+ return
1014+ }
1015+ run .Text += u .Val
1016+ if run .Font == nil {
1017+ run .Font = & Font {}
1018+ }
1019+ run .Font .Underline = "single"
1020+ if u .Class == "font1" {
1021+ run .Font .Underline = "double"
1022+ }
1023+ }
1024+ extractI := func (i * decodeVMLFontI , run * RichTextRun ) {
1025+ if i == nil {
1026+ return
1027+ }
1028+ extractU (i .U , run )
1029+ run .Text += i .Val
1030+ if run .Font == nil {
1031+ run .Font = & Font {}
1032+ }
1033+ run .Font .Italic = true
1034+ }
1035+ extractB := func (b * decodeVMLFontB , run * RichTextRun ) {
1036+ if b == nil {
1037+ return
1038+ }
1039+ extractI (b .I , run )
1040+ run .Text += b .Val
1041+ if run .Font == nil {
1042+ run .Font = & Font {}
1043+ }
1044+ run .Font .Bold = true
1045+ }
1046+ for _ , fnt := range font {
1047+ var run RichTextRun
1048+ extractB (fnt .B , & run )
1049+ extractI (fnt .I , & run )
1050+ extractU (fnt .U , & run )
1051+ run .Text += fnt .Val
1052+ if fnt .Face != "" || fnt .Size > 0 || fnt .Color != "" {
1053+ if run .Font == nil {
1054+ run .Font = & Font {}
1055+ }
1056+ run .Font .Family = fnt .Face
1057+ run .Font .Size = float64 (fnt .Size / 20 )
1058+ run .Font .Color = fnt .Color
1059+ }
1060+ runs = append (runs , run )
1061+ }
1062+ return runs
1063+ }
0 commit comments