This commit is contained in:
Daniel 2024-04-13 11:45:33 +08:00
parent 86f92208bf
commit 47aa891f34
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 31 additions and 28 deletions

View file

@ -18,7 +18,6 @@ package av
import ( import (
"bytes" "bytes"
"strconv"
"strings" "strings"
"github.com/siyuan-note/siyuan/kernel/util" "github.com/siyuan-note/siyuan/kernel/util"
@ -196,11 +195,9 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int {
} }
case KeyTypeTemplate: case KeyTypeTemplate:
if nil != value.Template && nil != other.Template { if nil != value.Template && nil != other.Template {
vContent := strings.TrimSpace(value.Template.Content) v1, ok1 := util.Convert2Float(value.Template.Content)
oContent := strings.TrimSpace(other.Template.Content) v2, ok2 := util.Convert2Float(other.Template.Content)
if util.IsNumeric(vContent) && util.IsNumeric(oContent) { if ok1 && ok2 {
v1, _ := strconv.ParseFloat(vContent, 64)
v2, _ := strconv.ParseFloat(oContent, 64)
if v1 > v2 { if v1 > v2 {
return 1 return 1
} }
@ -236,9 +233,9 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int {
} }
oContent := strings.TrimSpace(oContentBuf.String()) oContent := strings.TrimSpace(oContentBuf.String())
if util.IsNumeric(vContent) && util.IsNumeric(oContent) { v1, ok1 := util.Convert2Float(value.Template.Content)
v1, _ := strconv.ParseFloat(vContent, 64) v2, ok2 := util.Convert2Float(other.Template.Content)
v2, _ := strconv.ParseFloat(oContent, 64) if ok1 && ok2 {
if v1 > v2 { if v1 > v2 {
return 1 return 1
} }
@ -265,9 +262,9 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int {
} }
oContent := strings.TrimSpace(oContentBuf.String()) oContent := strings.TrimSpace(oContentBuf.String())
if util.IsNumeric(vContent) && util.IsNumeric(oContent) { v1, ok1 := util.Convert2Float(value.Template.Content)
v1, _ := strconv.ParseFloat(vContent, 64) v2, ok2 := util.Convert2Float(other.Template.Content)
v2, _ := strconv.ParseFloat(oContent, 64) if ok1 && ok2 {
if v1 > v2 { if v1 > v2 {
return 1 return 1
} }

View file

@ -19,7 +19,8 @@ package av
import ( import (
"math" "math"
"sort" "sort"
"strconv"
"github.com/siyuan-note/siyuan/kernel/util"
) )
// LayoutTable 描述了表格布局的结构。 // LayoutTable 描述了表格布局的结构。
@ -413,7 +414,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) {
sum := 0.0 sum := 0.0
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content)
sum += val sum += val
} }
} }
@ -423,7 +424,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) {
count := 0 count := 0
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content)
sum += val sum += val
count++ count++
} }
@ -435,7 +436,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) {
values := []float64{} values := []float64{}
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content)
values = append(values, val) values = append(values, val)
} }
} }
@ -451,7 +452,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) {
minVal := math.MaxFloat64 minVal := math.MaxFloat64
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content)
if val < minVal { if val < minVal {
minVal = val minVal = val
} }
@ -464,7 +465,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) {
maxVal := -math.MaxFloat64 maxVal := -math.MaxFloat64
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content)
if val > maxVal { if val > maxVal {
maxVal = val maxVal = val
} }
@ -478,7 +479,7 @@ func (table *Table) calcColTemplate(col *TableColumn, colIndex int) {
maxVal := -math.MaxFloat64 maxVal := -math.MaxFloat64
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64) val, _ := util.Convert2Float(row.Cells[colIndex].Value.Template.Content)
if val < minVal { if val < minVal {
minVal = val minVal = val
} }
@ -1634,7 +1635,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) {
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) {
for _, content := range row.Cells[colIndex].Value.Rollup.Contents { for _, content := range row.Cells[colIndex].Value.Rollup.Contents {
val, _ := strconv.ParseFloat(content.String(), 64) val, _ := util.Convert2Float(content.String())
sum += val sum += val
} }
} }
@ -1646,7 +1647,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) {
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) {
for _, content := range row.Cells[colIndex].Value.Rollup.Contents { for _, content := range row.Cells[colIndex].Value.Rollup.Contents {
val, _ := strconv.ParseFloat(content.String(), 64) val, _ := util.Convert2Float(content.String())
sum += val sum += val
count++ count++
} }
@ -1660,7 +1661,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) {
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) {
for _, content := range row.Cells[colIndex].Value.Rollup.Contents { for _, content := range row.Cells[colIndex].Value.Rollup.Contents {
val, _ := strconv.ParseFloat(content.String(), 64) val, _ := util.Convert2Float(content.String())
values = append(values, val) values = append(values, val)
} }
} }
@ -1678,7 +1679,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) {
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) {
for _, content := range row.Cells[colIndex].Value.Rollup.Contents { for _, content := range row.Cells[colIndex].Value.Rollup.Contents {
val, _ := strconv.ParseFloat(content.String(), 64) val, _ := util.Convert2Float(content.String())
if val < minVal { if val < minVal {
minVal = val minVal = val
} }
@ -1693,7 +1694,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) {
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) {
for _, content := range row.Cells[colIndex].Value.Rollup.Contents { for _, content := range row.Cells[colIndex].Value.Rollup.Contents {
val, _ := strconv.ParseFloat(content.String(), 64) val, _ := util.Convert2Float(content.String())
if val > maxVal { if val > maxVal {
maxVal = val maxVal = val
} }
@ -1709,7 +1710,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) {
for _, row := range table.Rows { for _, row := range table.Rows {
if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) {
for _, content := range row.Cells[colIndex].Value.Rollup.Contents { for _, content := range row.Cells[colIndex].Value.Rollup.Contents {
val, _ := strconv.ParseFloat(content.String(), 64) val, _ := util.Convert2Float(content.String())
if val < minVal { if val < minVal {
minVal = val minVal = val
} }

View file

@ -24,6 +24,7 @@ import (
"time" "time"
"unicode" "unicode"
"github.com/88250/gulu"
"github.com/88250/lute/html" "github.com/88250/lute/html"
) )
@ -117,9 +118,13 @@ func RemoveRedundantSpace(str string) string {
return buf.String() return buf.String()
} }
func IsNumeric(s string) bool { func Convert2Float(s string) (float64, bool) {
_, err := strconv.ParseFloat(s, 64) s = gulu.Str.RemoveInvisible(s)
return err == nil ret, err := strconv.ParseFloat(strings.TrimSpace(s), 64)
if nil != err {
return 0, false
}
return ret, true
} }
func ContainsSubStr(s string, subStrs []string) bool { func ContainsSubStr(s string, subStrs []string) bool {