|
1 | 1 | [[formuals]] |
2 | | -== Formulas |
| 2 | +== Formulas |
| 3 | + |
| 4 | +The Groovy Excel Builder supports formulas in excel documents by providing an easy way to create formulas with support for helper methods to get cell references |
| 5 | + |
| 6 | +=== Simple String |
| 7 | + |
| 8 | +You can specify the exact text of your formula using the example below |
| 9 | + |
| 10 | +[source,groovy] |
| 11 | +---- |
| 12 | +ExcelBuilder.build { |
| 13 | + sheet { |
| 14 | + row { |
| 15 | + formula("SUM(1,2)") |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | +---- |
| 20 | + |
| 21 | +=== Closure Customization |
| 22 | + |
| 23 | +An additional way to create a formula is to supply a closure to the `formula` method. The provided closure will have access to a few methods to make referencing cells easier. Whatever gets returned from the closure will be set as the formula |
| 24 | + |
| 25 | +[source,groovy] |
| 26 | +---- |
| 27 | +ExcelBuilder.build { |
| 28 | + sheet { |
| 29 | + row { |
| 30 | + formula { |
| 31 | + "SUM(1,2)" |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | +---- |
| 37 | + |
| 38 | +==== Exact Cell Reference |
| 39 | + |
| 40 | +Inside of the forumla closure you can get a reference to an exact cell by providing the index of the row and column |
| 41 | + |
| 42 | +[source,groovy] |
| 43 | +---- |
| 44 | +... |
| 45 | +formula { |
| 46 | + "SUM(${exactCell(0,1)},2)" |
| 47 | +} |
| 48 | +... |
| 49 | +---- |
| 50 | + |
| 51 | +The call to `exactCell` will return the string "A2" because the `0` represents the column index ("A") and the `1` represents the row index ("2"). |
| 52 | + |
| 53 | +If you have previously defined columns, you can reference exact cells based on the column id. |
| 54 | + |
| 55 | +[source,groovy] |
| 56 | +---- |
| 57 | +ExcelBuilder.build { |
| 58 | + sheet { |
| 59 | + columns { |
| 60 | + column("Foo", "foo") |
| 61 | + } |
| 62 | + row { |
| 63 | + formula { |
| 64 | + "SUM(${exactCell("foo")}, ${exactCell("foo", 1)})" |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +---- |
| 70 | + |
| 71 | +The first call to exact cell will return "A1" because the `foo` column is in column "A" in the first row. The second call to exact cell is also specifying a row index and will return "A2" because `foo` is column "A" and the row index of `1` resolves to row "2". |
| 72 | + |
| 73 | +==== Relative Cell References |
| 74 | + |
| 75 | +In addition to retrieving exact cell references, you can also retrieve references relative to the formula |
| 76 | + |
| 77 | +[source,groovy] |
| 78 | +---- |
| 79 | +ExcelBuilder.build { |
| 80 | + sheet { |
| 81 | + row() |
| 82 | + row { |
| 83 | + cell("A2") |
| 84 | + formula { |
| 85 | + "SUM(${relativeCell(-1)}, ${relativeCell(-1, -1)})" |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +---- |
| 91 | + |
| 92 | +The first call to `relativeCell` will reference the cell in the previous column in the same row. In that example that would be "A2". The second call to `relativeCell` will resolve to "A1" because the first argument indicates to go back 1 column and the second argument says to go back one row. The formula will be put in cell "B2", so minus one column and minus one row would be "A1". |
| 93 | + |
| 94 | +==== Anchoring Cells |
| 95 | + |
| 96 | +Once you have a reference to any cell, you can anchor the column or row or both |
| 97 | + |
| 98 | +[source,groovy] |
| 99 | +---- |
| 100 | +... |
| 101 | +
|
| 102 | +formula { |
| 103 | + "SUM(${exactCell(0,1).anchorColumn()}, ${exactCell(0,2).anchorRow()}, ${exactCell(0,3).anchor()})" |
| 104 | +} |
| 105 | +... |
| 106 | +---- |
| 107 | + |
| 108 | +The resulting formula will be |
| 109 | + |
| 110 | +`SUM($A2, A$3, $A$4)` |
0 commit comments