Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement YEAR function #1007

Merged
merged 10 commits into from
Aug 21, 2021
Merged
31 changes: 31 additions & 0 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ type formulaFuncs struct {
// VAR.P
// VARP
// VLOOKUP
// YEAR
//
func (f *File) CalcCellValue(sheet, cell string) (result string, err error) {
var (
Expand Down Expand Up @@ -6445,6 +6446,36 @@ func (fn *formulaFuncs) MONTH(argsList *list.List) formulaArg {
return newNumberFormulaArg(float64(timeFromExcelTime(num.Number, false).Month()))
}

// YEAR function returns an integer representing the year of a supplied date.
// The syntax of the function is:
//
// YEAR(serial_number)
//
func (fn *formulaFuncs) YEAR(argsList *list.List) formulaArg {
if argsList.Len() != 1 {
return newErrorFormulaArg(formulaErrorVALUE, "YEAR requires exactly 1 argument")
}
arg := argsList.Front().Value.(formulaArg)
num := arg.ToNumber()
if num.Type != ArgNumber {
dateString := strings.ToLower(arg.Value())
if !isDateOnlyFmt(dateString) {
if _, _, _, _, _, err := strToTime(dateString); err.Type == ArgError {
return err
}
}
year, _, _, _, err := strToDate(dateString)
if err.Type == ArgError {
return err
}
return newNumberFormulaArg(float64(year))
}
if num.Number < 0 {
return newErrorFormulaArg(formulaErrorNUM, "YEAR only accepts positive argument")
}
return newNumberFormulaArg(float64(timeFromExcelTime(num.Number, false).Year()))
}

// NOW function returns the current date and time. The function receives no
// arguments and therefore. The syntax of the function is:
//
Expand Down
15 changes: 14 additions & 1 deletion calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,13 @@ func TestCalcCellValue(t *testing.T) {
// MONTH
"=MONTH(42171)": "6",
"=MONTH(\"31-May-2015\")": "5",
// YEAR
"=YEAR(15)": "1900",
"=YEAR(\"15\")": "1900",
"=YEAR(2048)": "1905",
"=YEAR(42171)": "2015",
"=YEAR(\"29-May-2015\")": "2015",
"=YEAR(\"05/03/1984\")": "1984",
// Text Functions
// CHAR
"=CHAR(65)": "A",
Expand Down Expand Up @@ -1986,10 +1993,16 @@ func TestCalcCellValue(t *testing.T) {
"=DAY(\"0-January-1900\")": "#VALUE!",
// MONTH
"=MONTH()": "MONTH requires exactly 1 argument",
"=MONTH(43891,43101)": "MONTH requires exactly 1 argument",
"=MONTH(0,0)": "MONTH requires exactly 1 argument",
"=MONTH(-1)": "MONTH only accepts positive argument",
"=MONTH(\"text\")": "#VALUE!",
"=MONTH(\"January 25, 100\")": "#VALUE!",
// YEAR
"=YEAR()": "YEAR requires exactly 1 argument",
"=YEAR(0,0)": "YEAR requires exactly 1 argument",
"=YEAR(-1)": "YEAR only accepts positive argument",
"=YEAR(\"text\")": "#VALUE!",
"=YEAR(\"January 25, 100\")": "#VALUE!",
// NOW
"=NOW(A1)": "NOW accepts no arguments",
// TODAY
Expand Down