From ad4abd484ac4da1abfda5480515f193eda50bf8b Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Fri, 8 Aug 2008 08:05:07 +0000 Subject: [PATCH] Patch 45577 - Added implementations for Excel functions NOW and TODAY, added property getters to HSSFConditionalFormattingRule git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@683901 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 3 +- src/documentation/content/xdocs/status.xml | 3 +- .../hssf/record/formula/functions/Now.java | 55 ++++++++++------ .../hssf/record/formula/functions/Today.java | 63 ++++++++++++------- .../HSSFConditionalFormattingRule.java | 51 +++++++++------ 5 files changed, 113 insertions(+), 62 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index dc25332839..a18332829a 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,7 +37,8 @@ - 45582 - Fix for workbook streams with extra bytes trailing the EOFRecord + 45577 - Added implementations for Excel functions NOW and TODAY + 45582 - Fix for workbook streams with extra bytes trailing the EOFRecord 45537 - Include headers and footers (of slides and notes) in the extracted text from HSLF 45472 - Fixed incorrect default row height in OpenOffice 2.3 44692 - HSSFPicture.resize() stretched image when there was a text next to it diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index b0230d148b..b0bc9bb21e 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,7 +34,8 @@ - 45582 - Fix for workbook streams with extra bytes trailing the EOFRecord + 45577 - Added implementations for Excel functions NOW and TODAY + 45582 - Fix for workbook streams with extra bytes trailing the EOFRecord 45537 - Include headers and footers (of slides and notes) in the extracted text from HSLF 45472 - Fixed incorrect default row height in OpenOffice 2.3 44692 - HSSFPicture.resize() stretched image when there was a text next to it diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/Now.java b/src/java/org/apache/poi/hssf/record/formula/functions/Now.java index 2c23a97b37..70930a9dcb 100644 --- a/src/java/org/apache/poi/hssf/record/formula/functions/Now.java +++ b/src/java/org/apache/poi/hssf/record/formula/functions/Now.java @@ -1,25 +1,40 @@ -/* -* Licensed to the Apache Software Foundation (ASF) under one or more -* contributor license agreements. See the NOTICE file distributed with -* this work for additional information regarding copyright ownership. -* The ASF licenses this file to You 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. -*/ -/* - * Created on May 15, 2005 +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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 org.apache.poi.hssf.record.formula.functions; + +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.usermodel.HSSFDateUtil; + +/** + * Implementation of Excel NOW() Function * + * @author Frank Taffelt */ -package org.apache.poi.hssf.record.formula.functions; +public final class Now implements Function { -public class Now extends NotImplementedFunction { + public Eval evaluate(Eval[] evals, int srcCellRow, short srcCellCol) { + if (evals.length > 0) { + return ErrorEval.VALUE_INVALID; + } + java.util.Date now = new java.util.Date(System.currentTimeMillis()); + return new NumberEval(HSSFDateUtil.getExcelDate(now)); + } } diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/Today.java b/src/java/org/apache/poi/hssf/record/formula/functions/Today.java index 6260d20a58..b707412136 100644 --- a/src/java/org/apache/poi/hssf/record/formula/functions/Today.java +++ b/src/java/org/apache/poi/hssf/record/formula/functions/Today.java @@ -1,25 +1,46 @@ -/* -* Licensed to the Apache Software Foundation (ASF) under one or more -* contributor license agreements. See the NOTICE file distributed with -* this work for additional information regarding copyright ownership. -* The ASF licenses this file to You 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. -*/ -/* - * Created on May 15, 2005 - * - */ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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 org.apache.poi.hssf.record.formula.functions; -public class Today extends NotImplementedFunction { +import java.util.Calendar; +import java.util.GregorianCalendar; + +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.usermodel.HSSFDateUtil; +/** + * Implementation of Excel TODAY() Function
+ * + * @author Frank Taffelt + */ +public class Today implements Function { + + public Eval evaluate(Eval[] evals, int srcCellRow, short srcCellCol) { + if (evals.length > 0) { + return ErrorEval.VALUE_INVALID; + } + + Calendar now = new GregorianCalendar(); + now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE),0,0,0); + now.set(Calendar.MILLISECOND, 0); + return new NumberEval(HSSFDateUtil.getExcelDate(now.getTime())); + } } + diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java index 1d4100655a..10d86015aa 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java @@ -26,17 +26,16 @@ import org.apache.poi.hssf.record.cf.PatternFormatting; import org.apache.poi.hssf.record.formula.Ptg; /** - * + * * High level representation of Conditional Formatting Rule. * It allows to specify formula based conditions for the Conditional Formatting * and the formatting settings such as font, border and pattern. - * + * * @author Dmitriy Kumshayev */ - public final class HSSFConditionalFormattingRule { - private static final byte CELL_COMPARISON = CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS; + private static final byte CELL_COMPARISON = CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS; private final CFRuleRecord cfRuleRecord; private final HSSFWorkbook workbook; @@ -50,11 +49,11 @@ public final class HSSFConditionalFormattingRule { return cfRuleRecord; } - + private HSSFFontFormatting getFontFormatting(boolean create) { FontFormatting fontFormatting = cfRuleRecord.getFontFormatting(); - if ( fontFormatting != null) + if ( fontFormatting != null) { cfRuleRecord.setFontFormatting(fontFormatting); return new HSSFFontFormatting(cfRuleRecord); @@ -70,7 +69,7 @@ public final class HSSFConditionalFormattingRule return null; } } - + /** * @return - font formatting object if defined, null otherwise */ @@ -79,19 +78,19 @@ public final class HSSFConditionalFormattingRule return getFontFormatting(false); } /** - * create a new font formatting structure if it does not exist, + * create a new font formatting structure if it does not exist, * otherwise just return existing object. - * @return - font formatting object, never returns null. + * @return - font formatting object, never returns null. */ public HSSFFontFormatting createFontFormatting() { return getFontFormatting(true); } - + private HSSFBorderFormatting getBorderFormatting(boolean create) { BorderFormatting borderFormatting = cfRuleRecord.getBorderFormatting(); - if ( borderFormatting != null) + if ( borderFormatting != null) { cfRuleRecord.setBorderFormatting(borderFormatting); return new HSSFBorderFormatting(cfRuleRecord); @@ -115,19 +114,19 @@ public final class HSSFConditionalFormattingRule return getBorderFormatting(false); } /** - * create a new border formatting structure if it does not exist, + * create a new border formatting structure if it does not exist, * otherwise just return existing object. - * @return - border formatting object, never returns null. + * @return - border formatting object, never returns null. */ public HSSFBorderFormatting createBorderFormatting() { return getBorderFormatting(true); } - + private HSSFPatternFormatting getPatternFormatting(boolean create) { PatternFormatting patternFormatting = cfRuleRecord.getPatternFormatting(); - if ( patternFormatting != null) + if ( patternFormatting != null) { cfRuleRecord.setPatternFormatting(patternFormatting); return new HSSFPatternFormatting(cfRuleRecord); @@ -143,7 +142,7 @@ public final class HSSFConditionalFormattingRule return null; } } - + /** * @return - pattern formatting object if defined, null otherwise */ @@ -152,15 +151,29 @@ public final class HSSFConditionalFormattingRule return getPatternFormatting(false); } /** - * create a new pattern formatting structure if it does not exist, + * create a new pattern formatting structure if it does not exist, * otherwise just return existing object. - * @return - pattern formatting object, never returns null. + * @return - pattern formatting object, never returns null. */ public HSSFPatternFormatting createPatternFormatting() { return getPatternFormatting(true); } - + + /** + * @return - the conditiontype for the cfrule + */ + public byte getConditionType() { + return cfRuleRecord.getConditionType(); + } + + /** + * @return - the comparisionoperatation for the cfrule + */ + public byte getComparisonOperation() { + return cfRuleRecord.getComparisonOperation(); + } + public String getFormula1() { return toFormulaString(cfRuleRecord.getParsedExpression1()); -- 2.39.5