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

feat(formula): today function, set numfmt data #1295

Merged
merged 24 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3fdffa5
feat(formula): today function, set numfmt data
Dushusir Jan 29, 2024
8b1fd3d
feat(formula): supports numfmt
Dushusir Jan 30, 2024
149152b
test(formula): set numfmt data
Dushusir Jan 30, 2024
26a9f6e
fix(formula): sum get numfmt from link cell
Dushusir Jan 30, 2024
616d6e7
fix(sheet): recover status bar
Dushusir Jan 30, 2024
8d2c5ea
fix(formula): set numfmt on formula cell
Dushusir Jan 30, 2024
8ce958c
feat(formula): date function single number
Dushusir Jan 30, 2024
9b3a463
feat(formula): date function with array
Dushusir Jan 31, 2024
4ecdf50
fix(formula): date function test, link to formatted cell supports format
Dushusir Jan 31, 2024
de9cc3e
fix(formula): date serial calculation
Dushusir Feb 3, 2024
814be41
fix(formula): function calculate null params, abs gets 0 when linked …
Dushusir Feb 3, 2024
b6cb67b
fix(formula): concatenate calculate zero value
Dushusir Feb 3, 2024
5cc3063
fix(formula): sum,max,min .etc add blank params check
Dushusir Feb 3, 2024
8aa1fb7
test(formula): new test template
Dushusir Feb 3, 2024
bb769ba
fix(formula): date function gets error when date before 1900.1.1
Dushusir Feb 3, 2024
dc4c6bd
feat(formula): add edate function
Dushusir Feb 6, 2024
df2b6ca
test(formula): test edate function
Dushusir Feb 6, 2024
7e49aea
feat(formula): year,month,day functions
Dushusir Feb 6, 2024
9598eb5
feat(formula): match, xmatch functions
Dushusir Feb 6, 2024
ba2c693
test(formula): xmatch function test case
Dushusir Feb 6, 2024
ddebc78
test(formula): nested functions test
Dushusir Feb 6, 2024
644fbb4
fix(formula): formula cell shows format, cell calculation sets percen…
Dushusir Feb 6, 2024
28e2f49
fix(formula): edate,month,year functions support blank cell
Dushusir Feb 6, 2024
38d9310
fix(formula): xmatch description
Dushusir Feb 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(formula): date serial calculation
  • Loading branch information
Dushusir committed Feb 19, 2024
commit de9cc3e4f01f204674d4252a6e538fc383a8018c
4 changes: 4 additions & 0 deletions packages/engine-formula/src/basics/__tests__/date.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { excelDateSerial } from '../date';

describe('Test date', () => {
it('Function excelDateSerial', () => {
expect(excelDateSerial(new Date(1900, 1, 28))).toBe(59);
expect(excelDateSerial(new Date(1900, 1, 29))).toBe(61);
expect(excelDateSerial(new Date(1900, 2, 1))).toBe(61);
expect(excelDateSerial(new Date(1901, 0, 1))).toBe(367);
expect(excelDateSerial(new Date(2024, 1, 2))).toBe(45324);
});
});
22 changes: 18 additions & 4 deletions packages/engine-formula/src/basics/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,26 @@ export const DEFFAULT_DATE_FORMAT = 'yyyy/mm/dd';
/**
* Excel stores dates as sequential serial numbers so they can be used in calculations. By default, January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,447 days after January 1, 1900.
*
* Excel has a leap year error in 1900. February 29, 1900 is considered a legal date. In fact, there is no February 29 in 1900.
* 1900.2.28 Date Serial 59
* 1900.2.29 Date Serial 61
* 1900.3.1 Date Serial 61
* 1901.1.1 Date Serial 367
* @param date
* @returns
*/
export function excelDateSerial(date: Date): number {
// TODO@Dushusir: set current time zone, reference https://stackoverflow.com/questions/38399465/how-to-get-list-of-all-timezones-in-javascript
const baseDate = new Date(1900, 0, 1); // January 1, 1900
const dayDifference = (date.getTime() - baseDate.getTime()) / (1000 * 3600 * 24);
return Math.ceil(dayDifference) + 1; // +1 for adjusting for Excel's 1900 leap year error
const baseDate = new Date(Date.UTC(1900, 0, 1)); // January 1, 1900, UTC
const leapDayDate = new Date(Date.UTC(1900, 1, 28)); // February 28, 1900, UTC
const dateInUTC = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());

// Calculate the difference in days between the base date and the input date
let dayDifference = (dateInUTC - baseDate.getTime()) / (1000 * 3600 * 24);

// If the date is later than February 28, 1900, the day difference needs to be adjusted to account for Excel errors
if (dateInUTC > leapDayDate.getTime()) {
dayDifference += 1;
}

return Math.floor(dayDifference) + 1; // Excel serial number starts from 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@ import { ErrorType } from '../../../..';

// mock new Date() use V
const _Date = Date;
global.Date = vi.fn((...params) => params.length > 0 ? new _Date(params[0], params[1], params[2]) : new _Date(2020, 0, 1)) as any;
global.Date = vi.fn((...params) => {
if (params.length === 1) {
return new _Date(params[0]);
}

if (params.length === 3) {
return new _Date(params[0], params[1], params[2]);
}

return new _Date(2020, 0, 1);
}) as any;
// global.Date = vi.fn((...params) => params.length > 0 ? new _Date(params[0], params[1], params[2]) : new _Date(2020, 0, 1)) as any;
global.Date.UTC = _Date.UTC;

describe('Test today function', () => {
const textFunction = new Today(FUNCTION_NAMES_DATE.TODAY);
Expand Down