--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.formula.functions;\r
+\r
+import org.apache.poi.ss.formula.OperationEvaluationContext;\r
+import org.apache.poi.ss.formula.eval.*;\r
+import org.apache.poi.ss.usermodel.DateUtil;\r
+\r
+import java.util.Calendar;\r
+import java.util.GregorianCalendar;\r
+\r
+/**\r
+ * Implementation for Excel WeekNum() function.<p/>\r
+ * <p/>\r
+ * <b>Syntax</b>:<br/> <b>WeekNum </b>(<b>Serial_num</b>,<b>Return_type</b>)<br/>\r
+ * <p/>\r
+ * Returns a number that indicates where the week falls numerically within a year.\r
+ * <p/>\r
+ * <p/>\r
+ * Serial_num is a date within the week. Dates should be entered by using the DATE function,\r
+ * or as results of other formulas or functions. For example, use DATE(2008,5,23)\r
+ * for the 23rd day of May, 2008. Problems can occur if dates are entered as text.\r
+ * Return_type is a number that determines on which day the week begins. The default is 1.\r
+ * 1 Week begins on Sunday. Weekdays are numbered 1 through 7.\r
+ * 2 Week begins on Monday. Weekdays are numbered 1 through 7.\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class WeekNum extends Fixed2ArgFunction implements FreeRefFunction {\r
+\r
+ public static final FreeRefFunction instance = new WeekNum();\r
+\r
+ public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval serialNumVE, ValueEval returnTypeVE) {\r
+ double serialNum = 0.0;\r
+ try {\r
+ serialNum = NumericFunction.singleOperandEvaluate(serialNumVE, srcRowIndex, srcColumnIndex);\r
+ } catch (EvaluationException e) {\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+ Calendar serialNumCalendar = new GregorianCalendar();\r
+ serialNumCalendar.setTime(DateUtil.getJavaDate(serialNum, false));\r
+\r
+ int returnType = 0;\r
+ try {\r
+ ValueEval ve = OperandResolver.getSingleValue(returnTypeVE, srcRowIndex, srcColumnIndex);\r
+ returnType = OperandResolver.coerceValueToInt(ve);\r
+ } catch (EvaluationException e) {\r
+ return ErrorEval.NUM_ERROR;\r
+ }\r
+\r
+ if (returnType != 1 && returnType != 2) {\r
+ return ErrorEval.NUM_ERROR;\r
+ }\r
+\r
+ return new NumberEval(this.getWeekNo(serialNumCalendar, returnType));\r
+ }\r
+\r
+ public int getWeekNo(Calendar cal, int weekStartOn) {\r
+ if (weekStartOn == 1) {\r
+ cal.setFirstDayOfWeek(Calendar.SUNDAY);\r
+ } else {\r
+ cal.setFirstDayOfWeek(Calendar.MONDAY);\r
+ }\r
+ return cal.get(Calendar.WEEK_OF_YEAR);\r
+ }\r
+\r
+ public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {\r
+ if (args.length == 2) {\r
+ return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);\r
+ }\r
+ return ErrorEval.VALUE_INVALID;\r
+ }\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.formula.functions;\r
+\r
+/**\r
+ * Tests WeekNum() as loaded from a test data spreadsheet.<p/>\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class TestWeekNumFunctionsFromSpreadsheet extends BaseTestFunctionsFromSpreadsheet {\r
+\r
+ protected String getFilename() {\r
+ return "WeekNumFunctionTestCaseData.xls";\r
+ }\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.formula.functions;\r
+\r
+/**\r
+ * Tests WeekNum() as loaded from a test data 2013 excel spreadsheet.<p/>\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public class TestWeekNumFunctionsFromSpreadsheet2013 extends BaseTestFunctionsFromSpreadsheet {\r
+\r
+ protected String getFilename() {\r
+ //Only open this file with Excel 2013 to keep binary specific to that version\r
+ return "WeekNumFunctionTestCaseData2013.xls";\r
+ }\r
+}\r