Line 0
Link Here
|
|
|
1 |
/* ==================================================================== |
2 |
Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
contributor license agreements. See the NOTICE file distributed with |
4 |
this work for additional information regarding copyright ownership. |
5 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
(the "License"); you may not use this file except in compliance with |
7 |
the License. You may obtain a copy of the License at |
8 |
|
9 |
http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
11 |
Unless required by applicable law or agreed to in writing, software |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
See the License for the specific language governing permissions and |
15 |
limitations under the License. |
16 |
==================================================================== */ |
17 |
|
18 |
package org.apache.poi.ss.formula.functions; |
19 |
|
20 |
import org.apache.poi.ss.formula.OperationEvaluationContext; |
21 |
import org.apache.poi.ss.formula.eval.*; |
22 |
import org.apache.poi.ss.usermodel.DateUtil; |
23 |
|
24 |
import java.util.Calendar; |
25 |
import java.util.GregorianCalendar; |
26 |
|
27 |
/** |
28 |
* Implementation for Excel WeekNum() function.<p/> |
29 |
* <p/> |
30 |
* <b>Syntax</b>:<br/> <b>WeekNum </b>(<b>Serial_num</b>,<b>Return_type</b>)<br/> |
31 |
* <p/> |
32 |
* Returns a number that indicates where the week falls numerically within a year. |
33 |
* <p/> |
34 |
* <p/> |
35 |
* Serial_num is a date within the week. Dates should be entered by using the DATE function, |
36 |
* or as results of other formulas or functions. For example, use DATE(2008,5,23) |
37 |
* for the 23rd day of May, 2008. Problems can occur if dates are entered as text. |
38 |
* Return_type is a number that determines on which day the week begins. The default is 1. |
39 |
* 1 Week begins on Sunday. Weekdays are numbered 1 through 7. |
40 |
* 2 Week begins on Monday. Weekdays are numbered 1 through 7. |
41 |
* |
42 |
* @author cedric dot walter @ gmail dot com |
43 |
*/ |
44 |
public class WeekNum extends Fixed2ArgFunction implements FreeRefFunction { |
45 |
|
46 |
public static final FreeRefFunction instance = new WeekNum(); |
47 |
|
48 |
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval serialNumVE, ValueEval returnTypeVE) { |
49 |
double serialNum = 0.0; |
50 |
try { |
51 |
serialNum = NumericFunction.singleOperandEvaluate(serialNumVE, srcRowIndex, srcColumnIndex); |
52 |
} catch (EvaluationException e) { |
53 |
return ErrorEval.VALUE_INVALID; |
54 |
} |
55 |
Calendar serialNumCalendar = new GregorianCalendar(); |
56 |
serialNumCalendar.setTime(DateUtil.getJavaDate(serialNum, false)); |
57 |
|
58 |
int returnType = 0; |
59 |
try { |
60 |
ValueEval ve = OperandResolver.getSingleValue(returnTypeVE, srcRowIndex, srcColumnIndex); |
61 |
returnType = OperandResolver.coerceValueToInt(ve); |
62 |
} catch (EvaluationException e) { |
63 |
return ErrorEval.NUM_ERROR; |
64 |
} |
65 |
|
66 |
if (returnType != 1 && returnType != 2) { |
67 |
return ErrorEval.NUM_ERROR; |
68 |
} |
69 |
|
70 |
return new NumberEval(this.getWeekNo(serialNumCalendar, returnType)); |
71 |
} |
72 |
|
73 |
public int getWeekNo(Calendar cal, int weekStartOn) { |
74 |
if (weekStartOn == 1) { |
75 |
cal.setFirstDayOfWeek(Calendar.SUNDAY); |
76 |
} else { |
77 |
cal.setFirstDayOfWeek(Calendar.MONDAY); |
78 |
} |
79 |
return cal.get(Calendar.WEEK_OF_YEAR); |
80 |
} |
81 |
|
82 |
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) { |
83 |
if (args.length == 2) { |
84 |
return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]); |
85 |
} |
86 |
return ErrorEval.VALUE_INVALID; |
87 |
} |
88 |
} |