MCPcopy
hub / github.com/qax-os/excelize / addContentTypePart

Method addContentTypePart

workbook.go:361–414  ·  view source on GitHub ↗

addContentTypePart provides a function to add content type part relationships in the file [Content_Types].xml by given index and content type.

(index int, contentType string)

Source from the content-addressed store, hash-verified

359// addContentTypePart provides a function to add content type part relationships
360// in the file [Content_Types].xml by given index and content type.
361func (f *File) addContentTypePart(index int, contentType string) error {
362 setContentType := map[string]func() error{
363 "comments": f.setContentTypePartVMLExtensions,
364 "drawings": f.setContentTypePartImageExtensions,
365 }
366 partNames := map[string]string{
367 "chart": "/xl/charts/chart" + strconv.Itoa(index) + ".xml",
368 "chartsheet": "/xl/chartsheets/sheet" + strconv.Itoa(index) + ".xml",
369 "comments": "/xl/comments" + strconv.Itoa(index) + ".xml",
370 "customProperties": "/docProps/custom.xml",
371 "drawings": "/xl/drawings/drawing" + strconv.Itoa(index) + ".xml",
372 "table": "/xl/tables/table" + strconv.Itoa(index) + ".xml",
373 "pivotTable": "/xl/pivotTables/pivotTable" + strconv.Itoa(index) + ".xml",
374 "pivotCache": "/xl/pivotCache/pivotCacheDefinition" + strconv.Itoa(index) + ".xml",
375 "sharedStrings": "/xl/sharedStrings.xml",
376 "slicer": "/xl/slicers/slicer" + strconv.Itoa(index) + ".xml",
377 "slicerCache": "/xl/slicerCaches/slicerCache" + strconv.Itoa(index) + ".xml",
378 }
379 contentTypes := map[string]string{
380 "chart": ContentTypeDrawingML,
381 "chartsheet": ContentTypeSpreadSheetMLChartsheet,
382 "comments": ContentTypeSpreadSheetMLComments,
383 "customProperties": ContentTypeCustomProperties,
384 "drawings": ContentTypeDrawing,
385 "table": ContentTypeSpreadSheetMLTable,
386 "pivotTable": ContentTypeSpreadSheetMLPivotTable,
387 "pivotCache": ContentTypeSpreadSheetMLPivotCacheDefinition,
388 "sharedStrings": ContentTypeSpreadSheetMLSharedStrings,
389 "slicer": ContentTypeSlicer,
390 "slicerCache": ContentTypeSlicerCache,
391 }
392 s, ok := setContentType[contentType]
393 if ok {
394 if err := s(); err != nil {
395 return err
396 }
397 }
398 content, err := f.contentTypesReader()
399 if err != nil {
400 return err
401 }
402 content.mu.Lock()
403 defer content.mu.Unlock()
404 for _, v := range content.Overrides {
405 if v.PartName == partNames[contentType] {
406 return err
407 }
408 }
409 content.Overrides = append(content.Overrides, xlsxOverride{
410 PartName: partNames[contentType],
411 ContentType: contentTypes[contentType],
412 })
413 return f.setContentTypePartRelsExtensions()
414}
415
416// removeContentTypesPart provides a function to remove relationships by given
417// content type and part name in the file [Content_Types].xml.

Callers 14

addSlicerMethod · 0.95
addSlicerCacheMethod · 0.95
addDrawingSlicerMethod · 0.95
TestAddContentTypePartFunction · 0.95
AddTableMethod · 0.95
AddChartMethod · 0.95
AddChartSheetMethod · 0.95
sharedStringsReaderMethod · 0.95
SetCustomPropsMethod · 0.95
AddPivotTableMethod · 0.95
addVMLObjectMethod · 0.95
AddShapeMethod · 0.95

Calls 2

contentTypesReaderMethod · 0.95

Tested by 1

TestAddContentTypePartFunction · 0.76