-
-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formula): today function, set numfmt data (#1295)
* feat(formula): today function, set numfmt data * feat(formula): supports numfmt * test(formula): set numfmt data * fix(formula): sum get numfmt from link cell * fix(sheet): recover status bar * fix(formula): set numfmt on formula cell * feat(formula): date function single number * feat(formula): date function with array * fix(formula): date function test, link to formatted cell supports format * fix(formula): date serial calculation * fix(formula): function calculate null params, abs gets 0 when linked to blank cell * fix(formula): concatenate calculate zero value * fix(formula): sum,max,min .etc add blank params check * test(formula): new test template * fix(formula): date function gets error when date before 1900.1.1 * feat(formula): add edate function * test(formula): test edate function * feat(formula): year,month,day functions * feat(formula): match, xmatch functions * test(formula): xmatch function test case * test(formula): nested functions test * fix(formula): formula cell shows format, cell calculation sets percentage * fix(formula): edate,month,year functions support blank cell * fix(formula): xmatch description
- Loading branch information
Showing
92 changed files
with
3,146 additions
and
785 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { describe, expect, it } from 'vitest'; | ||
import { excelDateSerial, excelSerialToDate, formatDateDefault } 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); | ||
}); | ||
|
||
it('Function excelSerialToDate', () => { | ||
expect(formatDateDefault(excelSerialToDate(59))).toBe('1900/02/28'); | ||
expect(formatDateDefault(excelSerialToDate(61))).toBe('1900/03/01'); | ||
expect(formatDateDefault(excelSerialToDate(367))).toBe('1901/01/01'); | ||
expect(formatDateDefault(excelSerialToDate(45324))).toBe('2024/02/02'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
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 { | ||
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 | ||
} | ||
|
||
export function excelSerialToDate(serial: number): Date { | ||
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 | ||
|
||
let dayDifference = Math.floor(serial) - 1; // Adjust for Excel serial number starting from 1 | ||
|
||
// If the serial number corresponds to a date later than February 28, 1900, adjust the day difference | ||
if (dayDifference > (leapDayDate.getTime() - baseDate.getTime()) / (1000 * 3600 * 24)) { | ||
dayDifference -= 1; | ||
} | ||
|
||
const resultDate = new Date(baseDate.getTime() + dayDifference * (1000 * 3600 * 24)); | ||
return resultDate; | ||
} | ||
|
||
export function formatDateDefault(date: Date): string { | ||
// Get the year from the date object | ||
const year: number = date.getFullYear(); | ||
|
||
// Get the month from the date object and add 1 (since getMonth() returns 0-11) | ||
// Convert it to a string and pad with zero if necessary to ensure two digits | ||
const month: string = (date.getMonth() + 1).toString().padStart(2, '0'); | ||
|
||
// Get the day from the date object | ||
// Convert it to a string and pad with zero if necessary to ensure two digits | ||
const day: string = date.getDate().toString().padStart(2, '0'); | ||
|
||
// Concatenate year, month, and day with '/' as separator to form yyyy/mm/dd format | ||
return `${year}/${month}/${day}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
packages/engine-formula/src/commands/mutations/set-numfmt-formula-data.mutation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright 2023-present DreamNum Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { IMutation } from '@univerjs/core'; | ||
import { CommandType } from '@univerjs/core'; | ||
import type { IAccessor } from '@wendellhu/redi'; | ||
|
||
import type { INumfmtItemMap } from '../../basics/common'; | ||
import { FormulaDataModel } from '../../models/formula-data.model'; | ||
|
||
export interface ISetNumfmtFormulaDataMutationParams { | ||
numfmtItemMap: INumfmtItemMap; | ||
} | ||
|
||
export const SetNumfmtFormulaDataMutation: IMutation<ISetNumfmtFormulaDataMutationParams> = { | ||
id: 'formula.mutation.set-numfmt-formula-data', | ||
type: CommandType.MUTATION, | ||
handler: (accessor: IAccessor, params: ISetNumfmtFormulaDataMutationParams) => { | ||
const formulaDataModel = accessor.get(FormulaDataModel); | ||
formulaDataModel.updateNumfmtItemMap(params.numfmtItemMap); | ||
return true; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.