1package sheet23import (4 "encoding/json"5 "fmt"6)78// A chart, a pivot table and a filter column's criteria as the lines of9// the file that hold them, to be written and read one at a time: macros10// answer the dialogs that make them with these (see11// docs/reference/macro-api.md), as they answer the rules panel with a12// rule's line.1314// JSON is the chart as a line of the file.15func (c Chart) JSON() string {16 raw, _ := encodeChart(c)17 return string(raw)18}1920// ParseChart reads a chart written by JSON.21func ParseChart(line string) (Chart, error) {22 var fc fileChart23 if err := json.Unmarshal([]byte(line), &fc); err != nil {24 return Chart{}, err25 }26 return decodeChart(fc)27}2829// JSON is the pivot table's definition as a line of the file.30func (p Pivot) JSON() string {31 raw, _ := encodePivot(&p)32 return string(raw)33}3435// ParsePivot reads a pivot table's definition written by JSON.36func ParsePivot(line string) (Pivot, error) {37 var fp filePivot38 if err := json.Unmarshal([]byte(line), &fp); err != nil {39 return Pivot{}, err40 }41 p, err := decodePivot(fp)42 if err != nil {43 return Pivot{}, err44 }45 return *p, nil46}4748// JSON is the criteria as a filter column's are written in the file:49// the values hidden, and a condition and its value.50func (cr Criteria) JSON() string {51 raw, _ := json.Marshal(fileCriteria{Hidden: cr.Hidden, Condition: cr.Cond.Op.String(), Value: cr.Cond.Arg})52 return string(raw)53}5455// ParseCriteria reads criteria written by JSON.56func ParseCriteria(line string) (Criteria, error) {57 var fc fileCriteria58 if err := json.Unmarshal([]byte(line), &fc); err != nil {59 return Criteria{}, err60 }61 op, ok := ParseCondOp(fc.Condition)62 if !ok {63 return Criteria{}, fmt.Errorf("unknown filter condition %q", fc.Condition)64 }65 cr := Criteria{Hidden: fc.Hidden, Cond: Condition{Op: op, Arg: fc.Value}}66 if !op.TakesArg() {67 cr.Cond.Arg = ""68 }69 return cr, nil70}