-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhance.go
More file actions
61 lines (51 loc) · 1.31 KB
/
enhance.go
File metadata and controls
61 lines (51 loc) · 1.31 KB
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
package dgexcel
import (
"fmt"
"strings"
dgerr "github.com/darwinOrg/go-common/enums/error"
"github.com/xuri/excelize/v2"
)
func RemoveDuplicateRowsByColumn(xlsx *excelize.File, sheetName string, startRowIndex int, columnIndex int, reserveValues ...string) (*excelize.File, error) {
rows, err := xlsx.GetRows(sheetName)
if err != nil {
return nil, err
}
if startRowIndex >= len(rows) {
return nil, dgerr.ARGUMENT_NOT_VALID
}
newRows := rows[:startRowIndex]
rows = rows[startRowIndex:]
mp := make(map[string]bool)
var duplicatedRowIndexes []int
for i, row := range rows {
cell := strings.TrimSpace(row[columnIndex])
if len(reserveValues) > 0 {
for _, reserveValue := range reserveValues {
if cell == reserveValue {
newRows = append(newRows, row)
continue
}
}
}
if mp[cell] {
duplicatedRowIndexes = append(duplicatedRowIndexes, startRowIndex+i)
} else {
mp[cell] = true
newRows = append(newRows, row)
}
}
if len(duplicatedRowIndexes) == 0 {
return xlsx, nil
}
nf := excelize.NewFile()
_, err = nf.NewSheet(sheetName)
if err != nil {
return nil, err
}
for rowIndex, row := range newRows {
for colIndex, cellValue := range row {
_ = nf.SetCellValue(sheetName, fmt.Sprintf("%s%d", ColumnIndexToName(colIndex), rowIndex+1), cellValue)
}
}
return nf, nil
}