summaryrefslogtreecommitdiff
path: root/calc.go
diff options
context:
space:
mode:
authorStani <spe.stani.be@gmail.com>2021-08-21 14:15:53 +0200
committerGitHub <noreply@github.com>2021-08-21 20:15:53 +0800
commit9b55f4f9f0b839934eb8113d2092c60a1a5b64b8 (patch)
tree5e63e75b107f6180636cd846b0fce8461b90a4c5 /calc.go
parent4d716fa7edde76ed838aefcd8c5945bcfe3ba8f3 (diff)
This closes #1006, new fn: MONTH ref #65
Co-authored-by: xuri <xuri.me@gmail.com>
Diffstat (limited to 'calc.go')
-rw-r--r--calc.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/calc.go b/calc.go
index 9f3e25f..e59d344 100644
--- a/calc.go
+++ b/calc.go
@@ -504,6 +504,7 @@ type formulaFuncs struct {
// VAR.P
// VARP
// VLOOKUP
+// YEAR
//
func (f *File) CalcCellValue(sheet, cell string) (result string, err error) {
var (
@@ -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:
//