spreadsheet_ods_formula/
lookup.rs

1//!
2//! Data lookup functions.
3//!
4
5pub use crate::generated::lookup::*;
6
7use crate::{Any, FnAny2, FnAnyVar, Reference, Scalar, Text};
8use std::fmt::Write;
9
10/// Parameter for ADDRESS().
11#[derive(Debug)]
12pub enum AddressAbs {
13    RowAbsColAbs,
14    RowAbsColRel,
15    RowRelColAbs,
16    RowRelColRel,
17}
18
19impl Any for AddressAbs {
20    fn formula(&self, buf: &mut String) {
21        let _ = write!(
22            buf,
23            "{}",
24            match self {
25                AddressAbs::RowAbsColAbs => 1,
26                AddressAbs::RowAbsColRel => 2,
27                AddressAbs::RowRelColAbs => 3,
28                AddressAbs::RowRelColRel => 4,
29            }
30        );
31    }
32}
33
34/// Parameter for MATCH().
35#[derive(Debug)]
36pub enum MatchType {
37    MaxInDescendingList,
38    ExactMatch,
39    MaxInAscendingList,
40}
41
42impl Any for MatchType {
43    fn formula(&self, buf: &mut String) {
44        let _ = write!(
45            buf,
46            "{}",
47            match self {
48                MatchType::MaxInDescendingList => -1,
49                MatchType::ExactMatch => 0,
50                MatchType::MaxInAscendingList => 1,
51            }
52        );
53    }
54}
55
56/// Return a value from a data pilot table.
57#[inline]
58pub fn getpivotdata<A: Text, B: Reference>(datafield: A, table: B) -> FnAny2<A, B> {
59    FnAny2("GETPIVOTDATA", datafield, table)
60}
61
62/// Return a value from a data pilot table.
63#[inline]
64pub fn getpivotdata_fields<
65    A: Text + 'static,
66    B: Reference + 'static,
67    F: Text + 'static,
68    S: Scalar + 'static,
69    const N: usize,
70>(
71    datafield: A,
72    table: B,
73    fields: [(F, S); N],
74) -> FnAnyVar {
75    let mut param: Vec<Box<dyn Any>> = Vec::new();
76
77    param.push(Box::new(datafield));
78    param.push(Box::new(table));
79
80    for (n, sc) in fields {
81        param.push(Box::new(n));
82        param.push(Box::new(sc));
83    }
84
85    FnAnyVar("GETPIVOTDATA", param)
86}