@@ -296,6 +296,41 @@ func addRelationshipNameSpaceToWorksheet(worksheetMarshal string) string {
296296 return newSheetMarshall
297297}
298298
299+ func cellIDStringWithFixed (cellIDString string ) string {
300+ letterPart := strings .Map (letterOnlyMapF , cellIDString )
301+ intPart := strings .Map (intOnlyMapF , cellIDString )
302+
303+ if letterPart != "" && intPart == "" {
304+ return fixedCellRefChar + letterPart
305+ } else if letterPart != "" && intPart != "" {
306+ return fixedCellRefChar + letterPart + fixedCellRefChar + intPart
307+ }
308+
309+ return ""
310+ }
311+
312+ // AutoFilter doesn't work in LibreOffice unless a special "FilterDatabase" tag
313+ // is present in the "DefinedNames" array. See:
314+ // - https://github.com/SheetJS/sheetjs/issues/1165
315+ // - https://bugs.documentfoundation.org/show_bug.cgi?id=118592
316+ func autoFilterDefinedName (sheet * Sheet , sheetIndex int ) (* xlsxDefinedName , error ) {
317+ if sheet .AutoFilter == nil {
318+ return nil , nil
319+ }
320+
321+ return & xlsxDefinedName {
322+ Data : fmt .Sprintf (
323+ "'%s'!%v:%v" ,
324+ strings .ReplaceAll (sheet .Name , "'" , "''" ),
325+ cellIDStringWithFixed (sheet .AutoFilter .TopLeftCell ),
326+ cellIDStringWithFixed (sheet .AutoFilter .BottomRightCell ),
327+ ),
328+ Name : "_xlnm._FilterDatabase" ,
329+ LocalSheetID : sheetIndex - 1 ,
330+ Hidden : true ,
331+ }, nil
332+ }
333+
299334// MakeStreamParts constructs a map of file name to XML content
300335// representing the file in terms of the structure of an XLSX file.
301336func (f * File ) MakeStreamParts () (map [string ]string , error ) {
@@ -370,6 +405,14 @@ func (f *File) MakeStreamParts() (map[string]string, error) {
370405 return parts , err
371406 }
372407 }
408+
409+ definedName , err := autoFilterDefinedName (sheet , sheetIndex )
410+ if err != nil {
411+ return parts , err
412+ } else if definedName != nil {
413+ workbook .DefinedNames .DefinedName = append (workbook .DefinedNames .DefinedName , * definedName )
414+ }
415+
373416 sheetIndex ++
374417 }
375418
@@ -511,6 +554,14 @@ func (f *File) MarshallParts(zipWriter *zip.Writer) error {
511554 return wrap (err )
512555 }
513556 }
557+
558+ definedName , err := autoFilterDefinedName (sheet , sheetIndex )
559+ if err != nil {
560+ return wrap (err )
561+ } else if definedName != nil {
562+ workbook .DefinedNames .DefinedName = append (workbook .DefinedNames .DefinedName , * definedName )
563+ }
564+
514565 sheetIndex ++
515566 }
516567
0 commit comments