@@ -130,13 +130,14 @@ func (f *File) AddComment(sheet string, opts Comment) error {
130
130
//
131
131
// err := f.DeleteComment("Sheet1", "A30")
132
132
func (f * File ) DeleteComment (sheet , cell string ) error {
133
- if err := checkSheetName (sheet ); err != nil {
133
+ ws , err := f .workSheetReader (sheet )
134
+ if err != nil {
134
135
return err
135
136
}
136
- sheetXMLPath , ok := f .getSheetXMLPath (sheet )
137
- if ! ok {
138
- return ErrSheetNotExist {sheet }
137
+ if ws .LegacyDrawing == nil {
138
+ return err
139
139
}
140
+ sheetXMLPath , _ := f .getSheetXMLPath (sheet )
140
141
commentsXML := f .getSheetComments (filepath .Base (sheetXMLPath ))
141
142
if ! strings .HasPrefix (commentsXML , "/" ) {
142
143
commentsXML = "xl" + strings .TrimPrefix (commentsXML , ".." )
@@ -164,6 +165,82 @@ func (f *File) DeleteComment(sheet, cell string) error {
164
165
}
165
166
f .Comments [commentsXML ] = cmts
166
167
}
168
+ sheetRelationshipsDrawingVML := f .getSheetRelationshipsTargetByID (sheet , ws .LegacyDrawing .RID )
169
+ return f .deleteFormControl (sheetRelationshipsDrawingVML , cell , true )
170
+ }
171
+
172
+ // deleteFormControl provides the method to delete shape from
173
+ // xl/drawings/vmlDrawing%d.xml by giving path, cell and shape type.
174
+ func (f * File ) deleteFormControl (sheetRelationshipsDrawingVML , cell string , isComment bool ) error {
175
+ col , row , err := CellNameToCoordinates (cell )
176
+ if err != nil {
177
+ return err
178
+ }
179
+ vmlID , _ := strconv .Atoi (strings .TrimSuffix (strings .TrimPrefix (sheetRelationshipsDrawingVML , "../drawings/vmlDrawing" ), ".vml" ))
180
+ drawingVML := strings .ReplaceAll (sheetRelationshipsDrawingVML , ".." , "xl" )
181
+ vml := f .VMLDrawing [drawingVML ]
182
+ if vml == nil {
183
+ vml = & vmlDrawing {
184
+ XMLNSv : "urn:schemas-microsoft-com:vml" ,
185
+ XMLNSo : "urn:schemas-microsoft-com:office:office" ,
186
+ XMLNSx : "urn:schemas-microsoft-com:office:excel" ,
187
+ XMLNSmv : "http://macVmlSchemaUri" ,
188
+ ShapeLayout : & xlsxShapeLayout {
189
+ Ext : "edit" , IDmap : & xlsxIDmap {Ext : "edit" , Data : vmlID },
190
+ },
191
+ ShapeType : & xlsxShapeType {
192
+ Stroke : & xlsxStroke {JoinStyle : "miter" },
193
+ VPath : & vPath {GradientShapeOK : "t" , ConnectType : "rect" },
194
+ },
195
+ }
196
+ // Load exist VML shapes from xl/drawings/vmlDrawing%d.vml
197
+ d , err := f .decodeVMLDrawingReader (drawingVML )
198
+ if err != nil {
199
+ return err
200
+ }
201
+ if d != nil {
202
+ vml .ShapeType .ID = d .ShapeType .ID
203
+ vml .ShapeType .CoordSize = d .ShapeType .CoordSize
204
+ vml .ShapeType .Spt = d .ShapeType .Spt
205
+ vml .ShapeType .Path = d .ShapeType .Path
206
+ for _ , v := range d .Shape {
207
+ s := xlsxShape {
208
+ ID : v .ID ,
209
+ Type : v .Type ,
210
+ Style : v .Style ,
211
+ Button : v .Button ,
212
+ Filled : v .Filled ,
213
+ FillColor : v .FillColor ,
214
+ InsetMode : v .InsetMode ,
215
+ Stroked : v .Stroked ,
216
+ StrokeColor : v .StrokeColor ,
217
+ Val : v .Val ,
218
+ }
219
+ vml .Shape = append (vml .Shape , s )
220
+ }
221
+ }
222
+ }
223
+ cond := func (objectType string ) bool {
224
+ if isComment {
225
+ return objectType == "Note"
226
+ }
227
+ return objectType != "Note"
228
+ }
229
+ for i , sp := range vml .Shape {
230
+ var shapeVal decodeShapeVal
231
+ if err = xml .Unmarshal ([]byte (fmt .Sprintf ("<shape>%s</shape>" , sp .Val )), & shapeVal ); err == nil &&
232
+ cond (shapeVal .ClientData .ObjectType ) && shapeVal .ClientData .Anchor != "" {
233
+ leftCol , topRow , err := extractAnchorCell (shapeVal .ClientData .Anchor )
234
+ if err != nil {
235
+ return err
236
+ }
237
+ if leftCol == col - 1 && topRow == row - 1 {
238
+ vml .Shape = append (vml .Shape [:i ], vml .Shape [i + 1 :]... )
239
+ break
240
+ }
241
+ }
242
+ }
243
+ f .VMLDrawing [drawingVML ] = vml
167
244
return err
168
245
}
169
246
@@ -375,74 +452,11 @@ func (f *File) DeleteFormControl(sheet, cell string) error {
375
452
if err != nil {
376
453
return err
377
454
}
378
- col , row , err := CellNameToCoordinates (cell )
379
- if err != nil {
380
- return err
381
- }
382
455
if ws .LegacyDrawing == nil {
383
456
return err
384
457
}
385
458
sheetRelationshipsDrawingVML := f .getSheetRelationshipsTargetByID (sheet , ws .LegacyDrawing .RID )
386
- vmlID , _ := strconv .Atoi (strings .TrimSuffix (strings .TrimPrefix (sheetRelationshipsDrawingVML , "../drawings/vmlDrawing" ), ".vml" ))
387
- drawingVML := strings .ReplaceAll (sheetRelationshipsDrawingVML , ".." , "xl" )
388
- vml := f .VMLDrawing [drawingVML ]
389
- if vml == nil {
390
- vml = & vmlDrawing {
391
- XMLNSv : "urn:schemas-microsoft-com:vml" ,
392
- XMLNSo : "urn:schemas-microsoft-com:office:office" ,
393
- XMLNSx : "urn:schemas-microsoft-com:office:excel" ,
394
- XMLNSmv : "http://macVmlSchemaUri" ,
395
- ShapeLayout : & xlsxShapeLayout {
396
- Ext : "edit" , IDmap : & xlsxIDmap {Ext : "edit" , Data : vmlID },
397
- },
398
- ShapeType : & xlsxShapeType {
399
- Stroke : & xlsxStroke {JoinStyle : "miter" },
400
- VPath : & vPath {GradientShapeOK : "t" , ConnectType : "rect" },
401
- },
402
- }
403
- // Load exist VML shapes from xl/drawings/vmlDrawing%d.vml
404
- d , err := f .decodeVMLDrawingReader (drawingVML )
405
- if err != nil {
406
- return err
407
- }
408
- if d != nil {
409
- vml .ShapeType .ID = d .ShapeType .ID
410
- vml .ShapeType .CoordSize = d .ShapeType .CoordSize
411
- vml .ShapeType .Spt = d .ShapeType .Spt
412
- vml .ShapeType .Path = d .ShapeType .Path
413
- for _ , v := range d .Shape {
414
- s := xlsxShape {
415
- ID : v .ID ,
416
- Type : v .Type ,
417
- Style : v .Style ,
418
- Button : v .Button ,
419
- Filled : v .Filled ,
420
- FillColor : v .FillColor ,
421
- InsetMode : v .InsetMode ,
422
- Stroked : v .Stroked ,
423
- StrokeColor : v .StrokeColor ,
424
- Val : v .Val ,
425
- }
426
- vml .Shape = append (vml .Shape , s )
427
- }
428
- }
429
- }
430
- for i , sp := range vml .Shape {
431
- var shapeVal decodeShapeVal
432
- if err = xml .Unmarshal ([]byte (fmt .Sprintf ("<shape>%s</shape>" , sp .Val )), & shapeVal ); err == nil &&
433
- shapeVal .ClientData .ObjectType != "Note" && shapeVal .ClientData .Anchor != "" {
434
- leftCol , topRow , err := extractAnchorCell (shapeVal .ClientData .Anchor )
435
- if err != nil {
436
- return err
437
- }
438
- if leftCol == col - 1 && topRow == row - 1 {
439
- vml .Shape = append (vml .Shape [:i ], vml .Shape [i + 1 :]... )
440
- break
441
- }
442
- }
443
- }
444
- f .VMLDrawing [drawingVML ] = vml
445
- return err
459
+ return f .deleteFormControl (sheetRelationshipsDrawingVML , cell , false )
446
460
}
447
461
448
462
// countVMLDrawing provides a function to get VML drawing files count storage
0 commit comments