Add support for insert BMP format images

- Add support for workbook function groups
- Update code and docs for the build-in currency number format
- Update unit tests
This commit is contained in:
xuri 2023-02-05 00:21:23 +08:00
parent 12645e7116
commit 1f69f6b24a
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
8 changed files with 54 additions and 37 deletions

View File

@ -51,8 +51,8 @@ func parseGraphicOptions(opts *GraphicOptions) *GraphicOptions {
// AddPicture provides the method to add picture in a sheet by given picture // AddPicture provides the method to add picture in a sheet by given picture
// format set (such as offset, scale, aspect ratio setting and print settings) // format set (such as offset, scale, aspect ratio setting and print settings)
// and file path, supported image types: EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, // and file path, supported image types: BMP, EMF, EMZ, GIF, JPEG, JPG, PNG,
// TIF, TIFF, WMF, and WMZ. This function is concurrency safe. For example: // SVG, TIF, TIFF, WMF, and WMZ. This function is concurrency safe. For example:
// //
// package main // package main
// //
@ -436,8 +436,9 @@ func (f *File) addMedia(file []byte, ext string) string {
// type for relationship parts and the Main Document part. // type for relationship parts and the Main Document part.
func (f *File) setContentTypePartImageExtensions() error { func (f *File) setContentTypePartImageExtensions() error {
imageTypes := map[string]string{ imageTypes := map[string]string{
"jpeg": "image/", "png": "image/", "gif": "image/", "svg": "image/", "tiff": "image/", "bmp": "image/", "jpeg": "image/", "png": "image/", "gif": "image/",
"emf": "image/x-", "wmf": "image/x-", "emz": "image/x-", "wmz": "image/x-", "svg": "image/", "tiff": "image/", "emf": "image/x-", "wmf": "image/x-",
"emz": "image/x-", "wmz": "image/x-",
} }
content, err := f.contentTypesReader() content, err := f.contentTypesReader()
if err != nil { if err != nil {

View File

@ -12,6 +12,7 @@ import (
"strings" "strings"
"testing" "testing"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff" _ "golang.org/x/image/tiff"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -64,6 +65,7 @@ func TestAddPicture(t *testing.T) {
assert.NoError(t, f.AddPicture("Sheet1", "Q8", filepath.Join("test", "images", "excel.gif"), nil)) assert.NoError(t, f.AddPicture("Sheet1", "Q8", filepath.Join("test", "images", "excel.gif"), nil))
assert.NoError(t, f.AddPicture("Sheet1", "Q15", filepath.Join("test", "images", "excel.jpg"), nil)) assert.NoError(t, f.AddPicture("Sheet1", "Q15", filepath.Join("test", "images", "excel.jpg"), nil))
assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), nil)) assert.NoError(t, f.AddPicture("Sheet1", "Q22", filepath.Join("test", "images", "excel.tif"), nil))
assert.NoError(t, f.AddPicture("Sheet1", "Q28", filepath.Join("test", "images", "excel.bmp"), nil))
// Test write file to given path // Test write file to given path
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture1.xlsx"))) assert.NoError(t, f.SaveAs(filepath.Join("test", "TestAddPicture1.xlsx")))

View File

@ -500,8 +500,8 @@ func (f *File) getSheetXMLPath(sheet string) (string, bool) {
} }
// SetSheetBackground provides a function to set background picture by given // SetSheetBackground provides a function to set background picture by given
// worksheet name and file path. Supported image types: EMF, EMZ, GIF, JPEG, // worksheet name and file path. Supported image types: BMP, EMF, EMZ, GIF,
// JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ. // JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
func (f *File) SetSheetBackground(sheet, picture string) error { func (f *File) SetSheetBackground(sheet, picture string) error {
var err error var err error
// Check picture exists first. // Check picture exists first.
@ -514,7 +514,7 @@ func (f *File) SetSheetBackground(sheet, picture string) error {
// SetSheetBackgroundFromBytes provides a function to set background picture by // SetSheetBackgroundFromBytes provides a function to set background picture by
// given worksheet name, extension name and image data. Supported image types: // given worksheet name, extension name and image data. Supported image types:
// EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ. // BMP, EMF, EMZ, GIF, JPEG, JPG, PNG, SVG, TIF, TIFF, WMF, and WMZ.
func (f *File) SetSheetBackgroundFromBytes(sheet, extension string, picture []byte) error { func (f *File) SetSheetBackgroundFromBytes(sheet, extension string, picture []byte) error {
if len(picture) == 0 { if len(picture) == 0 {
return ErrParameterInvalid return ErrParameterInvalid

View File

@ -596,7 +596,10 @@ func TestAttrValToFloat(t *testing.T) {
func TestSetSheetBackgroundFromBytes(t *testing.T) { func TestSetSheetBackgroundFromBytes(t *testing.T) {
f := NewFile() f := NewFile()
assert.NoError(t, f.SetSheetName("Sheet1", ".svg")) assert.NoError(t, f.SetSheetName("Sheet1", ".svg"))
for i, imageTypes := range []string{".svg", ".emf", ".emz", ".gif", ".jpg", ".png", ".tif", ".wmf", ".wmz"} { for i, imageTypes := range []string{
".svg", ".bmp", ".emf", ".emz", ".gif",
".jpg", ".png", ".tif", ".wmf", ".wmz",
} {
file := fmt.Sprintf("excelize%s", imageTypes) file := fmt.Sprintf("excelize%s", imageTypes)
if i > 0 { if i > 0 {
file = filepath.Join("test", "images", fmt.Sprintf("excel%s", imageTypes)) file = filepath.Join("test", "images", fmt.Sprintf("excel%s", imageTypes))

View File

@ -277,7 +277,7 @@ var langNumFmt = map[string]map[int]string{
// currencyNumFmt defined the currency number format map. // currencyNumFmt defined the currency number format map.
var currencyNumFmt = map[int]string{ var currencyNumFmt = map[int]string{
164: `"CN¥",##0.00`, 164: `"¥"#,##0.00`,
165: "[$$-409]#,##0.00", 165: "[$$-409]#,##0.00",
166: "[$$-45C]#,##0.00", 166: "[$$-45C]#,##0.00",
167: "[$$-1004]#,##0.00", 167: "[$$-1004]#,##0.00",
@ -1491,8 +1491,8 @@ func parseFormatStyleSet(style *Style) (*Style, error) {
// //
// Index | Symbol // Index | Symbol
// -------+--------------------------------------------------------------- // -------+---------------------------------------------------------------
// 164 | CN¥ // 164 | ¥
// 165 | $ English (China) // 165 | $ English (United States)
// 166 | $ Cherokee (United States) // 166 | $ Cherokee (United States)
// 167 | $ Chinese (Singapore) // 167 | $ Chinese (Singapore)
// 168 | $ Chinese (Taiwan) // 168 | $ Chinese (Taiwan)
@ -1533,28 +1533,28 @@ func parseFormatStyleSet(style *Style) (*Style, error) {
// 203 | ₡ Spanish (Costa Rica) // 203 | ₡ Spanish (Costa Rica)
// 204 | ₦ Hausa (Nigeria) // 204 | ₦ Hausa (Nigeria)
// 205 | ₦ Igbo (Nigeria) // 205 | ₦ Igbo (Nigeria)
// 206 | ₦ Yoruba (Nigeria) // 206 | ₩ Korean (South Korea)
// 207 | ₩ Korean (South Korea) // 207 | ₪ Hebrew (Israel)
// 208 | ₪ Hebrew (Israel) // 208 | ₫ Vietnamese (Vietnam)
// 209 | ₫ Vietnamese (Vietnam) // 209 | € Basque (Spain)
// 210 | € Basque (Spain) // 210 | € Breton (France)
// 211 | € Breton (France) // 211 | € Catalan (Spain)
// 212 | € Catalan (Spain) // 212 | € Corsican (France)
// 213 | € Corsican (France) // 213 | € Dutch (Belgium)
// 214 | € Dutch (Belgium) // 214 | € Dutch (Netherlands)
// 215 | € Dutch (Netherlands) // 215 | € English (Ireland)
// 216 | € English (Ireland) // 216 | € Estonian (Estonia)
// 217 | € Estonian (Estonia) // 217 | € Euro (€ 123)
// 218 | € Euro (123) // 218 | € Euro (123)
// 219 | € Euro (123 €) // 219 | € Finnish (Finland)
// 220 | € Finnish (Finland) // 220 | € French (Belgium)
// 221 | € French (Belgium) // 221 | € French (France)
// 222 | € French (France) // 222 | € French (Luxembourg)
// 223 | € French (Luxembourg) // 223 | € French (Monaco)
// 224 | € French (Monaco) // 224 | € French (Réunion)
// 225 | € French (Réunion) // 225 | € Galician (Spain)
// 226 | € Galician (Spain) // 226 | € German (Austria)
// 227 | € German (Austria) // 227 | € German (German)
// 228 | € German (Luxembourg) // 228 | € German (Luxembourg)
// 229 | € Greek (Greece) // 229 | € Greek (Greece)
// 230 | € Inari Sami (Finland) // 230 | € Inari Sami (Finland)

BIN
test/images/excel.bmp Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View File

@ -181,9 +181,9 @@ var IndexedColorMapping = []string{
// supportedImageTypes defined supported image types. // supportedImageTypes defined supported image types.
var supportedImageTypes = map[string]string{ var supportedImageTypes = map[string]string{
".emf": ".emf", ".emz": ".emz", ".gif": ".gif", ".jpeg": ".jpeg", ".bmp": ".bmp", ".emf": ".emf", ".emz": ".emz", ".gif": ".gif",
".jpg": ".jpeg", ".png": ".png", ".svg": ".svg", ".tif": ".tiff", ".jpeg": ".jpeg", ".jpg": ".jpeg", ".png": ".png", ".svg": ".svg",
".tiff": ".tiff", ".wmf": ".wmf", ".wmz": ".wmz", ".tif": ".tiff", ".tiff": ".tiff", ".wmf": ".wmf", ".wmz": ".wmz",
} }
// supportedContentTypes defined supported file format types. // supportedContentTypes defined supported file format types.

View File

@ -45,7 +45,7 @@ type xlsxWorkbook struct {
WorkbookProtection *xlsxWorkbookProtection `xml:"workbookProtection"` WorkbookProtection *xlsxWorkbookProtection `xml:"workbookProtection"`
BookViews *xlsxBookViews `xml:"bookViews"` BookViews *xlsxBookViews `xml:"bookViews"`
Sheets xlsxSheets `xml:"sheets"` Sheets xlsxSheets `xml:"sheets"`
FunctionGroups *xlsxExtLst `xml:"functionGroups"` FunctionGroups *xlsxFunctionGroups `xml:"functionGroups"`
ExternalReferences *xlsxExternalReferences `xml:"externalReferences"` ExternalReferences *xlsxExternalReferences `xml:"externalReferences"`
DefinedNames *xlsxDefinedNames `xml:"definedNames"` DefinedNames *xlsxDefinedNames `xml:"definedNames"`
CalcPr *xlsxCalcPr `xml:"calcPr"` CalcPr *xlsxCalcPr `xml:"calcPr"`
@ -171,6 +171,17 @@ type xlsxSheet struct {
State string `xml:"state,attr,omitempty"` State string `xml:"state,attr,omitempty"`
} }
// xlsxFunctionGroup represents a single function group.
type xlsxFunctionGroup struct {
Name string `xml:"name,attr"`
}
// xlsxFunctionGroups defines the collection of function groups for the workbook.
type xlsxFunctionGroups struct {
BuiltInGroupCount *int `xml:"builtInGroupCount,attr"`
FunctionGroup []xlsxFunctionGroup `xml:"functionGroup"`
}
// xlsxExternalReferences directly maps the externalReferences element of the // xlsxExternalReferences directly maps the externalReferences element of the
// external workbook references part. // external workbook references part.
type xlsxExternalReferences struct { type xlsxExternalReferences struct {