| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | |
| 7 | "amadan.net/oficina/sheets/internal/sheets/xlsx" |
| 8 | ) |
| 9 | |
| 10 | // importReportPath is where an import's report lives, and where its |
| 11 | // job lands. |
| 12 | func importReportPath(sheetID uint64) string { |
| 13 | return "/sheets/" + strconv.FormatUint(sheetID, 10) + "/import-report" |
| 14 | } |
| 15 | |
| 16 | // reportLine is one entry of the report as a person reads it. |
| 17 | type reportLine struct { |
| 18 | Title, Detail string |
| 19 | Count int |
| 20 | Cells []string |
| 21 | More int // coordinates past xlsx.MaxListedCells |
| 22 | } |
| 23 | |
| 24 | type importReportView struct { |
| 25 | Sheet SheetSummary |
| 26 | OmittedWorksheets int |
| 27 | Lines []reportLine |
| 28 | } |
| 29 | |
| 30 | // reportCopy is each code's wording, in the order the page lists them: |
| 31 | // what changed in the cells first, then what was left out. A code this |
| 32 | // table does not name is still shown, under its code, rather than |
| 33 | // dropped. |
| 34 | var reportCopy = []struct { |
| 35 | code xlsx.Code |
| 36 | title, detail string |
| 37 | }{ |
| 38 | {xlsx.FormulaGoogleOnly, "Google-only formulas kept as values", |
| 39 | "These used a function only Google Sheets has. Each cell holds the value Google saved, not the formula."}, |
| 40 | {xlsx.FormulaOtherSheet, "Formulas reading other sheets kept as values", |
| 41 | "These referred to another worksheet or workbook, which did not come across. Each cell holds its saved value."}, |
| 42 | {xlsx.FormulaArray, "Array formulas kept as values", |
| 43 | "Sheets cannot fill a range from one formula. Each cell holds its saved value."}, |
| 44 | {xlsx.FormulaUnsupported, "Other formulas kept as values", |
| 45 | "Sheets cannot calculate these. Each cell holds the value the workbook saved."}, |
| 46 | {xlsx.FormulaDiffers, "Formulas that calculate differently here", |
| 47 | "These stayed formulas, but Sheets' result differs from the value the workbook saved. Check them."}, |
| 48 | {xlsx.FormulaUnchecked, "Formulas not checked", |
| 49 | "These stayed formulas. There was no saved value to compare them with, or they read the clock, so their results here were not checked."}, |
| 50 | {xlsx.TextKept, "Text kept as text", |
| 51 | `This text would otherwise have become a number, a true/false value, a formula or a blank cell, so it is written as ="…" to stay exactly as it was.`}, |
| 52 | {xlsx.FormatPartial, "Formatting partly kept", |
| 53 | "These cells used formatting Sheets does not have, such as borders, fonts, wrapping or built-in number formats."}, |
| 54 | {xlsx.OmittedMerges, "Merged cells", "Each merge was left out; the cells are separate here."}, |
| 55 | {xlsx.OmittedComments, "Comments and notes", "Not imported."}, |
| 56 | {xlsx.OmittedValidation, "Data validation rules", "Not imported."}, |
| 57 | {xlsx.OmittedConditional, "Conditional formatting rules", "Not imported."}, |
| 58 | {xlsx.OmittedHyperlinks, "Links", "The cell text came across; the links did not."}, |
| 59 | {xlsx.OmittedDefinedNames, "Named ranges", "Not imported."}, |
| 60 | {xlsx.OmittedHiddenRows, "Hidden rows", "They are visible here."}, |
| 61 | {xlsx.OmittedHiddenCols, "Hidden columns", "They are visible here."}, |
| 62 | {xlsx.OmittedFrozenPanes, "Frozen rows or columns", "Not imported."}, |
| 63 | {xlsx.OmittedFilter, "Filters", "Not imported."}, |
| 64 | {xlsx.OmittedPictures, "Pictures in cells", "Not imported."}, |
| 65 | {xlsx.OmittedWorkbookChart, "Charts in the workbook", "Not imported."}, |
| 66 | {xlsx.OmittedWorkbookImage, "Images in the workbook", "Not imported."}, |
| 67 | {xlsx.OmittedUndetected, "Parts that could not be checked", |
| 68 | "Some features could not be read, so this report may not list everything that was left out."}, |
| 69 | } |
| 70 | |
| 71 | // reportLines puts a report in reading order. |
| 72 | func reportLines(rep xlsx.Report) []reportLine { |
| 73 | byCode := map[xlsx.Code]xlsx.Entry{} |
| 74 | for _, e := range rep.Entries { |
| 75 | byCode[e.Code] = e |
| 76 | } |
| 77 | line := func(e xlsx.Entry, title, detail string) reportLine { |
| 78 | return reportLine{Title: title, Detail: detail, Count: e.Count, |
| 79 | Cells: e.Cells, More: max(0, e.Count-len(e.Cells))} |
| 80 | } |
| 81 | var out []reportLine |
| 82 | for _, c := range reportCopy { |
| 83 | if e, ok := byCode[c.code]; ok && e.Count > 0 { |
| 84 | out = append(out, line(e, c.title, c.detail)) |
| 85 | delete(byCode, c.code) |
| 86 | } |
| 87 | } |
| 88 | for _, e := range rep.Entries { |
| 89 | if _, ok := byCode[e.Code]; ok && e.Count > 0 { |
| 90 | out = append(out, line(e, string(e.Code), "")) |
| 91 | } |
| 92 | } |
| 93 | return out |
| 94 | } |
| 95 | |
| 96 | // handleImportReport shows what an import changed or left behind. |
| 97 | // |
| 98 | // Read access to the sheet is the whole rule: the report is a fact |
| 99 | // about the sheet, so anyone who may read the sheet may read it, and |
| 100 | // anyone who may not gets the same 404 as for the sheet itself. A |
| 101 | // sheet with no report is a 404 too. |
| 102 | func (a *App) handleImportReport(w http.ResponseWriter, r *http.Request) { |
| 103 | s, ok := a.sheetFromURL(w, r) |
| 104 | if !ok { |
| 105 | return |
| 106 | } |
| 107 | rep, ok, err := a.importReport(s.ID) |
| 108 | if err != nil { |
| 109 | a.fail(w, r, err) |
| 110 | return |
| 111 | } |
| 112 | if !ok { |
| 113 | a.notFound(w, r) |
| 114 | return |
| 115 | } |
| 116 | a.render(w, r, "import_report", titled("Import report", s.Title), http.StatusOK, importReportView{ |
| 117 | Sheet: s, |
| 118 | OmittedWorksheets: rep.OmittedWorksheets(), |
| 119 | Lines: reportLines(rep), |
| 120 | }) |
| 121 | } |
| 122 | |