Skip to content

Commit 4671b7b

Browse files
committed
feat(excel): highlight cells changed last week
1 parent 278939c commit 4671b7b

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

excel/balance.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,40 +158,45 @@ func balances(doc *sie.Document) map[int]*balance {
158158
for _, acc := range doc.Accounts {
159159
balances[acc.ID] = newBalance()
160160
if acc.InBalance != 0 {
161-
balances[acc.ID].add(time.Time{}, acc.InBalance)
161+
balances[acc.ID].add(time.Time{}, time.Time{}, acc.InBalance)
162162
}
163163
}
164164
for _, entry := range doc.Entries {
165165
for _, tran := range entry.Transactions {
166-
balances[tran.AccountID].add(entry.Date, tran.Amount)
166+
balances[tran.AccountID].add(entry.Date, entry.Filed, tran.Amount)
167167
}
168168
}
169169
return balances
170170
}
171171

172172
type balance struct {
173173
total sie.Decimal
174-
months map[string][]sie.Decimal
174+
months map[string][]cellValue
175+
}
176+
177+
type cellValue struct {
178+
amount sie.Decimal
179+
when time.Time
175180
}
176181

177182
func newBalance() *balance {
178183
return &balance{
179-
months: make(map[string][]sie.Decimal),
184+
months: make(map[string][]cellValue),
180185
}
181186
}
182187

183-
func (b *balance) add(date time.Time, amount sie.Decimal) {
188+
func (b *balance) add(date, filed time.Time, amount sie.Decimal) {
184189
b.total += amount
185190
key := date.Format("2006-01")
186-
b.months[key] = append(b.months[key], amount)
191+
b.months[key] = append(b.months[key], cellValue{amount: amount, when: filed})
187192
}
188193

189194
func (b *balance) inverse() *balance {
190195
new := newBalance()
191196
new.total -= b.total
192197
for m, v := range b.months {
193198
for i := range v {
194-
new.months[m] = append(new.months[m], -v[i])
199+
new.months[m] = append(new.months[m], cellValue{amount: -v[i].amount, when: v[i].when})
195200
}
196201
}
197202
return new

excel/result.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,24 @@ func xlsxAccountMonths(xlsx *excelize.File, sheet string, row int, id int, descr
225225
col := 'C'
226226
for !t.After(ends) {
227227
if v := bal.months[t.Format("2006-01")]; len(v) == 1 {
228-
_ = xlsx.SetCellValue(sheet, cell(col, row), v[0].Float64())
228+
_ = xlsx.SetCellValue(sheet, cell(col, row), v[0].amount.Float64())
229229
} else if len(v) != 0 {
230230
_ = xlsx.SetCellFormula(sheet, cell(col, row), sumFormula(v))
231231
}
232+
if style := cellStyle(bal.months[t.Format("2006-01")]); style != nil {
233+
style, _ := xlsx.NewStyle(mergeStyles(defaultStyle(), customNumberFormat(), style))
234+
_ = xlsx.SetCellStyle(sheet, cell(col, row), cell(col, row), style)
235+
} else {
236+
style, _ := xlsx.NewStyle(mergeStyles(defaultStyle(), customNumberFormat()))
237+
_ = xlsx.SetCellStyle(sheet, cell(col, row), cell(col, row), style)
238+
}
232239
col++
233240
t = t.AddDate(0, 1, 0)
234241
}
235242
col++
236243

237244
_ = xlsx.SetCellFormula(sheet, cell(col, row), fmt.Sprintf("SUM(C%d:%c%d)", row, col-1, row))
238-
style, _ := xlsx.NewStyle(mergeStyles(defaultStyle(), customNumberFormat()))
239-
_ = xlsx.SetCellStyle(sheet, cell('C', row), cell(col, row), style)
240-
style, _ = xlsx.NewStyle(mergeStyles(defaultStyle(), fontItalic(), customNumberFormat()))
245+
style, _ := xlsx.NewStyle(mergeStyles(defaultStyle(), fontItalic(), customNumberFormat()))
241246
_ = xlsx.SetCellStyle(sheet, cell(col, row), cell(col, row), style)
242247
}
243248

@@ -324,6 +329,16 @@ func thickBorder(where ...string) *excelize.Style {
324329
return s
325330
}
326331

332+
func highlight() *excelize.Style {
333+
return &excelize.Style{
334+
Fill: excelize.Fill{
335+
Type: "pattern",
336+
Color: []string{"#FFFF50"},
337+
Pattern: 1,
338+
},
339+
}
340+
}
341+
327342
func mergeStyles(ext ...*excelize.Style) *excelize.Style {
328343
if len(ext) == 0 {
329344
return nil
@@ -462,17 +477,33 @@ func xlsxSectionSum(xlsx *excelize.File, sheet string, row int, hdr string, star
462477
_ = xlsx.SetRowHeight(sheet, row, 20)
463478
}
464479

465-
func sumFormula(v []sie.Decimal) string {
480+
func sumFormula(v []cellValue) string {
466481
var b strings.Builder
467482
for i, d := range v {
468483
switch {
469-
case i > 0 && d >= 0:
484+
case i > 0 && d.amount >= 0:
470485
b.WriteString(" + ")
471-
case i > 0 && d < 0:
486+
case i > 0 && d.amount < 0:
472487
b.WriteString(" - ")
473-
d = -d
488+
d.amount = -d.amount
474489
}
475-
fmt.Fprintf(&b, "%v", d.Float64())
490+
fmt.Fprintf(&b, "%v", d.amount.Float64())
476491
}
477492
return b.String()
478493
}
494+
495+
func cellStyle(v []cellValue) *excelize.Style {
496+
if len(v) == 0 {
497+
return nil
498+
}
499+
var latest time.Time
500+
for _, d := range v {
501+
if d.when.After(latest) {
502+
latest = d.when
503+
}
504+
}
505+
if latest.After(time.Now().AddDate(0, 0, -7)) {
506+
return highlight()
507+
}
508+
return nil
509+
}

0 commit comments

Comments
 (0)