2023-01-02 11:47:31 +08:00
|
|
|
// Copyright 2016 - 2023 The excelize Authors. All rights reserved. Use of
|
2018-09-14 00:44:23 +08:00
|
|
|
// this source code is governed by a BSD-style license that can be found in
|
|
|
|
// the LICENSE file.
|
|
|
|
//
|
2022-02-17 00:09:11 +08:00
|
|
|
// Package excelize providing a set of functions that allow you to write to and
|
|
|
|
// read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and
|
|
|
|
// writing spreadsheet documents generated by Microsoft Excel™ 2007 and later.
|
|
|
|
// Supports complex components by high compatibility, and provided streaming
|
|
|
|
// API for generating or reading data from a worksheet with huge amounts of
|
2023-01-02 11:47:31 +08:00
|
|
|
// data. This library needs Go version 1.16 or later.
|
2018-09-14 00:58:48 +08:00
|
|
|
|
2017-05-05 14:40:28 +08:00
|
|
|
package excelize
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
const (
|
2021-07-29 00:03:57 +08:00
|
|
|
nanosInADay = float64((24 * time.Hour) / time.Nanosecond)
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
dayNanoseconds = 24 * time.Hour
|
|
|
|
maxDuration = 290 * 364 * dayNanoseconds
|
2021-10-12 00:01:11 +08:00
|
|
|
roundEpsilon = 1e-9
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
)
|
2017-05-05 14:40:28 +08:00
|
|
|
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
var (
|
2021-08-21 11:50:49 +08:00
|
|
|
daysInMonth = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
2021-07-29 00:03:57 +08:00
|
|
|
excel1900Epoc = time.Date(1899, time.December, 30, 0, 0, 0, 0, time.UTC)
|
|
|
|
excel1904Epoc = time.Date(1904, time.January, 1, 0, 0, 0, 0, time.UTC)
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
excelMinTime1900 = time.Date(1899, time.December, 31, 0, 0, 0, 0, time.UTC)
|
|
|
|
excelBuggyPeriodStart = time.Date(1900, time.March, 1, 0, 0, 0, 0, time.UTC).Add(-time.Nanosecond)
|
|
|
|
)
|
2017-05-05 14:40:28 +08:00
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// timeToExcelTime provides a function to convert time to Excel time.
|
2022-05-02 12:30:18 +08:00
|
|
|
func timeToExcelTime(t time.Time, date1904 bool) (float64, error) {
|
|
|
|
date := excelMinTime1900
|
|
|
|
if date1904 {
|
|
|
|
date = excel1904Epoc
|
|
|
|
}
|
|
|
|
if t.Before(date) {
|
2022-04-30 09:54:11 +08:00
|
|
|
return 0, nil
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
}
|
2022-05-02 12:30:18 +08:00
|
|
|
tt, diff, result := t, t.Sub(date), 0.0
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
for diff >= maxDuration {
|
|
|
|
result += float64(maxDuration / dayNanoseconds)
|
|
|
|
tt = tt.Add(-maxDuration)
|
2022-05-02 12:30:18 +08:00
|
|
|
diff = tt.Sub(date)
|
Huge refactorig for consistent col/row numbering (#356)
* Huge refactorig for consistent col/row numbering
Started from simply changing ToALphaString()/TitleToNumber() logic and related fixes.
But have to go deeper, do fixes, after do related fixes and again and again.
Major improvements:
1. Tests made stronger again (But still be weak).
2. "Empty" returns for incorrect input replaces with panic.
3. Check for correct col/row/cell naming & addressing by default.
4. Removed huge amount of duplicated code.
5. Removed ToALphaString(), TitleToNumber() and it helpers functions at all,
and replaced with SplitCellName(), JoinCellName(), ColumnNameToNumber(), ColumnNumberToName(), CellNameToCoordinates(), CoordinatesToCellName().
6. Minor fixes for internal variable naming for code readability (ex. col, row for input params, colIdx, rowIdx for slice indexes etc).
* Formatting fixes
2019-03-20 00:14:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rem := diff % dayNanoseconds
|
|
|
|
result += float64(diff-rem)/float64(dayNanoseconds) + float64(rem)/float64(dayNanoseconds)
|
|
|
|
|
|
|
|
// Excel dates after 28th February 1900 are actually one day out.
|
|
|
|
// Excel behaves as though the date 29th February 1900 existed, which it didn't.
|
|
|
|
// Microsoft intentionally included this bug in Excel so that it would remain compatible with the spreadsheet
|
|
|
|
// program that had the majority market share at the time; Lotus 1-2-3.
|
|
|
|
// https://www.myonlinetraininghub.com/excel-date-and-time
|
2022-05-02 12:30:18 +08:00
|
|
|
if !date1904 && t.After(excelBuggyPeriodStart) {
|
2022-04-30 09:54:11 +08:00
|
|
|
result++
|
2018-06-19 20:28:40 +08:00
|
|
|
}
|
2019-03-23 20:08:06 +08:00
|
|
|
return result, nil
|
2017-05-05 14:40:28 +08:00
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// shiftJulianToNoon provides a function to process julian date to noon.
|
2017-05-05 14:40:28 +08:00
|
|
|
func shiftJulianToNoon(julianDays, julianFraction float64) (float64, float64) {
|
|
|
|
switch {
|
|
|
|
case -0.5 < julianFraction && julianFraction < 0.5:
|
|
|
|
julianFraction += 0.5
|
|
|
|
case julianFraction >= 0.5:
|
|
|
|
julianDays++
|
|
|
|
julianFraction -= 0.5
|
|
|
|
case julianFraction <= -0.5:
|
|
|
|
julianDays--
|
|
|
|
julianFraction += 1.5
|
|
|
|
}
|
|
|
|
return julianDays, julianFraction
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// fractionOfADay provides a function to return the integer values for hour,
|
2017-05-05 14:40:28 +08:00
|
|
|
// minutes, seconds and nanoseconds that comprised a given fraction of a day.
|
|
|
|
// values would round to 1 us.
|
|
|
|
func fractionOfADay(fraction float64) (hours, minutes, seconds, nanoseconds int) {
|
|
|
|
const (
|
|
|
|
c1us = 1e3
|
|
|
|
c1s = 1e9
|
|
|
|
c1day = 24 * 60 * 60 * c1s
|
|
|
|
)
|
|
|
|
|
|
|
|
frac := int64(c1day*fraction + c1us/2)
|
|
|
|
nanoseconds = int((frac%c1s)/c1us) * c1us
|
|
|
|
frac /= c1s
|
|
|
|
seconds = int(frac % 60)
|
|
|
|
frac /= 60
|
|
|
|
minutes = int(frac % 60)
|
|
|
|
hours = int(frac / 60)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// julianDateToGregorianTime provides a function to convert julian date to
|
2017-05-05 14:40:28 +08:00
|
|
|
// gregorian time.
|
|
|
|
func julianDateToGregorianTime(part1, part2 float64) time.Time {
|
|
|
|
part1I, part1F := math.Modf(part1)
|
|
|
|
part2I, part2F := math.Modf(part2)
|
|
|
|
julianDays := part1I + part2I
|
|
|
|
julianFraction := part1F + part2F
|
|
|
|
julianDays, julianFraction = shiftJulianToNoon(julianDays, julianFraction)
|
|
|
|
day, month, year := doTheFliegelAndVanFlandernAlgorithm(int(julianDays))
|
|
|
|
hours, minutes, seconds, nanoseconds := fractionOfADay(julianFraction)
|
|
|
|
return time.Date(year, time.Month(month), day, hours, minutes, seconds, nanoseconds, time.UTC)
|
|
|
|
}
|
|
|
|
|
2018-12-15 00:08:55 +08:00
|
|
|
// doTheFliegelAndVanFlandernAlgorithm; By this point generations of
|
|
|
|
// programmers have repeated the algorithm sent to the editor of
|
|
|
|
// "Communications of the ACM" in 1968 (published in CACM, volume 11, number
|
|
|
|
// 10, October 1968, p.657). None of those programmers seems to have found it
|
|
|
|
// necessary to explain the constants or variable names set out by Henry F.
|
|
|
|
// Fliegel and Thomas C. Van Flandern. Maybe one day I'll buy that jounal and
|
|
|
|
// expand an explanation here - that day is not today.
|
2017-05-05 14:40:28 +08:00
|
|
|
func doTheFliegelAndVanFlandernAlgorithm(jd int) (day, month, year int) {
|
|
|
|
l := jd + 68569
|
|
|
|
n := (4 * l) / 146097
|
|
|
|
l = l - (146097*n+3)/4
|
|
|
|
i := (4000 * (l + 1)) / 1461001
|
|
|
|
l = l - (1461*i)/4 + 31
|
|
|
|
j := (80 * l) / 2447
|
|
|
|
d := l - (2447*j)/80
|
|
|
|
l = j / 11
|
|
|
|
m := j + 2 - (12 * l)
|
|
|
|
y := 100*(n-49) + i + l
|
|
|
|
return d, m, y
|
|
|
|
}
|
|
|
|
|
2018-08-06 10:21:24 +08:00
|
|
|
// timeFromExcelTime provides a function to convert an excelTime
|
|
|
|
// representation (stored as a floating point number) to a time.Time.
|
2017-05-05 14:40:28 +08:00
|
|
|
func timeFromExcelTime(excelTime float64, date1904 bool) time.Time {
|
|
|
|
var date time.Time
|
2022-01-23 00:32:34 +08:00
|
|
|
wholeDaysPart := int(excelTime)
|
2017-05-05 14:40:28 +08:00
|
|
|
// Excel uses Julian dates prior to March 1st 1900, and Gregorian
|
|
|
|
// thereafter.
|
2021-07-29 00:03:57 +08:00
|
|
|
if wholeDaysPart <= 61 {
|
2017-05-05 14:40:28 +08:00
|
|
|
const OFFSET1900 = 15018.0
|
|
|
|
const OFFSET1904 = 16480.0
|
|
|
|
const MJD0 float64 = 2400000.5
|
|
|
|
var date time.Time
|
|
|
|
if date1904 {
|
|
|
|
date = julianDateToGregorianTime(MJD0, excelTime+OFFSET1904)
|
|
|
|
} else {
|
|
|
|
date = julianDateToGregorianTime(MJD0, excelTime+OFFSET1900)
|
|
|
|
}
|
|
|
|
return date
|
|
|
|
}
|
2022-01-23 00:32:34 +08:00
|
|
|
floatPart := excelTime - float64(wholeDaysPart) + roundEpsilon
|
2017-05-05 14:40:28 +08:00
|
|
|
if date1904 {
|
2021-07-29 00:03:57 +08:00
|
|
|
date = excel1904Epoc
|
2017-05-05 14:40:28 +08:00
|
|
|
} else {
|
2021-07-29 00:03:57 +08:00
|
|
|
date = excel1900Epoc
|
2017-05-05 14:40:28 +08:00
|
|
|
}
|
2021-07-29 00:03:57 +08:00
|
|
|
durationPart := time.Duration(nanosInADay * floatPart)
|
2021-10-12 00:01:11 +08:00
|
|
|
return date.AddDate(0, 0, wholeDaysPart).Add(durationPart).Truncate(time.Second)
|
2017-05-05 14:40:28 +08:00
|
|
|
}
|
2020-03-03 19:31:02 +08:00
|
|
|
|
|
|
|
// ExcelDateToTime converts a float-based excel date representation to a time.Time.
|
|
|
|
func ExcelDateToTime(excelDate float64, use1904Format bool) (time.Time, error) {
|
|
|
|
if excelDate < 0 {
|
|
|
|
return time.Time{}, newInvalidExcelDateError(excelDate)
|
|
|
|
}
|
|
|
|
return timeFromExcelTime(excelDate, use1904Format), nil
|
|
|
|
}
|
2021-08-21 11:50:49 +08:00
|
|
|
|
|
|
|
// isLeapYear determine if leap year for a given year.
|
|
|
|
func isLeapYear(y int) bool {
|
|
|
|
if y == y/400*400 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if y == y/100*100 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return y == y/4*4
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDaysInMonth provides a function to get the days by a given year and
|
|
|
|
// month number.
|
|
|
|
func getDaysInMonth(y, m int) int {
|
|
|
|
if m == 2 && isLeapYear(y) {
|
|
|
|
return 29
|
|
|
|
}
|
|
|
|
return daysInMonth[m-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateDate provides a function to validate if a valid date by a given
|
|
|
|
// year, month, and day number.
|
|
|
|
func validateDate(y, m, d int) bool {
|
|
|
|
if m < 1 || m > 12 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if d < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return d <= getDaysInMonth(y, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// formatYear converts the given year number into a 4-digit format.
|
|
|
|
func formatYear(y int) int {
|
|
|
|
if y < 1900 {
|
|
|
|
if y < 30 {
|
|
|
|
y += 2000
|
|
|
|
} else {
|
|
|
|
y += 1900
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|