table.go 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package av
  17. import (
  18. "math"
  19. "sort"
  20. "strconv"
  21. )
  22. // LayoutTable 描述了表格布局的结构。
  23. type LayoutTable struct {
  24. Spec int `json:"spec"` // 布局格式版本
  25. ID string `json:"id"` // 布局 ID
  26. Columns []*ViewTableColumn `json:"columns"` // 表格列
  27. RowIDs []string `json:"rowIds"` // 行 ID,用于自定义排序
  28. Filters []*ViewFilter `json:"filters"` // 过滤规则
  29. Sorts []*ViewSort `json:"sorts"` // 排序规则
  30. PageSize int `json:"pageSize"` // 每页行数
  31. }
  32. type ViewTableColumn struct {
  33. ID string `json:"id"` // 列 ID
  34. Wrap bool `json:"wrap"` // 是否换行
  35. Hidden bool `json:"hidden"` // 是否隐藏
  36. Pin bool `json:"pin"` // 是否固定
  37. Width string `json:"width"` // 列宽度
  38. Calc *ColumnCalc `json:"calc,omitempty"` // 计算
  39. }
  40. type Calculable interface {
  41. CalcCols()
  42. }
  43. type ColumnCalc struct {
  44. Operator CalcOperator `json:"operator"`
  45. Result *Value `json:"result"`
  46. }
  47. type CalcOperator string
  48. const (
  49. CalcOperatorNone CalcOperator = ""
  50. CalcOperatorCountAll CalcOperator = "Count all"
  51. CalcOperatorCountValues CalcOperator = "Count values"
  52. CalcOperatorCountUniqueValues CalcOperator = "Count unique values"
  53. CalcOperatorCountEmpty CalcOperator = "Count empty"
  54. CalcOperatorCountNotEmpty CalcOperator = "Count not empty"
  55. CalcOperatorPercentEmpty CalcOperator = "Percent empty"
  56. CalcOperatorPercentNotEmpty CalcOperator = "Percent not empty"
  57. CalcOperatorSum CalcOperator = "Sum"
  58. CalcOperatorAverage CalcOperator = "Average"
  59. CalcOperatorMedian CalcOperator = "Median"
  60. CalcOperatorMin CalcOperator = "Min"
  61. CalcOperatorMax CalcOperator = "Max"
  62. CalcOperatorRange CalcOperator = "Range"
  63. CalcOperatorEarliest CalcOperator = "Earliest"
  64. CalcOperatorLatest CalcOperator = "Latest"
  65. CalcOperatorChecked CalcOperator = "Checked"
  66. CalcOperatorUnchecked CalcOperator = "Unchecked"
  67. CalcOperatorPercentChecked CalcOperator = "Percent checked"
  68. CalcOperatorPercentUnchecked CalcOperator = "Percent unchecked"
  69. )
  70. // Table 描述了表格实例的结构。
  71. type Table struct {
  72. ID string `json:"id"` // 表格布局 ID
  73. Icon string `json:"icon"` // 表格图标
  74. Name string `json:"name"` // 表格名称
  75. HideAttrViewName bool `json:"hideAttrViewName"` // 是否隐藏属性视图名称
  76. Filters []*ViewFilter `json:"filters"` // 过滤规则
  77. Sorts []*ViewSort `json:"sorts"` // 排序规则
  78. Columns []*TableColumn `json:"columns"` // 表格列
  79. Rows []*TableRow `json:"rows"` // 表格行
  80. RowCount int `json:"rowCount"` // 表格总行数
  81. PageSize int `json:"pageSize"` // 每页行数
  82. }
  83. type TableColumn struct {
  84. ID string `json:"id"` // 列 ID
  85. Name string `json:"name"` // 列名
  86. Type KeyType `json:"type"` // 列类型
  87. Icon string `json:"icon"` // 列图标
  88. Wrap bool `json:"wrap"` // 是否换行
  89. Hidden bool `json:"hidden"` // 是否隐藏
  90. Pin bool `json:"pin"` // 是否固定
  91. Width string `json:"width"` // 列宽度
  92. Calc *ColumnCalc `json:"calc"` // 计算
  93. // 以下是某些列类型的特有属性
  94. Options []*SelectOption `json:"options,omitempty"` // 选项列表
  95. NumberFormat NumberFormat `json:"numberFormat"` // 列数字格式化
  96. Template string `json:"template"` // 模板内容
  97. Relation *Relation `json:"relation,omitempty"` // 关联列
  98. Rollup *Rollup `json:"rollup,omitempty"` // 汇总列
  99. }
  100. type TableCell struct {
  101. ID string `json:"id"`
  102. Value *Value `json:"value"`
  103. ValueType KeyType `json:"valueType"`
  104. Color string `json:"color"`
  105. BgColor string `json:"bgColor"`
  106. }
  107. type TableRow struct {
  108. ID string `json:"id"`
  109. Cells []*TableCell `json:"cells"`
  110. }
  111. func (row *TableRow) GetBlockValue() (ret *Value) {
  112. for _, cell := range row.Cells {
  113. if KeyTypeBlock == cell.ValueType {
  114. ret = cell.Value
  115. break
  116. }
  117. }
  118. return
  119. }
  120. func (row *TableRow) GetValue(keyID string) (ret *Value) {
  121. for _, cell := range row.Cells {
  122. if nil != cell.Value && keyID == cell.Value.KeyID {
  123. ret = cell.Value
  124. break
  125. }
  126. }
  127. return
  128. }
  129. func (table *Table) GetType() LayoutType {
  130. return LayoutTypeTable
  131. }
  132. func (table *Table) GetID() string {
  133. return table.ID
  134. }
  135. func (table *Table) SortRows() {
  136. if 1 > len(table.Sorts) {
  137. return
  138. }
  139. type ColIndexSort struct {
  140. Index int
  141. Order SortOrder
  142. }
  143. var colIndexSorts []*ColIndexSort
  144. for _, s := range table.Sorts {
  145. for i, c := range table.Columns {
  146. if c.ID == s.Column {
  147. colIndexSorts = append(colIndexSorts, &ColIndexSort{Index: i, Order: s.Order})
  148. break
  149. }
  150. }
  151. }
  152. includeUneditedRows := map[string]bool{}
  153. for i, row := range table.Rows {
  154. for _, colIndexSort := range colIndexSorts {
  155. val := table.Rows[i].Cells[colIndexSort.Index].Value
  156. if !val.IsEdited() {
  157. // 如果该行的某个列的值是未编辑的,则该行不参与排序
  158. includeUneditedRows[row.ID] = true
  159. break
  160. }
  161. }
  162. }
  163. // 将包含未编辑的行和全部已编辑的行分开排序
  164. var uneditedRows, editedRows []*TableRow
  165. for _, row := range table.Rows {
  166. if _, ok := includeUneditedRows[row.ID]; ok {
  167. uneditedRows = append(uneditedRows, row)
  168. } else {
  169. editedRows = append(editedRows, row)
  170. }
  171. }
  172. sort.Slice(uneditedRows, func(i, j int) bool {
  173. val1 := uneditedRows[i].GetBlockValue()
  174. if nil == val1 {
  175. return true
  176. }
  177. val2 := uneditedRows[j].GetBlockValue()
  178. if nil == val2 {
  179. return false
  180. }
  181. return val1.CreatedAt < val2.CreatedAt
  182. })
  183. sort.Slice(editedRows, func(i, j int) bool {
  184. sorted := true
  185. for _, colIndexSort := range colIndexSorts {
  186. val1 := editedRows[i].Cells[colIndexSort.Index].Value
  187. if nil == val1 {
  188. return colIndexSort.Order == SortOrderAsc
  189. }
  190. val2 := editedRows[j].Cells[colIndexSort.Index].Value
  191. if nil == val2 {
  192. return colIndexSort.Order != SortOrderAsc
  193. }
  194. result := val1.Compare(val2)
  195. if 0 == result {
  196. sorted = false
  197. continue
  198. }
  199. sorted = true
  200. if colIndexSort.Order == SortOrderAsc {
  201. return 0 > result
  202. }
  203. return 0 < result
  204. }
  205. if !sorted {
  206. key1 := editedRows[i].GetBlockValue()
  207. if nil == key1 {
  208. return false
  209. }
  210. key2 := editedRows[j].GetBlockValue()
  211. if nil == key2 {
  212. return false
  213. }
  214. return key1.CreatedAt < key2.CreatedAt
  215. }
  216. return false
  217. })
  218. // 将包含未编辑的行放在最后
  219. table.Rows = append(editedRows, uneditedRows...)
  220. if 1 > len(table.Rows) {
  221. table.Rows = []*TableRow{}
  222. }
  223. }
  224. func (table *Table) FilterRows(attrView *AttributeView) {
  225. if 1 > len(table.Filters) {
  226. return
  227. }
  228. var colIndexes []int
  229. for _, f := range table.Filters {
  230. for i, c := range table.Columns {
  231. if c.ID == f.Column {
  232. colIndexes = append(colIndexes, i)
  233. break
  234. }
  235. }
  236. }
  237. rows := []*TableRow{}
  238. for _, row := range table.Rows {
  239. pass := true
  240. for j, index := range colIndexes {
  241. operator := table.Filters[j].Operator
  242. if nil == row.Cells[index].Value {
  243. if FilterOperatorIsNotEmpty == operator {
  244. pass = false
  245. } else if FilterOperatorIsEmpty == operator {
  246. pass = true
  247. break
  248. }
  249. if KeyTypeText != row.Cells[index].ValueType {
  250. pass = false
  251. }
  252. break
  253. }
  254. if !row.Cells[index].Value.Filter(table.Filters[j], attrView, row.ID) {
  255. pass = false
  256. break
  257. }
  258. }
  259. if pass {
  260. rows = append(rows, row)
  261. }
  262. }
  263. table.Rows = rows
  264. }
  265. func (table *Table) CalcCols() {
  266. for i, col := range table.Columns {
  267. if nil == col.Calc {
  268. continue
  269. }
  270. if CalcOperatorNone == col.Calc.Operator {
  271. continue
  272. }
  273. switch col.Type {
  274. case KeyTypeBlock:
  275. table.calcColBlock(col, i)
  276. case KeyTypeText:
  277. table.calcColText(col, i)
  278. case KeyTypeNumber:
  279. table.calcColNumber(col, i)
  280. case KeyTypeDate:
  281. table.calcColDate(col, i)
  282. case KeyTypeSelect:
  283. table.calcColSelect(col, i)
  284. case KeyTypeMSelect:
  285. table.calcColMSelect(col, i)
  286. case KeyTypeURL:
  287. table.calcColURL(col, i)
  288. case KeyTypeEmail:
  289. table.calcColEmail(col, i)
  290. case KeyTypePhone:
  291. table.calcColPhone(col, i)
  292. case KeyTypeMAsset:
  293. table.calcColMAsset(col, i)
  294. case KeyTypeTemplate:
  295. table.calcColTemplate(col, i)
  296. case KeyTypeCreated:
  297. table.calcColCreated(col, i)
  298. case KeyTypeUpdated:
  299. table.calcColUpdated(col, i)
  300. case KeyTypeCheckbox:
  301. table.calcColCheckbox(col, i)
  302. case KeyTypeRelation:
  303. table.calcColRelation(col, i)
  304. case KeyTypeRollup:
  305. table.calcColRollup(col, i)
  306. }
  307. }
  308. }
  309. func (table *Table) calcColTemplate(col *TableColumn, colIndex int) {
  310. switch col.Calc.Operator {
  311. case CalcOperatorCountAll:
  312. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  313. case CalcOperatorCountValues:
  314. countValues := 0
  315. for _, row := range table.Rows {
  316. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  317. countValues++
  318. }
  319. }
  320. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  321. case CalcOperatorCountUniqueValues:
  322. countUniqueValues := 0
  323. uniqueValues := map[string]bool{}
  324. for _, row := range table.Rows {
  325. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  326. if !uniqueValues[row.Cells[colIndex].Value.Template.Content] {
  327. uniqueValues[row.Cells[colIndex].Value.Template.Content] = true
  328. countUniqueValues++
  329. }
  330. }
  331. }
  332. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  333. case CalcOperatorCountEmpty:
  334. countEmpty := 0
  335. for _, row := range table.Rows {
  336. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Template || "" == row.Cells[colIndex].Value.Template.Content {
  337. countEmpty++
  338. }
  339. }
  340. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  341. case CalcOperatorCountNotEmpty:
  342. countNotEmpty := 0
  343. for _, row := range table.Rows {
  344. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  345. countNotEmpty++
  346. }
  347. }
  348. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  349. case CalcOperatorPercentEmpty:
  350. countEmpty := 0
  351. for _, row := range table.Rows {
  352. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Template || "" == row.Cells[colIndex].Value.Template.Content {
  353. countEmpty++
  354. }
  355. }
  356. if 0 < len(table.Rows) {
  357. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  358. }
  359. case CalcOperatorPercentNotEmpty:
  360. countNotEmpty := 0
  361. for _, row := range table.Rows {
  362. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  363. countNotEmpty++
  364. }
  365. }
  366. if 0 < len(table.Rows) {
  367. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  368. }
  369. case CalcOperatorSum:
  370. sum := 0.0
  371. for _, row := range table.Rows {
  372. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  373. val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64)
  374. sum += val
  375. }
  376. }
  377. col.Calc.Result = &Value{Number: NewFormattedValueNumber(sum, col.NumberFormat)}
  378. case CalcOperatorAverage:
  379. sum := 0.0
  380. count := 0
  381. for _, row := range table.Rows {
  382. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  383. val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64)
  384. sum += val
  385. count++
  386. }
  387. }
  388. if 0 != count {
  389. col.Calc.Result = &Value{Number: NewFormattedValueNumber(sum/float64(count), col.NumberFormat)}
  390. }
  391. case CalcOperatorMedian:
  392. values := []float64{}
  393. for _, row := range table.Rows {
  394. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  395. val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64)
  396. values = append(values, val)
  397. }
  398. }
  399. sort.Float64s(values)
  400. if len(values) > 0 {
  401. if len(values)%2 == 0 {
  402. col.Calc.Result = &Value{Number: NewFormattedValueNumber((values[len(values)/2-1]+values[len(values)/2])/2, col.NumberFormat)}
  403. } else {
  404. col.Calc.Result = &Value{Number: NewFormattedValueNumber(values[len(values)/2], col.NumberFormat)}
  405. }
  406. }
  407. case CalcOperatorMin:
  408. minVal := math.MaxFloat64
  409. for _, row := range table.Rows {
  410. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  411. val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64)
  412. if val < minVal {
  413. minVal = val
  414. }
  415. }
  416. }
  417. if math.MaxFloat64 != minVal {
  418. col.Calc.Result = &Value{Number: NewFormattedValueNumber(minVal, col.NumberFormat)}
  419. }
  420. case CalcOperatorMax:
  421. maxVal := -math.MaxFloat64
  422. for _, row := range table.Rows {
  423. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  424. val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64)
  425. if val > maxVal {
  426. maxVal = val
  427. }
  428. }
  429. }
  430. if -math.MaxFloat64 != maxVal {
  431. col.Calc.Result = &Value{Number: NewFormattedValueNumber(maxVal, col.NumberFormat)}
  432. }
  433. case CalcOperatorRange:
  434. minVal := math.MaxFloat64
  435. maxVal := -math.MaxFloat64
  436. for _, row := range table.Rows {
  437. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Template && "" != row.Cells[colIndex].Value.Template.Content {
  438. val, _ := strconv.ParseFloat(row.Cells[colIndex].Value.Template.Content, 64)
  439. if val < minVal {
  440. minVal = val
  441. }
  442. if val > maxVal {
  443. maxVal = val
  444. }
  445. }
  446. }
  447. if math.MaxFloat64 != minVal && -math.MaxFloat64 != maxVal {
  448. col.Calc.Result = &Value{Number: NewFormattedValueNumber(maxVal-minVal, col.NumberFormat)}
  449. }
  450. }
  451. }
  452. func (table *Table) calcColMAsset(col *TableColumn, colIndex int) {
  453. switch col.Calc.Operator {
  454. case CalcOperatorCountAll:
  455. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  456. case CalcOperatorCountValues:
  457. countValues := 0
  458. for _, row := range table.Rows {
  459. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MAsset && 0 < len(row.Cells[colIndex].Value.MAsset) {
  460. countValues += len(row.Cells[colIndex].Value.MAsset)
  461. }
  462. }
  463. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  464. case CalcOperatorCountUniqueValues:
  465. countUniqueValues := 0
  466. uniqueValues := map[string]bool{}
  467. for _, row := range table.Rows {
  468. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MAsset && 0 < len(row.Cells[colIndex].Value.MAsset) {
  469. for _, sel := range row.Cells[colIndex].Value.MAsset {
  470. if _, ok := uniqueValues[sel.Content]; !ok {
  471. uniqueValues[sel.Content] = true
  472. countUniqueValues++
  473. }
  474. }
  475. }
  476. }
  477. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  478. case CalcOperatorCountEmpty:
  479. countEmpty := 0
  480. for _, row := range table.Rows {
  481. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.MAsset || 0 == len(row.Cells[colIndex].Value.MAsset) {
  482. countEmpty++
  483. }
  484. }
  485. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  486. case CalcOperatorCountNotEmpty:
  487. countNotEmpty := 0
  488. for _, row := range table.Rows {
  489. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MAsset && 0 < len(row.Cells[colIndex].Value.MAsset) {
  490. countNotEmpty++
  491. }
  492. }
  493. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  494. case CalcOperatorPercentEmpty:
  495. countEmpty := 0
  496. for _, row := range table.Rows {
  497. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.MAsset || 0 == len(row.Cells[colIndex].Value.MAsset) {
  498. countEmpty++
  499. }
  500. }
  501. if 0 < len(table.Rows) {
  502. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  503. }
  504. case CalcOperatorPercentNotEmpty:
  505. countNotEmpty := 0
  506. for _, row := range table.Rows {
  507. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MAsset && 0 < len(row.Cells[colIndex].Value.MAsset) {
  508. countNotEmpty++
  509. }
  510. }
  511. if 0 < len(table.Rows) {
  512. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  513. }
  514. }
  515. }
  516. func (table *Table) calcColMSelect(col *TableColumn, colIndex int) {
  517. switch col.Calc.Operator {
  518. case CalcOperatorCountAll:
  519. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  520. case CalcOperatorCountValues:
  521. countValues := 0
  522. for _, row := range table.Rows {
  523. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MSelect && 0 < len(row.Cells[colIndex].Value.MSelect) {
  524. countValues += len(row.Cells[colIndex].Value.MSelect)
  525. }
  526. }
  527. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  528. case CalcOperatorCountUniqueValues:
  529. countUniqueValues := 0
  530. uniqueValues := map[string]bool{}
  531. for _, row := range table.Rows {
  532. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MSelect && 0 < len(row.Cells[colIndex].Value.MSelect) {
  533. for _, sel := range row.Cells[colIndex].Value.MSelect {
  534. if _, ok := uniqueValues[sel.Content]; !ok {
  535. uniqueValues[sel.Content] = true
  536. countUniqueValues++
  537. }
  538. }
  539. }
  540. }
  541. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  542. case CalcOperatorCountEmpty:
  543. countEmpty := 0
  544. for _, row := range table.Rows {
  545. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.MSelect || 0 == len(row.Cells[colIndex].Value.MSelect) {
  546. countEmpty++
  547. }
  548. }
  549. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  550. case CalcOperatorCountNotEmpty:
  551. countNotEmpty := 0
  552. for _, row := range table.Rows {
  553. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MSelect && 0 < len(row.Cells[colIndex].Value.MSelect) {
  554. countNotEmpty++
  555. }
  556. }
  557. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  558. case CalcOperatorPercentEmpty:
  559. countEmpty := 0
  560. for _, row := range table.Rows {
  561. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.MSelect || 0 == len(row.Cells[colIndex].Value.MSelect) {
  562. countEmpty++
  563. }
  564. }
  565. if 0 < len(table.Rows) {
  566. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  567. }
  568. case CalcOperatorPercentNotEmpty:
  569. countNotEmpty := 0
  570. for _, row := range table.Rows {
  571. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MSelect && 0 < len(row.Cells[colIndex].Value.MSelect) {
  572. countNotEmpty++
  573. }
  574. }
  575. if 0 < len(table.Rows) {
  576. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  577. }
  578. }
  579. }
  580. func (table *Table) calcColSelect(col *TableColumn, colIndex int) {
  581. switch col.Calc.Operator {
  582. case CalcOperatorCountAll:
  583. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  584. case CalcOperatorCountValues:
  585. countValues := 0
  586. for _, row := range table.Rows {
  587. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MSelect && 0 < len(row.Cells[colIndex].Value.MSelect) && nil != row.Cells[colIndex].Value.MSelect[0] && "" != row.Cells[colIndex].Value.MSelect[0].Content {
  588. countValues++
  589. }
  590. }
  591. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  592. case CalcOperatorCountUniqueValues:
  593. countUniqueValues := 0
  594. uniqueValues := map[string]bool{}
  595. for _, row := range table.Rows {
  596. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MSelect && 0 < len(row.Cells[colIndex].Value.MSelect) && nil != row.Cells[colIndex].Value.MSelect[0] && "" != row.Cells[colIndex].Value.MSelect[0].Content {
  597. if _, ok := uniqueValues[row.Cells[colIndex].Value.MSelect[0].Content]; !ok {
  598. uniqueValues[row.Cells[colIndex].Value.MSelect[0].Content] = true
  599. countUniqueValues++
  600. }
  601. }
  602. }
  603. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  604. case CalcOperatorCountEmpty:
  605. countEmpty := 0
  606. for _, row := range table.Rows {
  607. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.MSelect || 1 > len(row.Cells[colIndex].Value.MSelect) || nil == row.Cells[colIndex].Value.MSelect[0] || "" == row.Cells[colIndex].Value.MSelect[0].Content {
  608. countEmpty++
  609. }
  610. }
  611. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  612. case CalcOperatorCountNotEmpty:
  613. countNotEmpty := 0
  614. for _, row := range table.Rows {
  615. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MSelect && 0 < len(row.Cells[colIndex].Value.MSelect) && nil != row.Cells[colIndex].Value.MSelect[0] && "" != row.Cells[colIndex].Value.MSelect[0].Content {
  616. countNotEmpty++
  617. }
  618. }
  619. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  620. case CalcOperatorPercentEmpty:
  621. countEmpty := 0
  622. for _, row := range table.Rows {
  623. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.MSelect || 1 > len(row.Cells[colIndex].Value.MSelect) || nil == row.Cells[colIndex].Value.MSelect[0] || "" == row.Cells[colIndex].Value.MSelect[0].Content {
  624. countEmpty++
  625. }
  626. }
  627. if 0 < len(table.Rows) {
  628. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  629. }
  630. case CalcOperatorPercentNotEmpty:
  631. countNotEmpty := 0
  632. for _, row := range table.Rows {
  633. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.MSelect && 0 < len(row.Cells[colIndex].Value.MSelect) && nil != row.Cells[colIndex].Value.MSelect[0] && "" != row.Cells[colIndex].Value.MSelect[0].Content {
  634. countNotEmpty++
  635. }
  636. }
  637. if 0 < len(table.Rows) {
  638. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  639. }
  640. }
  641. }
  642. func (table *Table) calcColDate(col *TableColumn, colIndex int) {
  643. switch col.Calc.Operator {
  644. case CalcOperatorCountAll:
  645. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  646. case CalcOperatorCountValues:
  647. countValues := 0
  648. for _, row := range table.Rows {
  649. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
  650. countValues++
  651. }
  652. }
  653. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  654. case CalcOperatorCountUniqueValues:
  655. countUniqueValues := 0
  656. uniqueValues := map[int64]bool{}
  657. for _, row := range table.Rows {
  658. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
  659. if _, ok := uniqueValues[row.Cells[colIndex].Value.Date.Content]; !ok {
  660. countUniqueValues++
  661. uniqueValues[row.Cells[colIndex].Value.Date.Content] = true
  662. }
  663. }
  664. }
  665. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  666. case CalcOperatorCountEmpty:
  667. countEmpty := 0
  668. for _, row := range table.Rows {
  669. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Date || !row.Cells[colIndex].Value.Date.IsNotEmpty {
  670. countEmpty++
  671. }
  672. }
  673. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  674. case CalcOperatorCountNotEmpty:
  675. countNotEmpty := 0
  676. for _, row := range table.Rows {
  677. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
  678. countNotEmpty++
  679. }
  680. }
  681. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  682. case CalcOperatorPercentEmpty:
  683. countEmpty := 0
  684. for _, row := range table.Rows {
  685. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Date || !row.Cells[colIndex].Value.Date.IsNotEmpty {
  686. countEmpty++
  687. }
  688. }
  689. if 0 < len(table.Rows) {
  690. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  691. }
  692. case CalcOperatorPercentNotEmpty:
  693. countNotEmpty := 0
  694. for _, row := range table.Rows {
  695. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
  696. countNotEmpty++
  697. }
  698. }
  699. if 0 < len(table.Rows) {
  700. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  701. }
  702. case CalcOperatorEarliest:
  703. earliest := int64(0)
  704. var isNotTime, hasEndDate bool
  705. for _, row := range table.Rows {
  706. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
  707. if 0 == earliest || earliest > row.Cells[colIndex].Value.Date.Content {
  708. earliest = row.Cells[colIndex].Value.Date.Content
  709. isNotTime = row.Cells[colIndex].Value.Date.IsNotTime
  710. hasEndDate = row.Cells[colIndex].Value.Date.HasEndDate
  711. }
  712. }
  713. }
  714. if 0 != earliest {
  715. col.Calc.Result = &Value{Date: NewFormattedValueDate(earliest, 0, DateFormatNone, isNotTime, hasEndDate)}
  716. }
  717. case CalcOperatorLatest:
  718. latest := int64(0)
  719. var isNotTime, hasEndDate bool
  720. for _, row := range table.Rows {
  721. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
  722. if 0 == latest || latest < row.Cells[colIndex].Value.Date.Content {
  723. latest = row.Cells[colIndex].Value.Date.Content
  724. isNotTime = row.Cells[colIndex].Value.Date.IsNotTime
  725. hasEndDate = row.Cells[colIndex].Value.Date.HasEndDate
  726. }
  727. }
  728. }
  729. if 0 != latest {
  730. col.Calc.Result = &Value{Date: NewFormattedValueDate(latest, 0, DateFormatNone, isNotTime, hasEndDate)}
  731. }
  732. case CalcOperatorRange:
  733. earliest := int64(0)
  734. latest := int64(0)
  735. var isNotTime, hasEndDate bool
  736. for _, row := range table.Rows {
  737. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Date && row.Cells[colIndex].Value.Date.IsNotEmpty {
  738. if 0 == earliest || earliest > row.Cells[colIndex].Value.Date.Content {
  739. earliest = row.Cells[colIndex].Value.Date.Content
  740. isNotTime = row.Cells[colIndex].Value.Date.IsNotTime
  741. hasEndDate = row.Cells[colIndex].Value.Date.HasEndDate
  742. }
  743. if 0 == latest || latest < row.Cells[colIndex].Value.Date.Content {
  744. latest = row.Cells[colIndex].Value.Date.Content
  745. isNotTime = row.Cells[colIndex].Value.Date.IsNotTime
  746. hasEndDate = row.Cells[colIndex].Value.Date.HasEndDate
  747. }
  748. }
  749. }
  750. if 0 != earliest && 0 != latest {
  751. col.Calc.Result = &Value{Date: NewFormattedValueDate(earliest, latest, DateFormatDuration, isNotTime, hasEndDate)}
  752. }
  753. }
  754. }
  755. func (table *Table) calcColNumber(col *TableColumn, colIndex int) {
  756. switch col.Calc.Operator {
  757. case CalcOperatorCountAll:
  758. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  759. case CalcOperatorCountValues:
  760. countValues := 0
  761. for _, row := range table.Rows {
  762. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  763. countValues++
  764. }
  765. }
  766. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  767. case CalcOperatorCountUniqueValues:
  768. countUniqueValues := 0
  769. uniqueValues := map[float64]bool{}
  770. for _, row := range table.Rows {
  771. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  772. if !uniqueValues[row.Cells[colIndex].Value.Number.Content] {
  773. uniqueValues[row.Cells[colIndex].Value.Number.Content] = true
  774. countUniqueValues++
  775. }
  776. }
  777. }
  778. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  779. case CalcOperatorCountEmpty:
  780. countEmpty := 0
  781. for _, row := range table.Rows {
  782. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Number || !row.Cells[colIndex].Value.Number.IsNotEmpty {
  783. countEmpty++
  784. }
  785. }
  786. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  787. case CalcOperatorCountNotEmpty:
  788. countNotEmpty := 0
  789. for _, row := range table.Rows {
  790. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  791. countNotEmpty++
  792. }
  793. }
  794. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  795. case CalcOperatorPercentEmpty:
  796. countEmpty := 0
  797. for _, row := range table.Rows {
  798. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Number || !row.Cells[colIndex].Value.Number.IsNotEmpty {
  799. countEmpty++
  800. }
  801. }
  802. if 0 < len(table.Rows) {
  803. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  804. }
  805. case CalcOperatorPercentNotEmpty:
  806. countNotEmpty := 0
  807. for _, row := range table.Rows {
  808. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  809. countNotEmpty++
  810. }
  811. }
  812. if 0 < len(table.Rows) {
  813. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  814. }
  815. case CalcOperatorSum:
  816. sum := 0.0
  817. for _, row := range table.Rows {
  818. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  819. sum += row.Cells[colIndex].Value.Number.Content
  820. }
  821. }
  822. col.Calc.Result = &Value{Number: NewFormattedValueNumber(sum, col.NumberFormat)}
  823. case CalcOperatorAverage:
  824. sum := 0.0
  825. count := 0
  826. for _, row := range table.Rows {
  827. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  828. sum += row.Cells[colIndex].Value.Number.Content
  829. count++
  830. }
  831. }
  832. if 0 != count {
  833. col.Calc.Result = &Value{Number: NewFormattedValueNumber(sum/float64(count), col.NumberFormat)}
  834. }
  835. case CalcOperatorMedian:
  836. values := []float64{}
  837. for _, row := range table.Rows {
  838. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  839. values = append(values, row.Cells[colIndex].Value.Number.Content)
  840. }
  841. }
  842. sort.Float64s(values)
  843. if len(values) > 0 {
  844. if len(values)%2 == 0 {
  845. col.Calc.Result = &Value{Number: NewFormattedValueNumber((values[len(values)/2-1]+values[len(values)/2])/2, col.NumberFormat)}
  846. } else {
  847. col.Calc.Result = &Value{Number: NewFormattedValueNumber(values[len(values)/2], col.NumberFormat)}
  848. }
  849. }
  850. case CalcOperatorMin:
  851. minVal := math.MaxFloat64
  852. for _, row := range table.Rows {
  853. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  854. if row.Cells[colIndex].Value.Number.Content < minVal {
  855. minVal = row.Cells[colIndex].Value.Number.Content
  856. }
  857. }
  858. }
  859. if math.MaxFloat64 != minVal {
  860. col.Calc.Result = &Value{Number: NewFormattedValueNumber(minVal, col.NumberFormat)}
  861. }
  862. case CalcOperatorMax:
  863. maxVal := -math.MaxFloat64
  864. for _, row := range table.Rows {
  865. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  866. if row.Cells[colIndex].Value.Number.Content > maxVal {
  867. maxVal = row.Cells[colIndex].Value.Number.Content
  868. }
  869. }
  870. }
  871. if -math.MaxFloat64 != maxVal {
  872. col.Calc.Result = &Value{Number: NewFormattedValueNumber(maxVal, col.NumberFormat)}
  873. }
  874. case CalcOperatorRange:
  875. minVal := math.MaxFloat64
  876. maxVal := -math.MaxFloat64
  877. for _, row := range table.Rows {
  878. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Number && row.Cells[colIndex].Value.Number.IsNotEmpty {
  879. if row.Cells[colIndex].Value.Number.Content < minVal {
  880. minVal = row.Cells[colIndex].Value.Number.Content
  881. }
  882. if row.Cells[colIndex].Value.Number.Content > maxVal {
  883. maxVal = row.Cells[colIndex].Value.Number.Content
  884. }
  885. }
  886. }
  887. if math.MaxFloat64 != minVal && -math.MaxFloat64 != maxVal {
  888. col.Calc.Result = &Value{Number: NewFormattedValueNumber(maxVal-minVal, col.NumberFormat)}
  889. }
  890. }
  891. }
  892. func (table *Table) calcColText(col *TableColumn, colIndex int) {
  893. switch col.Calc.Operator {
  894. case CalcOperatorCountAll:
  895. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  896. case CalcOperatorCountValues:
  897. countValues := 0
  898. for _, row := range table.Rows {
  899. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Text && "" != row.Cells[colIndex].Value.Text.Content {
  900. countValues++
  901. }
  902. }
  903. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  904. case CalcOperatorCountUniqueValues:
  905. countUniqueValues := 0
  906. uniqueValues := map[string]bool{}
  907. for _, row := range table.Rows {
  908. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Text && "" != row.Cells[colIndex].Value.Text.Content {
  909. if !uniqueValues[row.Cells[colIndex].Value.Text.Content] {
  910. uniqueValues[row.Cells[colIndex].Value.Text.Content] = true
  911. countUniqueValues++
  912. }
  913. }
  914. }
  915. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  916. case CalcOperatorCountEmpty:
  917. countEmpty := 0
  918. for _, row := range table.Rows {
  919. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Text || "" == row.Cells[colIndex].Value.Text.Content {
  920. countEmpty++
  921. }
  922. }
  923. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  924. case CalcOperatorCountNotEmpty:
  925. countNotEmpty := 0
  926. for _, row := range table.Rows {
  927. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Text && "" != row.Cells[colIndex].Value.Text.Content {
  928. countNotEmpty++
  929. }
  930. }
  931. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  932. case CalcOperatorPercentEmpty:
  933. countEmpty := 0
  934. for _, row := range table.Rows {
  935. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Text || "" == row.Cells[colIndex].Value.Text.Content {
  936. countEmpty++
  937. }
  938. }
  939. if 0 < len(table.Rows) {
  940. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  941. }
  942. case CalcOperatorPercentNotEmpty:
  943. countNotEmpty := 0
  944. for _, row := range table.Rows {
  945. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Text && "" != row.Cells[colIndex].Value.Text.Content {
  946. countNotEmpty++
  947. }
  948. }
  949. if 0 < len(table.Rows) {
  950. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  951. }
  952. }
  953. }
  954. func (table *Table) calcColURL(col *TableColumn, colIndex int) {
  955. switch col.Calc.Operator {
  956. case CalcOperatorCountAll:
  957. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  958. case CalcOperatorCountValues:
  959. countValues := 0
  960. for _, row := range table.Rows {
  961. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.URL && "" != row.Cells[colIndex].Value.URL.Content {
  962. countValues++
  963. }
  964. }
  965. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  966. case CalcOperatorCountUniqueValues:
  967. countUniqueValues := 0
  968. uniqueValues := map[string]bool{}
  969. for _, row := range table.Rows {
  970. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.URL && "" != row.Cells[colIndex].Value.URL.Content {
  971. if !uniqueValues[row.Cells[colIndex].Value.URL.Content] {
  972. uniqueValues[row.Cells[colIndex].Value.URL.Content] = true
  973. countUniqueValues++
  974. }
  975. }
  976. }
  977. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  978. case CalcOperatorCountEmpty:
  979. countEmpty := 0
  980. for _, row := range table.Rows {
  981. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.URL || "" == row.Cells[colIndex].Value.URL.Content {
  982. countEmpty++
  983. }
  984. }
  985. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  986. case CalcOperatorCountNotEmpty:
  987. countNotEmpty := 0
  988. for _, row := range table.Rows {
  989. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.URL && "" != row.Cells[colIndex].Value.URL.Content {
  990. countNotEmpty++
  991. }
  992. }
  993. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  994. case CalcOperatorPercentEmpty:
  995. countEmpty := 0
  996. for _, row := range table.Rows {
  997. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.URL || "" == row.Cells[colIndex].Value.URL.Content {
  998. countEmpty++
  999. }
  1000. }
  1001. if 0 < len(table.Rows) {
  1002. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1003. }
  1004. case CalcOperatorPercentNotEmpty:
  1005. countNotEmpty := 0
  1006. for _, row := range table.Rows {
  1007. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.URL && "" != row.Cells[colIndex].Value.URL.Content {
  1008. countNotEmpty++
  1009. }
  1010. }
  1011. if 0 < len(table.Rows) {
  1012. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1013. }
  1014. }
  1015. }
  1016. func (table *Table) calcColEmail(col *TableColumn, colIndex int) {
  1017. switch col.Calc.Operator {
  1018. case CalcOperatorCountAll:
  1019. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  1020. case CalcOperatorCountValues:
  1021. countValues := 0
  1022. for _, row := range table.Rows {
  1023. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Email && "" != row.Cells[colIndex].Value.Email.Content {
  1024. countValues++
  1025. }
  1026. }
  1027. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  1028. case CalcOperatorCountUniqueValues:
  1029. countUniqueValues := 0
  1030. uniqueValues := map[string]bool{}
  1031. for _, row := range table.Rows {
  1032. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Email && "" != row.Cells[colIndex].Value.Email.Content {
  1033. if !uniqueValues[row.Cells[colIndex].Value.Email.Content] {
  1034. uniqueValues[row.Cells[colIndex].Value.Email.Content] = true
  1035. countUniqueValues++
  1036. }
  1037. }
  1038. }
  1039. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  1040. case CalcOperatorCountEmpty:
  1041. countEmpty := 0
  1042. for _, row := range table.Rows {
  1043. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Email || "" == row.Cells[colIndex].Value.Email.Content {
  1044. countEmpty++
  1045. }
  1046. }
  1047. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  1048. case CalcOperatorCountNotEmpty:
  1049. countNotEmpty := 0
  1050. for _, row := range table.Rows {
  1051. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Email && "" != row.Cells[colIndex].Value.Email.Content {
  1052. countNotEmpty++
  1053. }
  1054. }
  1055. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  1056. case CalcOperatorPercentEmpty:
  1057. countEmpty := 0
  1058. for _, row := range table.Rows {
  1059. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Email || "" == row.Cells[colIndex].Value.Email.Content {
  1060. countEmpty++
  1061. }
  1062. }
  1063. if 0 < len(table.Rows) {
  1064. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1065. }
  1066. case CalcOperatorPercentNotEmpty:
  1067. countNotEmpty := 0
  1068. for _, row := range table.Rows {
  1069. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Email && "" != row.Cells[colIndex].Value.Email.Content {
  1070. countNotEmpty++
  1071. }
  1072. }
  1073. if 0 < len(table.Rows) {
  1074. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1075. }
  1076. }
  1077. }
  1078. func (table *Table) calcColPhone(col *TableColumn, colIndex int) {
  1079. switch col.Calc.Operator {
  1080. case CalcOperatorCountAll:
  1081. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  1082. case CalcOperatorCountValues:
  1083. countValues := 0
  1084. for _, row := range table.Rows {
  1085. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Phone && "" != row.Cells[colIndex].Value.Phone.Content {
  1086. countValues++
  1087. }
  1088. }
  1089. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  1090. case CalcOperatorCountUniqueValues:
  1091. countUniqueValues := 0
  1092. uniqueValues := map[string]bool{}
  1093. for _, row := range table.Rows {
  1094. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Phone && "" != row.Cells[colIndex].Value.Phone.Content {
  1095. if !uniqueValues[row.Cells[colIndex].Value.Phone.Content] {
  1096. uniqueValues[row.Cells[colIndex].Value.Phone.Content] = true
  1097. countUniqueValues++
  1098. }
  1099. }
  1100. }
  1101. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  1102. case CalcOperatorCountEmpty:
  1103. countEmpty := 0
  1104. for _, row := range table.Rows {
  1105. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Phone || "" == row.Cells[colIndex].Value.Phone.Content {
  1106. countEmpty++
  1107. }
  1108. }
  1109. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  1110. case CalcOperatorCountNotEmpty:
  1111. countNotEmpty := 0
  1112. for _, row := range table.Rows {
  1113. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Phone && "" != row.Cells[colIndex].Value.Phone.Content {
  1114. countNotEmpty++
  1115. }
  1116. }
  1117. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  1118. case CalcOperatorPercentEmpty:
  1119. countEmpty := 0
  1120. for _, row := range table.Rows {
  1121. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Phone || "" == row.Cells[colIndex].Value.Phone.Content {
  1122. countEmpty++
  1123. }
  1124. }
  1125. if 0 < len(table.Rows) {
  1126. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1127. }
  1128. case CalcOperatorPercentNotEmpty:
  1129. countNotEmpty := 0
  1130. for _, row := range table.Rows {
  1131. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Phone && "" != row.Cells[colIndex].Value.Phone.Content {
  1132. countNotEmpty++
  1133. }
  1134. }
  1135. if 0 < len(table.Rows) {
  1136. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1137. }
  1138. }
  1139. }
  1140. func (table *Table) calcColBlock(col *TableColumn, colIndex int) {
  1141. switch col.Calc.Operator {
  1142. case CalcOperatorCountAll:
  1143. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  1144. case CalcOperatorCountValues:
  1145. countValues := 0
  1146. for _, row := range table.Rows {
  1147. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Block && "" != row.Cells[colIndex].Value.Block.Content {
  1148. countValues++
  1149. }
  1150. }
  1151. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  1152. case CalcOperatorCountUniqueValues:
  1153. countUniqueValues := 0
  1154. uniqueValues := map[string]bool{}
  1155. for _, row := range table.Rows {
  1156. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Block && "" != row.Cells[colIndex].Value.Block.Content {
  1157. if !uniqueValues[row.Cells[colIndex].Value.Block.Content] {
  1158. uniqueValues[row.Cells[colIndex].Value.Block.Content] = true
  1159. countUniqueValues++
  1160. }
  1161. }
  1162. }
  1163. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  1164. case CalcOperatorCountEmpty:
  1165. countEmpty := 0
  1166. for _, row := range table.Rows {
  1167. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Block || "" == row.Cells[colIndex].Value.Block.Content {
  1168. countEmpty++
  1169. }
  1170. }
  1171. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  1172. case CalcOperatorCountNotEmpty:
  1173. countNotEmpty := 0
  1174. for _, row := range table.Rows {
  1175. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Block && "" != row.Cells[colIndex].Value.Block.Content {
  1176. countNotEmpty++
  1177. }
  1178. }
  1179. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  1180. case CalcOperatorPercentEmpty:
  1181. countEmpty := 0
  1182. for _, row := range table.Rows {
  1183. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Block || "" == row.Cells[colIndex].Value.Block.Content {
  1184. countEmpty++
  1185. }
  1186. }
  1187. if 0 < len(table.Rows) {
  1188. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1189. }
  1190. case CalcOperatorPercentNotEmpty:
  1191. countNotEmpty := 0
  1192. for _, row := range table.Rows {
  1193. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Block && "" != row.Cells[colIndex].Value.Block.Content {
  1194. countNotEmpty++
  1195. }
  1196. }
  1197. if 0 < len(table.Rows) {
  1198. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1199. }
  1200. }
  1201. }
  1202. func (table *Table) calcColCreated(col *TableColumn, colIndex int) {
  1203. switch col.Calc.Operator {
  1204. case CalcOperatorCountAll:
  1205. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  1206. case CalcOperatorCountValues:
  1207. countValues := 0
  1208. for _, row := range table.Rows {
  1209. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Created {
  1210. countValues++
  1211. }
  1212. }
  1213. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  1214. case CalcOperatorCountUniqueValues:
  1215. countUniqueValues := 0
  1216. uniqueValues := map[int64]bool{}
  1217. for _, row := range table.Rows {
  1218. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Created {
  1219. if _, ok := uniqueValues[row.Cells[colIndex].Value.Created.Content]; !ok {
  1220. countUniqueValues++
  1221. uniqueValues[row.Cells[colIndex].Value.Created.Content] = true
  1222. }
  1223. }
  1224. }
  1225. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  1226. case CalcOperatorCountEmpty:
  1227. countEmpty := 0
  1228. for _, row := range table.Rows {
  1229. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Created {
  1230. countEmpty++
  1231. }
  1232. }
  1233. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  1234. case CalcOperatorCountNotEmpty:
  1235. countNotEmpty := 0
  1236. for _, row := range table.Rows {
  1237. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Created {
  1238. countNotEmpty++
  1239. }
  1240. }
  1241. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  1242. case CalcOperatorPercentEmpty:
  1243. countEmpty := 0
  1244. for _, row := range table.Rows {
  1245. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Created {
  1246. countEmpty++
  1247. }
  1248. }
  1249. if 0 < len(table.Rows) {
  1250. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1251. }
  1252. case CalcOperatorPercentNotEmpty:
  1253. countNotEmpty := 0
  1254. for _, row := range table.Rows {
  1255. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Created {
  1256. countNotEmpty++
  1257. }
  1258. }
  1259. if 0 < len(table.Rows) {
  1260. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1261. }
  1262. case CalcOperatorEarliest:
  1263. earliest := int64(0)
  1264. for _, row := range table.Rows {
  1265. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Created {
  1266. if 0 == earliest || earliest > row.Cells[colIndex].Value.Created.Content {
  1267. earliest = row.Cells[colIndex].Value.Created.Content
  1268. }
  1269. }
  1270. }
  1271. if 0 != earliest {
  1272. col.Calc.Result = &Value{Created: NewFormattedValueCreated(earliest, 0, CreatedFormatNone)}
  1273. }
  1274. case CalcOperatorLatest:
  1275. latest := int64(0)
  1276. for _, row := range table.Rows {
  1277. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Created {
  1278. if 0 == latest || latest < row.Cells[colIndex].Value.Created.Content {
  1279. latest = row.Cells[colIndex].Value.Created.Content
  1280. }
  1281. }
  1282. }
  1283. if 0 != latest {
  1284. col.Calc.Result = &Value{Created: NewFormattedValueCreated(latest, 0, CreatedFormatNone)}
  1285. }
  1286. case CalcOperatorRange:
  1287. earliest := int64(0)
  1288. latest := int64(0)
  1289. for _, row := range table.Rows {
  1290. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Created {
  1291. if 0 == earliest || earliest > row.Cells[colIndex].Value.Created.Content {
  1292. earliest = row.Cells[colIndex].Value.Created.Content
  1293. }
  1294. if 0 == latest || latest < row.Cells[colIndex].Value.Created.Content {
  1295. latest = row.Cells[colIndex].Value.Created.Content
  1296. }
  1297. }
  1298. }
  1299. if 0 != earliest && 0 != latest {
  1300. col.Calc.Result = &Value{Created: NewFormattedValueCreated(earliest, latest, CreatedFormatDuration)}
  1301. }
  1302. }
  1303. }
  1304. func (table *Table) calcColUpdated(col *TableColumn, colIndex int) {
  1305. switch col.Calc.Operator {
  1306. case CalcOperatorCountAll:
  1307. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  1308. case CalcOperatorCountValues:
  1309. countValues := 0
  1310. for _, row := range table.Rows {
  1311. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Updated && row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1312. countValues++
  1313. }
  1314. }
  1315. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  1316. case CalcOperatorCountUniqueValues:
  1317. countUniqueValues := 0
  1318. uniqueValues := map[int64]bool{}
  1319. for _, row := range table.Rows {
  1320. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Updated && row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1321. if _, ok := uniqueValues[row.Cells[colIndex].Value.Updated.Content]; !ok {
  1322. countUniqueValues++
  1323. uniqueValues[row.Cells[colIndex].Value.Updated.Content] = true
  1324. }
  1325. }
  1326. }
  1327. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  1328. case CalcOperatorCountEmpty:
  1329. countEmpty := 0
  1330. for _, row := range table.Rows {
  1331. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Updated || !row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1332. countEmpty++
  1333. }
  1334. }
  1335. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  1336. case CalcOperatorCountNotEmpty:
  1337. countNotEmpty := 0
  1338. for _, row := range table.Rows {
  1339. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Updated && row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1340. countNotEmpty++
  1341. }
  1342. }
  1343. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  1344. case CalcOperatorPercentEmpty:
  1345. countEmpty := 0
  1346. for _, row := range table.Rows {
  1347. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Updated || !row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1348. countEmpty++
  1349. }
  1350. }
  1351. if 0 < len(table.Rows) {
  1352. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1353. }
  1354. case CalcOperatorPercentNotEmpty:
  1355. countNotEmpty := 0
  1356. for _, row := range table.Rows {
  1357. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Updated && row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1358. countNotEmpty++
  1359. }
  1360. }
  1361. if 0 < len(table.Rows) {
  1362. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1363. }
  1364. case CalcOperatorEarliest:
  1365. earliest := int64(0)
  1366. for _, row := range table.Rows {
  1367. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Updated && row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1368. if 0 == earliest || earliest > row.Cells[colIndex].Value.Updated.Content {
  1369. earliest = row.Cells[colIndex].Value.Updated.Content
  1370. }
  1371. }
  1372. }
  1373. if 0 != earliest {
  1374. col.Calc.Result = &Value{Updated: NewFormattedValueUpdated(earliest, 0, UpdatedFormatNone)}
  1375. }
  1376. case CalcOperatorLatest:
  1377. latest := int64(0)
  1378. for _, row := range table.Rows {
  1379. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Updated && row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1380. if 0 == latest || latest < row.Cells[colIndex].Value.Updated.Content {
  1381. latest = row.Cells[colIndex].Value.Updated.Content
  1382. }
  1383. }
  1384. }
  1385. if 0 != latest {
  1386. col.Calc.Result = &Value{Updated: NewFormattedValueUpdated(latest, 0, UpdatedFormatNone)}
  1387. }
  1388. case CalcOperatorRange:
  1389. earliest := int64(0)
  1390. latest := int64(0)
  1391. for _, row := range table.Rows {
  1392. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Updated && row.Cells[colIndex].Value.Updated.IsNotEmpty {
  1393. if 0 == earliest || earliest > row.Cells[colIndex].Value.Updated.Content {
  1394. earliest = row.Cells[colIndex].Value.Updated.Content
  1395. }
  1396. if 0 == latest || latest < row.Cells[colIndex].Value.Updated.Content {
  1397. latest = row.Cells[colIndex].Value.Updated.Content
  1398. }
  1399. }
  1400. }
  1401. if 0 != earliest && 0 != latest {
  1402. col.Calc.Result = &Value{Updated: NewFormattedValueUpdated(earliest, latest, UpdatedFormatDuration)}
  1403. }
  1404. }
  1405. }
  1406. func (table *Table) calcColCheckbox(col *TableColumn, colIndex int) {
  1407. switch col.Calc.Operator {
  1408. case CalcOperatorCountAll:
  1409. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  1410. case CalcOperatorChecked:
  1411. countChecked := 0
  1412. for _, row := range table.Rows {
  1413. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Checkbox && row.Cells[colIndex].Value.Checkbox.Checked {
  1414. countChecked++
  1415. }
  1416. }
  1417. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countChecked), NumberFormatNone)}
  1418. case CalcOperatorUnchecked:
  1419. countUnchecked := 0
  1420. for _, row := range table.Rows {
  1421. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Checkbox && !row.Cells[colIndex].Value.Checkbox.Checked {
  1422. countUnchecked++
  1423. }
  1424. }
  1425. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUnchecked), NumberFormatNone)}
  1426. case CalcOperatorPercentChecked:
  1427. countChecked := 0
  1428. for _, row := range table.Rows {
  1429. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Checkbox && row.Cells[colIndex].Value.Checkbox.Checked {
  1430. countChecked++
  1431. }
  1432. }
  1433. if 0 < len(table.Rows) {
  1434. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countChecked)/float64(len(table.Rows)), NumberFormatPercent)}
  1435. }
  1436. case CalcOperatorPercentUnchecked:
  1437. countUnchecked := 0
  1438. for _, row := range table.Rows {
  1439. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Checkbox && !row.Cells[colIndex].Value.Checkbox.Checked {
  1440. countUnchecked++
  1441. }
  1442. }
  1443. if 0 < len(table.Rows) {
  1444. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUnchecked)/float64(len(table.Rows)), NumberFormatPercent)}
  1445. }
  1446. }
  1447. }
  1448. func (table *Table) calcColRelation(col *TableColumn, colIndex int) {
  1449. switch col.Calc.Operator {
  1450. case CalcOperatorCountAll:
  1451. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  1452. case CalcOperatorCountValues:
  1453. countValues := 0
  1454. for _, row := range table.Rows {
  1455. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Relation {
  1456. countValues++
  1457. }
  1458. }
  1459. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  1460. case CalcOperatorCountUniqueValues:
  1461. countUniqueValues := 0
  1462. uniqueValues := map[string]bool{}
  1463. for _, row := range table.Rows {
  1464. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Relation {
  1465. for _, id := range row.Cells[colIndex].Value.Relation.BlockIDs {
  1466. if !uniqueValues[id] {
  1467. uniqueValues[id] = true
  1468. countUniqueValues++
  1469. }
  1470. }
  1471. }
  1472. }
  1473. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  1474. case CalcOperatorCountEmpty:
  1475. countEmpty := 0
  1476. for _, row := range table.Rows {
  1477. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Relation || 0 == len(row.Cells[colIndex].Value.Relation.BlockIDs) {
  1478. countEmpty++
  1479. }
  1480. }
  1481. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  1482. case CalcOperatorCountNotEmpty:
  1483. countNotEmpty := 0
  1484. for _, row := range table.Rows {
  1485. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Relation && 0 < len(row.Cells[colIndex].Value.Relation.BlockIDs) {
  1486. countNotEmpty++
  1487. }
  1488. }
  1489. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  1490. case CalcOperatorPercentEmpty:
  1491. countEmpty := 0
  1492. for _, row := range table.Rows {
  1493. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Relation || 0 == len(row.Cells[colIndex].Value.Relation.BlockIDs) {
  1494. countEmpty++
  1495. }
  1496. }
  1497. if 0 < len(table.Rows) {
  1498. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1499. }
  1500. case CalcOperatorPercentNotEmpty:
  1501. countNotEmpty := 0
  1502. for _, row := range table.Rows {
  1503. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Relation && 0 < len(row.Cells[colIndex].Value.Relation.BlockIDs) {
  1504. countNotEmpty++
  1505. }
  1506. }
  1507. if 0 < len(table.Rows) {
  1508. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1509. }
  1510. }
  1511. }
  1512. func (table *Table) calcColRollup(col *TableColumn, colIndex int) {
  1513. switch col.Calc.Operator {
  1514. case CalcOperatorCountAll:
  1515. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(len(table.Rows)), NumberFormatNone)}
  1516. case CalcOperatorCountValues:
  1517. countValues := 0
  1518. for _, row := range table.Rows {
  1519. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup {
  1520. countValues++
  1521. }
  1522. }
  1523. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countValues), NumberFormatNone)}
  1524. case CalcOperatorCountUniqueValues:
  1525. countUniqueValues := 0
  1526. uniqueValues := map[string]bool{}
  1527. for _, row := range table.Rows {
  1528. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup {
  1529. for _, content := range row.Cells[colIndex].Value.Rollup.Contents {
  1530. if !uniqueValues[content.String()] {
  1531. uniqueValues[content.String()] = true
  1532. countUniqueValues++
  1533. }
  1534. }
  1535. }
  1536. }
  1537. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countUniqueValues), NumberFormatNone)}
  1538. case CalcOperatorCountEmpty:
  1539. countEmpty := 0
  1540. for _, row := range table.Rows {
  1541. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Rollup || 0 == len(row.Cells[colIndex].Value.Rollup.Contents) {
  1542. countEmpty++
  1543. }
  1544. }
  1545. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty), NumberFormatNone)}
  1546. case CalcOperatorCountNotEmpty:
  1547. countNotEmpty := 0
  1548. for _, row := range table.Rows {
  1549. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) {
  1550. countNotEmpty++
  1551. }
  1552. }
  1553. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty), NumberFormatNone)}
  1554. case CalcOperatorPercentEmpty:
  1555. countEmpty := 0
  1556. for _, row := range table.Rows {
  1557. if nil == row.Cells[colIndex] || nil == row.Cells[colIndex].Value || nil == row.Cells[colIndex].Value.Rollup || 0 == len(row.Cells[colIndex].Value.Rollup.Contents) {
  1558. countEmpty++
  1559. }
  1560. }
  1561. if 0 < len(table.Rows) {
  1562. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1563. }
  1564. case CalcOperatorPercentNotEmpty:
  1565. countNotEmpty := 0
  1566. for _, row := range table.Rows {
  1567. if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) {
  1568. countNotEmpty++
  1569. }
  1570. }
  1571. if 0 < len(table.Rows) {
  1572. col.Calc.Result = &Value{Number: NewFormattedValueNumber(float64(countNotEmpty)/float64(len(table.Rows)), NumberFormatPercent)}
  1573. }
  1574. }
  1575. }