| 1 | package xlsx |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "github.com/xuri/excelize/v2" |
| 7 | ) |
| 8 | |
| 9 | // reportOmissions counts what the first worksheet and the workbook |
| 10 | // hold that import does not carry. Counts, never content: a comment's |
| 11 | // text, a validation's list and a defined name's formula stay where |
| 12 | // they are. |
| 13 | func reportOmissions(f *excelize.File, sheet string, scan *sheetScan, rep *Report) { |
| 14 | undetected := 0 |
| 15 | count := func(code Code, n int, err error) { |
| 16 | if err != nil { |
| 17 | undetected++ |
| 18 | return |
| 19 | } |
| 20 | rep.add(code, n) |
| 21 | } |
| 22 | |
| 23 | merges, err := f.GetMergeCells(sheet, true) |
| 24 | count(OmittedMerges, len(merges), err) |
| 25 | comments, err := f.GetComments(sheet) |
| 26 | count(OmittedComments, len(comments), err) |
| 27 | validations, err := f.GetDataValidations(sheet) |
| 28 | count(OmittedValidation, len(validations), err) |
| 29 | conditional, err := f.GetConditionalFormats(sheet) |
| 30 | count(OmittedConditional, len(conditional), err) |
| 31 | pictures, err := f.GetPictureCells(sheet) |
| 32 | count(OmittedPictures, len(pictures), err) |
| 33 | rep.add(OmittedDefinedNames, definedNames(f.GetDefinedName())) |
| 34 | |
| 35 | rep.add(OmittedHyperlinks, scan.Hyperlinks) |
| 36 | rep.add(OmittedHiddenRows, scan.HiddenRows) |
| 37 | rep.add(OmittedHiddenCols, scan.HiddenCols) |
| 38 | if scan.FrozenPane { |
| 39 | rep.add(OmittedFrozenPanes, 1) |
| 40 | } |
| 41 | if scan.AutoFilter { |
| 42 | rep.add(OmittedFilter, 1) |
| 43 | } |
| 44 | rep.add(OmittedWorkbookChart, scan.Charts) |
| 45 | rep.add(OmittedWorkbookImage, scan.Images) |
| 46 | rep.add(OmittedUndetected, undetected) |
| 47 | } |
| 48 | |
| 49 | // partialStyle reports whether a cell's style uses something |
| 50 | // formatFromStyle does not carry. base is the workbook's default |
| 51 | // style: every cell has a font family and size, and only one that |
| 52 | // differs from the default was a choice somebody made. |
| 53 | func partialStyle(st, base *excelize.Style) bool { |
| 54 | if len(st.Border) > 0 { |
| 55 | return true |
| 56 | } |
| 57 | if st.NumFmt != 0 && (st.CustomNumFmt == nil || *st.CustomNumFmt == "") { |
| 58 | return true // a built-in number format: only custom ones come across |
| 59 | } |
| 60 | if st.Fill.Type == "gradient" || (st.Fill.Type == "pattern" && st.Fill.Pattern > 1) { |
| 61 | return true |
| 62 | } |
| 63 | if a := st.Alignment; a != nil { |
| 64 | if a.WrapText || a.Indent != 0 || a.TextRotation != 0 || a.ShrinkToFit || |
| 65 | a.Vertical == "top" || a.Vertical == "center" { |
| 66 | return true |
| 67 | } |
| 68 | } |
| 69 | if ft := st.Font; ft != nil { |
| 70 | if ft.Underline != "" || ft.Strike || ft.VertAlign != "" { |
| 71 | return true |
| 72 | } |
| 73 | if b := base.Font; b != nil && (ft.Family != b.Family || ft.Size != b.Size || |
| 74 | (ft.Color == "" && !sameTheme(ft.ColorTheme, b.ColorTheme))) { |
| 75 | return true // a theme ink differing from the default's: only literal ink comes across |
| 76 | } |
| 77 | } |
| 78 | return false |
| 79 | } |
| 80 | |
| 81 | func sameTheme(a, b *int) bool { |
| 82 | return (a == nil && b == nil) || (a != nil && b != nil && *a == *b) |
| 83 | } |
| 84 | |
| 85 | // definedNames counts the names a person defined. Excel's own _xlnm. |
| 86 | // names (a filter's range, a print area) are how other features are |
| 87 | // stored, and those are reported as themselves. |
| 88 | func definedNames(names []excelize.DefinedName) int { |
| 89 | n := 0 |
| 90 | for _, d := range names { |
| 91 | if !strings.HasPrefix(d.Name, "_xlnm.") { |
| 92 | n++ |
| 93 | } |
| 94 | } |
| 95 | return n |
| 96 | } |
| 97 | |