diff --git a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionObjectCellStyle.java b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionObjectCellStyle.java new file mode 100644 index 0000000..b87ca1d --- /dev/null +++ b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionObjectCellStyle.java @@ -0,0 +1,53 @@ +/* + * 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.expression.SpreadsheetExpressionEvaluationContext; +import walkingkooka.tree.expression.function.ExpressionFunctionParameter; + +import java.util.List; + +/** + * A custom function that retrieves the {@link walkingkooka.tree.text.TextStyle} for the current cell. + * This function exists primarily to support filtering cells using a predicate of the current cell. + */ +final class SpreadsheetExpressionFunctionObjectCellStyle extends SpreadsheetExpressionFunctionObject { + + /** + * Singleton + */ + final static SpreadsheetExpressionFunctionObjectCellStyle INSTANCE = new SpreadsheetExpressionFunctionObjectCellStyle(); + + private SpreadsheetExpressionFunctionObjectCellStyle() { + super("cellStyle"); + } + + @Override + public List> parameters(final int count) { + return ExpressionFunctionParameter.EMPTY; + } + + @Override + public Object apply(final List parameters, + final SpreadsheetExpressionEvaluationContext context) { + this.checkParameterCount(parameters); + + return context.cellOrFail() + .style(); + } +} diff --git a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionProviders.java b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionProviders.java index 12af5b0..f8c9780 100644 --- a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionProviders.java +++ b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionProviders.java @@ -56,6 +56,7 @@ public static ExpressionFunctionProvider expressionFunctionProvider(final CaseSe SpreadsheetExpressionFunctions.bitXor(), SpreadsheetExpressionFunctions.ceil(), SpreadsheetExpressionFunctions.cell(), + SpreadsheetExpressionFunctions.cellStyle(), SpreadsheetExpressionFunctions.cellValue(), SpreadsheetExpressionFunctions.charFunction(), SpreadsheetExpressionFunctions.choose(), diff --git a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctions.java b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctions.java index a613897..22c532b 100644 --- a/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctions.java +++ b/src/main/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctions.java @@ -171,6 +171,13 @@ public static ExpressionFunction return SpreadsheetExpressionFunctionObjectCell.INSTANCE; } + /** + * {@see SpreadsheetExpressionFunctionObjectCellStyle} + */ + public static ExpressionFunction cellStyle() { + return SpreadsheetExpressionFunctionObjectCellStyle.INSTANCE; + } + /** * {@see SpreadsheetExpressionFunctionObjectCellValue} */ diff --git a/src/test/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionObjectCellStyleTest.java b/src/test/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionObjectCellStyleTest.java new file mode 100644 index 0000000..714b7e9 --- /dev/null +++ b/src/test/java/walkingkooka/spreadsheet/expression/function/SpreadsheetExpressionFunctionObjectCellStyleTest.java @@ -0,0 +1,86 @@ +/* + * 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.collect.list.Lists; +import walkingkooka.spreadsheet.SpreadsheetCell; +import walkingkooka.spreadsheet.SpreadsheetFormula; +import walkingkooka.spreadsheet.expression.FakeSpreadsheetExpressionEvaluationContext; +import walkingkooka.spreadsheet.reference.SpreadsheetSelection; +import walkingkooka.tree.text.TextAlign; +import walkingkooka.tree.text.TextStyle; +import walkingkooka.tree.text.TextStylePropertyName; + +import java.util.Optional; + +public final class SpreadsheetExpressionFunctionObjectCellStyleTest extends SpreadsheetExpressionFunctionObjectTestCase { + + @Test + public void testApply() { + final TextStyle style = TextStyle.EMPTY.set( + TextStylePropertyName.TEXT_ALIGN, + TextAlign.LEFT + ); + + final SpreadsheetCell cell = SpreadsheetSelection.A1.setFormula(SpreadsheetFormula.EMPTY) + .setStyle(style); + this.applyAndCheck( + Lists.empty(), + new FakeSpreadsheetExpressionEvaluationContext() { + @Override + public Optional cell() { + return Optional.of(cell); + } + + @Override + public String toString() { + return "cell: " + this.cell(); + } + }, + style + ); + } + + @Override + public SpreadsheetExpressionFunctionObjectCellStyle createBiFunction() { + return SpreadsheetExpressionFunctionObjectCellStyle.INSTANCE; + } + + @Override + public int minimumParameterCount() { + return 0; + } + + // toString......................................................................................................... + + @Test + public void testToString() { + this.toStringAndCheck( + this.createBiFunction(), + "cellStyle" + ); + } + + // class............................................................................................................ + + @Override + public Class type() { + return SpreadsheetExpressionFunctionObjectCellStyle.class; + } +}