-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.go
67 lines (52 loc) · 1.78 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package ods
import "strconv"
// Reverses compression by permitting duplicate cells, preventing increments
// in NumberColumnsRepeated.
//
// The ignore parameter sets a limit on repetitions to ignore if the number of
// repetitions exceeds this value. This helps prevent file corruption caused by
// adding excessive rows or columns and enhances performance. This value
// should be set to only what is needed and should generally stay below 100.
func Uncompress(content Content, ignore int) Content {
sheets := content.Body.Spreadsheet.Table
for _, sheet := range sheets {
rows := sheet.TableRow
for ridx, row := range rows {
cells := row.TableCell
// Create a new slice for the row's cells
newCells := make([]TableCell, 0, len(cells))
for _, cell := range cells {
// Retrieve the number of cell repetitions
repetitions := getRepetitionCount(cell)
// Ensure we don't corrupt the file by adding too many rows/columns
// by avoiding uncompressing the end of a row or column
if repetitions > ignore {
newCells = append(newCells, cell)
continue
}
// Reset the number of repetitions, as they are no longer needed
cell.NumberColumnsRepeated = ""
// Insert a copy of the cell at the same index in the row for
// each repetition
for j := 0; j < repetitions; j++ {
newCells = append(newCells, cell)
}
}
rows[ridx].TableCell = newCells
}
sheet.TableRow = rows
}
content.Body.Spreadsheet.Table = sheets
return content
}
// Returns the number of repetitions as an int
func getRepetitionCount(cell TableCell) int {
// Attempt to parse the number of columns repeated
parsed, err := strconv.Atoi(cell.NumberColumnsRepeated)
if err != nil {
// No repetitions
return 1
}
// Return the number of repetitions
return parsed
}