1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Globalization;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using OfficeOpenXml.FormulaParsing.ExpressionGraph;
|
---|
7 |
|
---|
8 | namespace OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime
|
---|
9 | {
|
---|
10 | public class Weeknum : ExcelFunction
|
---|
11 | {
|
---|
12 | public override ExpressionGraph.CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
|
---|
13 | {
|
---|
14 | ValidateArguments(arguments, 1, eErrorType.Value);
|
---|
15 | var dateSerial = ArgToDecimal(arguments, 0);
|
---|
16 | var date = System.DateTime.FromOADate(dateSerial);
|
---|
17 | var startDay = DayOfWeek.Sunday;
|
---|
18 | if (arguments.Count() > 1)
|
---|
19 | {
|
---|
20 | var argStartDay = ArgToInt(arguments, 1);
|
---|
21 | switch (argStartDay)
|
---|
22 | {
|
---|
23 | case 1:
|
---|
24 | startDay = DayOfWeek.Sunday;
|
---|
25 | break;
|
---|
26 | case 2:
|
---|
27 | case 11:
|
---|
28 | startDay = DayOfWeek.Monday;
|
---|
29 | break;
|
---|
30 | case 12:
|
---|
31 | startDay = DayOfWeek.Tuesday;
|
---|
32 | break;
|
---|
33 | case 13:
|
---|
34 | startDay = DayOfWeek.Wednesday;
|
---|
35 | break;
|
---|
36 | case 14:
|
---|
37 | startDay = DayOfWeek.Thursday;
|
---|
38 | break;
|
---|
39 | case 15:
|
---|
40 | startDay = DayOfWeek.Friday;
|
---|
41 | break;
|
---|
42 | case 16:
|
---|
43 | startDay = DayOfWeek.Saturday;
|
---|
44 | break;
|
---|
45 | default:
|
---|
46 | // Not supported
|
---|
47 | ThrowExcelErrorValueException(eErrorType.Num);
|
---|
48 | break;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | if (DateTimeFormatInfo.CurrentInfo == null)
|
---|
52 | {
|
---|
53 | throw new InvalidOperationException(
|
---|
54 | "Could not execute Weeknum function because DateTimeFormatInfo.CurrentInfo was null");
|
---|
55 | }
|
---|
56 | var week = DateTimeFormatInfo.CurrentInfo.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay,
|
---|
57 | startDay);
|
---|
58 | return CreateResult(week, DataType.Integer);
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | }
|
---|
63 | }
|
---|