-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Implement support for Excel EMBED function and object file embedding #2189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ package excelize | |||||||||||
| import ( | ||||||||||||
| "bytes" | ||||||||||||
| "encoding/xml" | ||||||||||||
| "fmt" | ||||||||||||
| "image" | ||||||||||||
| "io" | ||||||||||||
| "os" | ||||||||||||
|
|
@@ -1018,3 +1019,155 @@ func (f *File) getDispImages(sheet, cell string) ([]Picture, error) { | |||||||||||
| } | ||||||||||||
| return pics, err | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // EmbeddedObjectOptions defines the format set of embedded object. | ||||||||||||
| type EmbeddedObjectOptions struct { | ||||||||||||
| AltText string | ||||||||||||
| PrintObject *bool | ||||||||||||
| Locked *bool | ||||||||||||
| ObjectType string // Default is "Package" | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // AddEmbeddedObject provides a method to embed a file as an object in a cell. | ||||||||||||
| // The embedded object will be accessible through Excel's EMBED formula. | ||||||||||||
| // Supported object types include "Package" for general files. For example: | ||||||||||||
| // | ||||||||||||
| // package main | ||||||||||||
| // | ||||||||||||
| // import ( | ||||||||||||
| // "fmt" | ||||||||||||
| // "os" | ||||||||||||
| // | ||||||||||||
| // "github.com/xuri/excelize/v2" | ||||||||||||
| // ) | ||||||||||||
| // | ||||||||||||
| // func main() { | ||||||||||||
| // f := excelize.NewFile() | ||||||||||||
| // defer func() { | ||||||||||||
| // if err := f.Close(); err != nil { | ||||||||||||
| // fmt.Println(err) | ||||||||||||
| // } | ||||||||||||
| // }() | ||||||||||||
| // | ||||||||||||
| // // Read file to embed | ||||||||||||
| // file, err := os.ReadFile("document.pdf") | ||||||||||||
| // if err != nil { | ||||||||||||
| // fmt.Println(err) | ||||||||||||
| // return | ||||||||||||
| // } | ||||||||||||
| // | ||||||||||||
| // // Add embedded object | ||||||||||||
| // if err := f.AddEmbeddedObject("Sheet1", "A1", "document.pdf", file, | ||||||||||||
| // &excelize.EmbeddedObjectOptions{ | ||||||||||||
| // ObjectType: "Package", | ||||||||||||
| // AltText: "Embedded PDF Document", | ||||||||||||
| // }); err != nil { | ||||||||||||
| // fmt.Println(err) | ||||||||||||
| // return | ||||||||||||
| // } | ||||||||||||
| // | ||||||||||||
| // if err := f.SaveAs("Book1.xlsx"); err != nil { | ||||||||||||
| // fmt.Println(err) | ||||||||||||
| // } | ||||||||||||
| // } | ||||||||||||
| func (f *File) AddEmbeddedObject(sheet, cell, filename string, file []byte, opts *EmbeddedObjectOptions) error { | ||||||||||||
| if opts == nil { | ||||||||||||
| opts = &EmbeddedObjectOptions{ | ||||||||||||
| ObjectType: "Package", | ||||||||||||
| PrintObject: boolPtr(true), | ||||||||||||
| Locked: boolPtr(true), | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| if opts.ObjectType == "" { | ||||||||||||
| opts.ObjectType = "Package" | ||||||||||||
| } | ||||||||||||
| if opts.PrintObject == nil { | ||||||||||||
| opts.PrintObject = boolPtr(true) | ||||||||||||
| } | ||||||||||||
| if opts.Locked == nil { | ||||||||||||
| opts.Locked = boolPtr(true) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Add embedded object to package | ||||||||||||
| objPath := f.addEmbeddedObject(file, filename) | ||||||||||||
|
|
||||||||||||
| // Add relationships | ||||||||||||
| sheetXMLPath, _ := f.getSheetXMLPath(sheet) | ||||||||||||
| sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetXMLPath, "xl/worksheets/") + ".rels" | ||||||||||||
| rID := f.addRels(sheetRels, SourceRelationshipOLEObject, "../"+objPath, "") | ||||||||||||
|
|
||||||||||||
| // Set EMBED formula in cell | ||||||||||||
| formula := fmt.Sprintf("EMBED(\"%s\",\"\")", opts.ObjectType) | ||||||||||||
| if err := f.SetCellFormula(sheet, cell, formula); err != nil { | ||||||||||||
| return err | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Add OLE object to worksheet | ||||||||||||
| ws, err := f.workSheetReader(sheet) | ||||||||||||
| if err != nil { | ||||||||||||
| return err | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| if ws.OleObjects == nil { | ||||||||||||
| ws.OleObjects = &xlsxInnerXML{} | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Create OLE object XML content | ||||||||||||
| oleObjectXML := fmt.Sprintf(`<oleObject progId="Package" dvAspect="DVASPECT_ICON" link="false" oleUpdate="OLEUPDATE_ONCALL" autoLoad="false" shapeId="1025" r:id="rId%d"/>`, rID) | ||||||||||||
|
||||||||||||
| oleObjectXML := fmt.Sprintf(`<oleObject progId="Package" dvAspect="DVASPECT_ICON" link="false" oleUpdate="OLEUPDATE_ONCALL" autoLoad="false" shapeId="1025" r:id="rId%d"/>`, rID) | |
| oleObjectXML := fmt.Sprintf(`<oleObject progId="%s" dvAspect="DVASPECT_ICON" link="false" oleUpdate="OLEUPDATE_ONCALL" autoLoad="false" shapeId="1025" r:id="rId%d"/>`, opts.ObjectType, rID) |
Copilot
AI
Jul 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The filename parameter is not used in the function implementation. Either remove it or use it for better object identification in the generated path.
| objPath := "embeddings/oleObject" + strconv.Itoa(count+1) + ".bin" | |
| // Sanitize filename to remove invalid characters and extensions | |
| baseName := strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)) | |
| baseName = strings.ReplaceAll(baseName, " ", "_") // Replace spaces with underscores | |
| objPath := "embeddings/" + baseName + "_" + strconv.Itoa(count+1) + ".bin" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing input validation for sheet and cell parameters. The function should validate these inputs before proceeding, similar to other cell functions in the codebase.