Support get the cell images inserted by IMAGE formula function
This commit is contained in:
parent
9999221450
commit
5dc22e874b
37
excelize.go
37
excelize.go
|
@ -605,7 +605,7 @@ func (f *File) metadataReader() (*xlsxMetadata, error) {
|
||||||
// deserialization of xl/richData/richvalue.xml.
|
// deserialization of xl/richData/richvalue.xml.
|
||||||
func (f *File) richValueReader() (*xlsxRichValueData, error) {
|
func (f *File) richValueReader() (*xlsxRichValueData, error) {
|
||||||
var richValue xlsxRichValueData
|
var richValue xlsxRichValueData
|
||||||
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLRichDataRichValue)))).
|
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLRdRichValuePart)))).
|
||||||
Decode(&richValue); err != nil && err != io.EOF {
|
Decode(&richValue); err != nil && err != io.EOF {
|
||||||
return &richValue, err
|
return &richValue, err
|
||||||
}
|
}
|
||||||
|
@ -616,18 +616,43 @@ func (f *File) richValueReader() (*xlsxRichValueData, error) {
|
||||||
// after deserialization of xl/richData/richValueRel.xml.
|
// after deserialization of xl/richData/richValueRel.xml.
|
||||||
func (f *File) richValueRelReader() (*xlsxRichValueRels, error) {
|
func (f *File) richValueRelReader() (*xlsxRichValueRels, error) {
|
||||||
var richValueRels xlsxRichValueRels
|
var richValueRels xlsxRichValueRels
|
||||||
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLRichDataRichValueRel)))).
|
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLRdRichValueRel)))).
|
||||||
Decode(&richValueRels); err != nil && err != io.EOF {
|
Decode(&richValueRels); err != nil && err != io.EOF {
|
||||||
return &richValueRels, err
|
return &richValueRels, err
|
||||||
}
|
}
|
||||||
return &richValueRels, nil
|
return &richValueRels, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getRichDataRichValueRelRelationships provides a function to get drawing
|
// richValueWebImageReader provides a function to get the pointer to the
|
||||||
// relationships from xl/richData/_rels/richValueRel.xml.rels by given
|
// structure after deserialization of xl/richData/rdRichValueWebImage.xml.
|
||||||
// relationship ID.
|
func (f *File) richValueWebImageReader() (*xlsxWebImagesSupportingRichData, error) {
|
||||||
|
var richValueWebImages xlsxWebImagesSupportingRichData
|
||||||
|
if err := f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(defaultXMLRdRichValueWebImagePart)))).
|
||||||
|
Decode(&richValueWebImages); err != nil && err != io.EOF {
|
||||||
|
return &richValueWebImages, err
|
||||||
|
}
|
||||||
|
return &richValueWebImages, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getRichDataRichValueRelRelationships provides a function to get relationships
|
||||||
|
// from xl/richData/_rels/richValueRel.xml.rels by given relationship ID.
|
||||||
func (f *File) getRichDataRichValueRelRelationships(rID string) *xlsxRelationship {
|
func (f *File) getRichDataRichValueRelRelationships(rID string) *xlsxRelationship {
|
||||||
if rels, _ := f.relsReader(defaultXMLRichDataRichValueRelRels); rels != nil {
|
if rels, _ := f.relsReader(defaultXMLRdRichValueRelRels); rels != nil {
|
||||||
|
rels.mu.Lock()
|
||||||
|
defer rels.mu.Unlock()
|
||||||
|
for _, v := range rels.Relationships {
|
||||||
|
if v.ID == rID {
|
||||||
|
return &v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getRichValueWebImageRelationships provides a function to get relationships
|
||||||
|
// from xl/richData/_rels/rdRichValueWebImage.xml.rels by given relationship ID.
|
||||||
|
func (f *File) getRichValueWebImageRelationships(rID string) *xlsxRelationship {
|
||||||
|
if rels, _ := f.relsReader(defaultXMLRdRichValueWebImagePartRels); rels != nil {
|
||||||
rels.mu.Lock()
|
rels.mu.Lock()
|
||||||
defer rels.mu.Unlock()
|
defer rels.mu.Unlock()
|
||||||
for _, v := range rels.Relationships {
|
for _, v := range rels.Relationships {
|
||||||
|
|
91
picture.go
91
picture.go
|
@ -31,6 +31,7 @@ type PictureInsertType int
|
||||||
const (
|
const (
|
||||||
PictureInsertTypePlaceOverCells PictureInsertType = iota
|
PictureInsertTypePlaceOverCells PictureInsertType = iota
|
||||||
PictureInsertTypePlaceInCell
|
PictureInsertTypePlaceInCell
|
||||||
|
PictureInsertTypeIMAGE
|
||||||
PictureInsertTypeDISPIMG
|
PictureInsertTypeDISPIMG
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -450,8 +451,7 @@ func (f *File) addMedia(file []byte, ext string) string {
|
||||||
// GetPictures provides a function to get picture meta info and raw content
|
// GetPictures provides a function to get picture meta info and raw content
|
||||||
// embed in spreadsheet by given worksheet and cell name. This function
|
// embed in spreadsheet by given worksheet and cell name. This function
|
||||||
// returns the image contents as []byte data types. This function is
|
// returns the image contents as []byte data types. This function is
|
||||||
// concurrency safe. Note that, this function doesn't support getting cell image
|
// concurrency safe. For example:
|
||||||
// inserted by IMAGE formula function currently. For example:
|
|
||||||
//
|
//
|
||||||
// f, err := excelize.OpenFile("Book1.xlsx")
|
// f, err := excelize.OpenFile("Book1.xlsx")
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
|
@ -507,8 +507,7 @@ func (f *File) GetPictures(sheet, cell string) ([]Picture, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetPictureCells returns all picture cell references in a worksheet by a
|
// GetPictureCells returns all picture cell references in a worksheet by a
|
||||||
// specific worksheet name. Note that, this function doesn't support getting
|
// specific worksheet name.
|
||||||
// cell image inserted by IMAGE formula function currently.
|
|
||||||
func (f *File) GetPictureCells(sheet string) ([]string, error) {
|
func (f *File) GetPictureCells(sheet string) ([]string, error) {
|
||||||
f.mu.Lock()
|
f.mu.Lock()
|
||||||
ws, err := f.workSheetReader(sheet)
|
ws, err := f.workSheetReader(sheet)
|
||||||
|
@ -812,7 +811,7 @@ func (f *File) getImageCells(sheet string) ([]string, error) {
|
||||||
}
|
}
|
||||||
cells = append(cells, c.R)
|
cells = append(cells, c.R)
|
||||||
}
|
}
|
||||||
r, err := f.getImageCellRel(&c)
|
r, err := f.getImageCellRel(&c, &Picture{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cells, err
|
return cells, err
|
||||||
}
|
}
|
||||||
|
@ -825,30 +824,52 @@ func (f *File) getImageCells(sheet string) ([]string, error) {
|
||||||
return cells, err
|
return cells, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// getImageCellRichValueIdx returns index of the cell image rich value by given
|
// getRichDataRichValueRel returns relationship of the cell image by given meta
|
||||||
// cell value meta index and meta blocks.
|
// blocks value.
|
||||||
func (f *File) getImageCellRichValueIdx(vm uint, blocks *xlsxMetadataBlocks) (int, error) {
|
func (f *File) getRichDataRichValueRel(val string) (*xlsxRelationship, error) {
|
||||||
richValueIdx := blocks.Bk[vm-1].Rc[0].V
|
var r *xlsxRelationship
|
||||||
richValue, err := f.richValueReader()
|
idx, err := strconv.Atoi(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return r, err
|
||||||
}
|
}
|
||||||
if richValueIdx >= len(richValue.Rv) {
|
richValueRel, err := f.richValueRelReader()
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
rv := richValue.Rv[richValueIdx].V
|
|
||||||
if len(rv) != 2 || rv[1] != "5" {
|
|
||||||
return -1, err
|
|
||||||
}
|
|
||||||
richValueRelIdx, err := strconv.Atoi(rv[0])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, err
|
return r, err
|
||||||
}
|
}
|
||||||
return richValueRelIdx, err
|
if idx >= len(richValueRel.Rels) {
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
rID := richValueRel.Rels[idx].ID
|
||||||
|
if r = f.getRichDataRichValueRelRelationships(rID); r != nil && r.Type != SourceRelationshipImage {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// getRichDataWebImagesRel returns relationship of a web image by given meta
|
||||||
|
// blocks value.
|
||||||
|
func (f *File) getRichDataWebImagesRel(val string) (*xlsxRelationship, error) {
|
||||||
|
var r *xlsxRelationship
|
||||||
|
idx, err := strconv.Atoi(val)
|
||||||
|
if err != nil {
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
richValueWebImages, err := f.richValueWebImageReader()
|
||||||
|
if err != nil {
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
if idx >= len(richValueWebImages.WebImageSrd) {
|
||||||
|
return r, err
|
||||||
|
}
|
||||||
|
rID := richValueWebImages.WebImageSrd[idx].Blip.RID
|
||||||
|
if r = f.getRichValueWebImageRelationships(rID); r != nil && r.Type != SourceRelationshipImage {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return r, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// getImageCellRel returns the cell image relationship.
|
// getImageCellRel returns the cell image relationship.
|
||||||
func (f *File) getImageCellRel(c *xlsxC) (*xlsxRelationship, error) {
|
func (f *File) getImageCellRel(c *xlsxC, pic *Picture) (*xlsxRelationship, error) {
|
||||||
var r *xlsxRelationship
|
var r *xlsxRelationship
|
||||||
if c.Vm == nil || c.V != formulaErrorVALUE {
|
if c.Vm == nil || c.V != formulaErrorVALUE {
|
||||||
return r, nil
|
return r, nil
|
||||||
|
@ -861,20 +882,23 @@ func (f *File) getImageCellRel(c *xlsxC) (*xlsxRelationship, error) {
|
||||||
if vmd == nil || int(*c.Vm) > len(vmd.Bk) || len(vmd.Bk[*c.Vm-1].Rc) == 0 {
|
if vmd == nil || int(*c.Vm) > len(vmd.Bk) || len(vmd.Bk[*c.Vm-1].Rc) == 0 {
|
||||||
return r, err
|
return r, err
|
||||||
}
|
}
|
||||||
richValueRelIdx, err := f.getImageCellRichValueIdx(*c.Vm, vmd)
|
richValueIdx := vmd.Bk[*c.Vm-1].Rc[0].V
|
||||||
if err != nil || richValueRelIdx == -1 {
|
richValue, err := f.richValueReader()
|
||||||
return r, err
|
|
||||||
}
|
|
||||||
richValueRel, err := f.richValueRelReader()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return r, err
|
return r, err
|
||||||
}
|
}
|
||||||
if richValueRelIdx >= len(richValueRel.Rels) {
|
if richValueIdx >= len(richValue.Rv) {
|
||||||
return r, err
|
return r, err
|
||||||
}
|
}
|
||||||
rID := richValueRel.Rels[richValueRelIdx].ID
|
rv := richValue.Rv[richValueIdx].V
|
||||||
if r = f.getRichDataRichValueRelRelationships(rID); r != nil && r.Type != SourceRelationshipImage {
|
if len(rv) == 2 && rv[1] == "5" {
|
||||||
return nil, err
|
pic.InsertType = PictureInsertTypePlaceInCell
|
||||||
|
return f.getRichDataRichValueRel(rv[0])
|
||||||
|
}
|
||||||
|
// cell image inserted by IMAGE formula function
|
||||||
|
if len(rv) > 3 && rv[1]+rv[2] == "10" {
|
||||||
|
pic.InsertType = PictureInsertTypeIMAGE
|
||||||
|
return f.getRichDataWebImagesRel(rv[0])
|
||||||
}
|
}
|
||||||
return r, err
|
return r, err
|
||||||
}
|
}
|
||||||
|
@ -888,11 +912,12 @@ func (f *File) getCellImages(sheet, cell string) ([]Picture, error) {
|
||||||
return pics, err
|
return pics, err
|
||||||
}
|
}
|
||||||
_, err = f.getCellStringFunc(sheet, cell, func(x *xlsxWorksheet, c *xlsxC) (string, bool, error) {
|
_, err = f.getCellStringFunc(sheet, cell, func(x *xlsxWorksheet, c *xlsxC) (string, bool, error) {
|
||||||
r, err := f.getImageCellRel(c)
|
pic := Picture{Format: &GraphicOptions{}, InsertType: PictureInsertTypePlaceInCell}
|
||||||
|
r, err := f.getImageCellRel(c, &pic)
|
||||||
if err != nil || r == nil {
|
if err != nil || r == nil {
|
||||||
return "", true, err
|
return "", true, err
|
||||||
}
|
}
|
||||||
pic := Picture{Extension: filepath.Ext(r.Target), Format: &GraphicOptions{}, InsertType: PictureInsertTypePlaceInCell}
|
pic.Extension = filepath.Ext(r.Target)
|
||||||
if buffer, _ := f.Pkg.Load(strings.TrimPrefix(strings.ReplaceAll(r.Target, "..", "xl"), "/")); buffer != nil {
|
if buffer, _ := f.Pkg.Load(strings.TrimPrefix(strings.ReplaceAll(r.Target, "..", "xl"), "/")); buffer != nil {
|
||||||
pic.File = buffer.([]byte)
|
pic.File = buffer.([]byte)
|
||||||
pics = append(pics, pic)
|
pics = append(pics, pic)
|
||||||
|
|
|
@ -246,7 +246,7 @@ func TestGetPicture(t *testing.T) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.NoError(t, f.SetCellFormula("Sheet1", "F21", "=_xlfn.DISPIMG(\"ID_********************************\",1)"))
|
assert.NoError(t, f.SetCellFormula("Sheet1", "F21", "=_xlfn.DISPIMG(\"ID_********************************\",1)"))
|
||||||
f.Pkg.Store(defaultXMLPathCellImages, []byte(`<etc:cellImages xmlns:etc="http://www.wps.cn/officeDocument/2017/etCustomData"><etc:cellImage><xdr:pic><xdr:nvPicPr><xdr:cNvPr id="1" name="ID_********************************" descr="CellImage1"/></xdr:nvPicPr><xdr:blipFill><a:blip r:embed="rId1"/></xdr:blipFill></xdr:pic></etc:cellImage></etc:cellImages>`))
|
f.Pkg.Store(defaultXMLPathCellImages, []byte(`<etc:cellImages xmlns:etc="http://www.wps.cn/officeDocument/2017/etCustomData"><etc:cellImage><xdr:pic><xdr:nvPicPr><xdr:cNvPr id="1" name="ID_********************************" descr="CellImage1"/></xdr:nvPicPr><xdr:blipFill><a:blip r:embed="rId1"/></xdr:blipFill></xdr:pic></etc:cellImage></etc:cellImages>`))
|
||||||
f.Pkg.Store(defaultXMLPathCellImagesRels, []byte(`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.jpeg"/></Relationships>`))
|
f.Pkg.Store(defaultXMLPathCellImagesRels, []byte(fmt.Sprintf(`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="%s" Target="media/image1.jpeg"/></Relationships>`, SourceRelationshipImage)))
|
||||||
pics, err = f.GetPictures("Sheet1", "F21")
|
pics, err = f.GetPictures("Sheet1", "F21")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, pics, 2)
|
assert.Len(t, pics, 2)
|
||||||
|
@ -457,9 +457,9 @@ func TestGetCellImages(t *testing.T) {
|
||||||
f := NewFile()
|
f := NewFile()
|
||||||
assert.NoError(t, f.AddPicture("Sheet1", "A1", filepath.Join("test", "images", "excel.png"), nil))
|
assert.NoError(t, f.AddPicture("Sheet1", "A1", filepath.Join("test", "images", "excel.png"), nil))
|
||||||
f.Pkg.Store(defaultXMLMetadata, []byte(`<metadata><valueMetadata count="1"><bk><rc t="1" v="0"/></bk></valueMetadata></metadata>`))
|
f.Pkg.Store(defaultXMLMetadata, []byte(`<metadata><valueMetadata count="1"><bk><rc t="1" v="0"/></bk></valueMetadata></metadata>`))
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValue, []byte(`<rvData count="1"><rv s="0"><v>0</v><v>5</v></rv></rvData>`))
|
f.Pkg.Store(defaultXMLRdRichValuePart, []byte(`<rvData count="1"><rv s="0"><v>0</v><v>5</v></rv></rvData>`))
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValueRel, []byte(`<richValueRels><rel r:id="rId1"/></richValueRels>`))
|
f.Pkg.Store(defaultXMLRdRichValueRel, []byte(`<richValueRels><rel r:id="rId1"/></richValueRels>`))
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValueRelRels, []byte(fmt.Sprintf(`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="%s" Target="../media/image1.png"/></Relationships>`, SourceRelationshipImage)))
|
f.Pkg.Store(defaultXMLRdRichValueRelRels, []byte(fmt.Sprintf(`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="%s" Target="../media/image1.png"/></Relationships>`, SourceRelationshipImage)))
|
||||||
f.Sheet.Store("xl/worksheets/sheet1.xml", &xlsxWorksheet{
|
f.Sheet.Store("xl/worksheets/sheet1.xml", &xlsxWorksheet{
|
||||||
SheetData: xlsxSheetData{Row: []xlsxRow{
|
SheetData: xlsxSheetData{Row: []xlsxRow{
|
||||||
{R: 1, C: []xlsxC{{R: "A1", T: "e", V: formulaErrorVALUE, Vm: uintPtr(1)}}},
|
{R: 1, C: []xlsxC{{R: "A1", T: "e", V: formulaErrorVALUE, Vm: uintPtr(1)}}},
|
||||||
|
@ -477,19 +477,19 @@ func TestGetCellImages(t *testing.T) {
|
||||||
assert.Equal(t, []string{"A1"}, cells)
|
assert.Equal(t, []string{"A1"}, cells)
|
||||||
|
|
||||||
// Test get the cell images without image relationships parts
|
// Test get the cell images without image relationships parts
|
||||||
f.Relationships.Delete(defaultXMLRichDataRichValueRelRels)
|
f.Relationships.Delete(defaultXMLRdRichValueRelRels)
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValueRelRels, []byte(fmt.Sprintf(`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="%s" Target="../media/image1.png"/></Relationships>`, SourceRelationshipHyperLink)))
|
f.Pkg.Store(defaultXMLRdRichValueRelRels, []byte(fmt.Sprintf(`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="%s" Target="../media/image1.png"/></Relationships>`, SourceRelationshipHyperLink)))
|
||||||
pics, err = f.GetPictures("Sheet1", "A1")
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Empty(t, pics)
|
assert.Empty(t, pics)
|
||||||
// Test get the cell images with unsupported charset rich data rich value relationships
|
// Test get the cell images with unsupported charset rich data rich value relationships
|
||||||
f.Relationships.Delete(defaultXMLRichDataRichValueRelRels)
|
f.Relationships.Delete(defaultXMLRdRichValueRelRels)
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValueRelRels, MacintoshCyrillicCharset)
|
f.Pkg.Store(defaultXMLRdRichValueRelRels, MacintoshCyrillicCharset)
|
||||||
pics, err = f.GetPictures("Sheet1", "A1")
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Empty(t, pics)
|
assert.Empty(t, pics)
|
||||||
// Test get the cell images with unsupported charset rich data rich value
|
// Test get the cell images with unsupported charset rich data rich value
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValueRel, MacintoshCyrillicCharset)
|
f.Pkg.Store(defaultXMLRdRichValueRel, MacintoshCyrillicCharset)
|
||||||
_, err = f.GetPictures("Sheet1", "A1")
|
_, err = f.GetPictures("Sheet1", "A1")
|
||||||
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
||||||
// Test get the image cells without block of metadata records
|
// Test get the image cells without block of metadata records
|
||||||
|
@ -498,7 +498,7 @@ func TestGetCellImages(t *testing.T) {
|
||||||
assert.Empty(t, cells)
|
assert.Empty(t, cells)
|
||||||
// Test get the cell images with rich data rich value relationships
|
// Test get the cell images with rich data rich value relationships
|
||||||
f.Pkg.Store(defaultXMLMetadata, []byte(`<metadata><valueMetadata count="1"><bk><rc t="1" v="0"/></bk></valueMetadata></metadata>`))
|
f.Pkg.Store(defaultXMLMetadata, []byte(`<metadata><valueMetadata count="1"><bk><rc t="1" v="0"/></bk></valueMetadata></metadata>`))
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValueRel, []byte(`<richValueRels/>`))
|
f.Pkg.Store(defaultXMLRdRichValueRel, []byte(`<richValueRels/>`))
|
||||||
pics, err = f.GetPictures("Sheet1", "A1")
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Empty(t, pics)
|
assert.Empty(t, pics)
|
||||||
|
@ -514,17 +514,17 @@ func TestGetCellImages(t *testing.T) {
|
||||||
|
|
||||||
f = prepareWorkbook()
|
f = prepareWorkbook()
|
||||||
// Test get the cell images with empty image cell rich value
|
// Test get the cell images with empty image cell rich value
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValue, []byte(`<rvData count="1"><rv s="0"><v></v><v>5</v></rv></rvData>`))
|
f.Pkg.Store(defaultXMLRdRichValuePart, []byte(`<rvData count="1"><rv s="0"><v></v><v>5</v></rv></rvData>`))
|
||||||
pics, err = f.GetPictures("Sheet1", "A1")
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
assert.EqualError(t, err, "strconv.Atoi: parsing \"\": invalid syntax")
|
assert.EqualError(t, err, "strconv.Atoi: parsing \"\": invalid syntax")
|
||||||
assert.Empty(t, pics)
|
assert.Empty(t, pics)
|
||||||
// Test get the cell images without image cell rich value
|
// Test get the cell images without image cell rich value
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValue, []byte(`<rvData count="1"><rv s="0"><v>0</v><v>1</v></rv></rvData>`))
|
f.Pkg.Store(defaultXMLRdRichValuePart, []byte(`<rvData count="1"><rv s="0"><v>0</v><v>1</v></rv></rvData>`))
|
||||||
pics, err = f.GetPictures("Sheet1", "A1")
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Empty(t, pics)
|
assert.Empty(t, pics)
|
||||||
// Test get the cell images with unsupported charset rich value
|
// Test get the cell images with unsupported charset rich value
|
||||||
f.Pkg.Store(defaultXMLRichDataRichValue, MacintoshCyrillicCharset)
|
f.Pkg.Store(defaultXMLRdRichValuePart, MacintoshCyrillicCharset)
|
||||||
_, err = f.GetPictures("Sheet1", "A1")
|
_, err = f.GetPictures("Sheet1", "A1")
|
||||||
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
||||||
|
|
||||||
|
@ -534,6 +534,44 @@ func TestGetCellImages(t *testing.T) {
|
||||||
pics, err = f.GetPictures("Sheet1", "A1")
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Empty(t, pics)
|
assert.Empty(t, pics)
|
||||||
|
|
||||||
|
f = prepareWorkbook()
|
||||||
|
// Test get the cell images inserted by IMAGE formula function
|
||||||
|
f.Pkg.Store(defaultXMLRdRichValuePart, []byte(`<rvData count="1"><rv s="1"><v>0</v><v>1</v><v>0</v><v>0</v></rv></rvData>`))
|
||||||
|
f.Pkg.Store(defaultXMLRdRichValueWebImagePart, []byte(`<webImagesSrd xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><webImageSrd><address r:id="rId1"/><blip r:id="rId2"/></webImageSrd>
|
||||||
|
</webImagesSrd>`))
|
||||||
|
f.Pkg.Store(defaultXMLRdRichValueWebImagePartRels, []byte(fmt.Sprintf(`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="%s" Target="https://github.com/xuri/excelize" TargetMode="External"/><Relationship Id="rId2" Type="%s" Target="../media/image1.png"/></Relationships>`, SourceRelationshipHyperLink, SourceRelationshipImage)))
|
||||||
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, len(pics))
|
||||||
|
assert.Equal(t, PictureInsertTypeIMAGE, pics[0].InsertType)
|
||||||
|
|
||||||
|
// Test get the cell images inserted by IMAGE formula function with unsupported charset web images relationships
|
||||||
|
f.Relationships.Delete(defaultXMLRdRichValueWebImagePartRels)
|
||||||
|
f.Pkg.Store(defaultXMLRdRichValueWebImagePartRels, MacintoshCyrillicCharset)
|
||||||
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Empty(t, pics)
|
||||||
|
|
||||||
|
// Test get the cell images inserted by IMAGE formula function without image part
|
||||||
|
f.Relationships.Delete(defaultXMLRdRichValueWebImagePartRels)
|
||||||
|
f.Pkg.Store(defaultXMLRdRichValueWebImagePartRels, []byte(fmt.Sprintf(`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="%s" Target="https://github.com/xuri/excelize" TargetMode="External"/><Relationship Id="rId2" Type="%s" Target="../media/image1.png"/></Relationships>`, SourceRelationshipHyperLink, SourceRelationshipHyperLink)))
|
||||||
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Empty(t, pics)
|
||||||
|
// Test get the cell images inserted by IMAGE formula function with unsupported charset web images part
|
||||||
|
f.Pkg.Store(defaultXMLRdRichValueWebImagePart, MacintoshCyrillicCharset)
|
||||||
|
_, err = f.GetPictures("Sheet1", "A1")
|
||||||
|
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
|
||||||
|
// Test get the cell images inserted by IMAGE formula function with empty charset web images part
|
||||||
|
f.Pkg.Store(defaultXMLRdRichValueWebImagePart, []byte(`<webImagesSrd xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" />`))
|
||||||
|
pics, err = f.GetPictures("Sheet1", "A1")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Empty(t, pics)
|
||||||
|
// Test get the cell images inserted by IMAGE formula function with invalid rich value index
|
||||||
|
f.Pkg.Store(defaultXMLRdRichValuePart, []byte(`<rvData count="1"><rv s="1"><v></v><v>1</v><v>0</v><v>0</v></rv></rvData>`))
|
||||||
|
_, err = f.GetPictures("Sheet1", "A1")
|
||||||
|
assert.EqualError(t, err, "strconv.Atoi: parsing \"\": invalid syntax")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetImageCells(t *testing.T) {
|
func TestGetImageCells(t *testing.T) {
|
||||||
|
|
36
templates.go
36
templates.go
|
@ -266,23 +266,25 @@ var supportedChartDataLabelsPosition = map[ChartType][]ChartDataLabelPositionTyp
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultTempFileSST = "sharedStrings"
|
defaultTempFileSST = "sharedStrings"
|
||||||
defaultXMLMetadata = "xl/metadata.xml"
|
defaultXMLMetadata = "xl/metadata.xml"
|
||||||
defaultXMLPathCalcChain = "xl/calcChain.xml"
|
defaultXMLPathCalcChain = "xl/calcChain.xml"
|
||||||
defaultXMLPathCellImages = "xl/cellimages.xml"
|
defaultXMLPathCellImages = "xl/cellimages.xml"
|
||||||
defaultXMLPathCellImagesRels = "xl/_rels/cellimages.xml.rels"
|
defaultXMLPathCellImagesRels = "xl/_rels/cellimages.xml.rels"
|
||||||
defaultXMLPathContentTypes = "[Content_Types].xml"
|
defaultXMLPathContentTypes = "[Content_Types].xml"
|
||||||
defaultXMLPathDocPropsApp = "docProps/app.xml"
|
defaultXMLPathDocPropsApp = "docProps/app.xml"
|
||||||
defaultXMLPathDocPropsCore = "docProps/core.xml"
|
defaultXMLPathDocPropsCore = "docProps/core.xml"
|
||||||
defaultXMLPathSharedStrings = "xl/sharedStrings.xml"
|
defaultXMLPathSharedStrings = "xl/sharedStrings.xml"
|
||||||
defaultXMLPathStyles = "xl/styles.xml"
|
defaultXMLPathStyles = "xl/styles.xml"
|
||||||
defaultXMLPathTheme = "xl/theme/theme1.xml"
|
defaultXMLPathTheme = "xl/theme/theme1.xml"
|
||||||
defaultXMLPathVolatileDeps = "xl/volatileDependencies.xml"
|
defaultXMLPathVolatileDeps = "xl/volatileDependencies.xml"
|
||||||
defaultXMLPathWorkbook = "xl/workbook.xml"
|
defaultXMLPathWorkbook = "xl/workbook.xml"
|
||||||
defaultXMLPathWorkbookRels = "xl/_rels/workbook.xml.rels"
|
defaultXMLPathWorkbookRels = "xl/_rels/workbook.xml.rels"
|
||||||
defaultXMLRichDataRichValue = "xl/richData/rdrichvalue.xml"
|
defaultXMLRdRichValuePart = "xl/richData/rdrichvalue.xml"
|
||||||
defaultXMLRichDataRichValueRel = "xl/richData/richValueRel.xml"
|
defaultXMLRdRichValueRel = "xl/richData/richValueRel.xml"
|
||||||
defaultXMLRichDataRichValueRelRels = "xl/richData/_rels/richValueRel.xml.rels"
|
defaultXMLRdRichValueRelRels = "xl/richData/_rels/richValueRel.xml.rels"
|
||||||
|
defaultXMLRdRichValueWebImagePart = "xl/richData/rdRichValueWebImage.xml"
|
||||||
|
defaultXMLRdRichValueWebImagePartRels = "xl/richData/_rels/rdRichValueWebImage.xml.rels"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IndexedColorMapping is the table of default mappings from indexed color value
|
// IndexedColorMapping is the table of default mappings from indexed color value
|
||||||
|
|
|
@ -98,3 +98,20 @@ type xlsxRichValueRels struct {
|
||||||
type xlsxRichValueRelRelationship struct {
|
type xlsxRichValueRelRelationship struct {
|
||||||
ID string `xml:"id,attr"`
|
ID string `xml:"id,attr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// xlsxWebImagesSupportingRichData directly maps the webImagesSrd element. This
|
||||||
|
// element specifies a list of sets of properties associated with web image rich
|
||||||
|
// values.
|
||||||
|
type xlsxWebImagesSupportingRichData struct {
|
||||||
|
XMLName xml.Name `xml:"webImagesSrd"`
|
||||||
|
WebImageSrd []xlsxWebImageSupportingRichData `xml:"webImageSrd"`
|
||||||
|
ExtLst *xlsxInnerXML `xml:"extLst"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// xlsxWebImageSupportingRichData directly maps the webImageSrd element. This
|
||||||
|
// element specifies a set of properties for a web image rich value.
|
||||||
|
type xlsxWebImageSupportingRichData struct {
|
||||||
|
Address xlsxExternalReference `xml:"address"`
|
||||||
|
MoreImagesAddress xlsxExternalReference `xml:"moreImagesAddress"`
|
||||||
|
Blip xlsxExternalReference `xml:"blip"`
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue