diff --git a/README.md b/README.md index bcd5a2b..d820793 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Note the list is incomplete when compared to Excel and Sheets and should be cons - cell - cellFormattedValue - cellFormatter +- cellFormula - cellParser - cellStyle - cellValue diff --git a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionCellFormula.java b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionCellFormula.java new file mode 100644 index 0000000..edf87ca --- /dev/null +++ b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionCellFormula.java @@ -0,0 +1,48 @@ +/* + * Copyright 2022 Miroslav Pokorny (github.com/mP1) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package walkingkooka.spreadsheet.expression.function; + +import walkingkooka.spreadsheet.SpreadsheetCell; +import walkingkooka.spreadsheet.SpreadsheetFormula; + +/** + * A custom function that retrieves the {@link SpreadsheetFormula} for the current cell. + *
+ * This function exists primarily to support filtering cells using a predicate of the current cell. + */ +final class SpreadsheetExpressionFunctionCellFormula extends SpreadsheetExpressionFunctionCell { + + /** + * Singleton + */ + final static SpreadsheetExpressionFunctionCellFormula INSTANCE = new SpreadsheetExpressionFunctionCellFormula(); + + private SpreadsheetExpressionFunctionCellFormula() { + super("cellFormula"); + } + + @Override + public Class returnType() { + return SpreadsheetFormula.class; + } + + @Override + SpreadsheetFormula extractCellPropertyOrNull(final SpreadsheetCell cell) { + return cell.formula(); + } +} diff --git a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionProviders.java b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionProviders.java index a1e64cf..f5830ea 100644 --- a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionProviders.java +++ b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionProviders.java @@ -58,6 +58,7 @@ public static ExpressionFunctionProvider expressionFunctionProvider(final CaseSe SpreadsheetExpressionFunctions.cell(), SpreadsheetExpressionFunctions.cellFormattedValue(), SpreadsheetExpressionFunctions.cellFormatter(), + SpreadsheetExpressionFunctions.cellFormula(), SpreadsheetExpressionFunctions.cellParser(), SpreadsheetExpressionFunctions.cellStyle(), SpreadsheetExpressionFunctions.cellValue(), diff --git a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctions.java b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctions.java index 88b3183..eafa852 100644 --- a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctions.java +++ b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctions.java @@ -21,6 +21,7 @@ import walkingkooka.net.AbsoluteUrl; import walkingkooka.reflect.PublicStaticHelper; import walkingkooka.spreadsheet.SpreadsheetError; +import walkingkooka.spreadsheet.SpreadsheetFormula; import walkingkooka.spreadsheet.expression.SpreadsheetExpressionEvaluationContext; import walkingkooka.spreadsheet.format.SpreadsheetFormatterSelector; import walkingkooka.spreadsheet.parser.SpreadsheetParserSelector; @@ -189,6 +190,13 @@ public static ExpressionFunction cellFormula() { + return SpreadsheetExpressionFunctionCellFormula.INSTANCE; + } + /** * {@see SpreadsheetExpressionFunctionCellParser} */ diff --git a/src/test/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionCellFormulaTest.java b/src/test/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionCellFormulaTest.java new file mode 100644 index 0000000..e62cea4 --- /dev/null +++ b/src/test/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionCellFormulaTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2022 Miroslav Pokorny (github.com/mP1) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package walkingkooka.spreadsheet.expression.function; + +import org.junit.jupiter.api.Test; +import walkingkooka.spreadsheet.SpreadsheetCell; +import walkingkooka.spreadsheet.SpreadsheetFormula; + +public final class SpreadsheetExpressionFunctionCellFormulaTest extends SpreadsheetExpressionFunctionCellTestCase { + + @Override + public SpreadsheetExpressionFunctionCellFormula createBiFunction() { + return SpreadsheetExpressionFunctionCellFormula.INSTANCE; + } + + @Override + SpreadsheetFormula valuePresent() { + return SpreadsheetFormula.EMPTY.setText("=1+2"); + } + + @Override + SpreadsheetFormula valueAbsent() { + return SpreadsheetFormula.EMPTY; + } + + @Override + SpreadsheetCell setProperty(final SpreadsheetCell cell, + final SpreadsheetFormula formula) { + return cell.setFormula(formula); + } + + // toString......................................................................................................... + + @Test + public void testToString() { + this.toStringAndCheck( + this.createBiFunction(), + "cellFormula" + ); + } + + // class............................................................................................................ + + @Override + public Class type() { + return SpreadsheetExpressionFunctionCellFormula.class; + } +}