mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-06 11:09:05 +03:00
Spreadsheet: Add max(If)/min(If) function for ranges
This commit is contained in:
parent
6c35419236
commit
99425c5adc
Notes:
sideshowbarker
2024-07-17 18:11:02 +09:00
Author: https://github.com/u9g Commit: https://github.com/SerenityOS/serenity/commit/99425c5adc Pull-request: https://github.com/SerenityOS/serenity/pull/12767 Reviewed-by: https://github.com/alimpfard
@ -359,6 +359,22 @@ function averageIf(condition, cells) {
|
||||
return sumAndCount[0] / sumAndCount[1];
|
||||
}
|
||||
|
||||
function maxIf(condition, cells) {
|
||||
return Math.max(...numericResolve(cells).filter(condition));
|
||||
}
|
||||
|
||||
function max(cells) {
|
||||
return maxIf(() => true, cells);
|
||||
}
|
||||
|
||||
function minIf(condition, cells) {
|
||||
return Math.min(...numericResolve(cells).filter(condition));
|
||||
}
|
||||
|
||||
function min(cells) {
|
||||
return minIf(() => true, cells);
|
||||
}
|
||||
|
||||
function median(cells) {
|
||||
const values = numericResolve(cells);
|
||||
|
||||
@ -713,6 +729,48 @@ averageIf.__documentation = JSON.stringify({
|
||||
},
|
||||
});
|
||||
|
||||
max.__documentation = JSON.stringify({
|
||||
name: "max",
|
||||
argc: 1,
|
||||
argnames: ["the range"],
|
||||
doc: "Gets the largest cell's value in the range",
|
||||
examples: {
|
||||
"max(R`A1:C4`)": "Finds the largest number within A1:C4",
|
||||
},
|
||||
});
|
||||
|
||||
maxIf.__documentation = JSON.stringify({
|
||||
name: "max",
|
||||
argc: 1,
|
||||
argnames: ["condition", "the range"],
|
||||
doc: "Gets the largest cell's value in the range which evaluates to true when passed to `condition`",
|
||||
examples: {
|
||||
"maxIf(x => x > 4, R`A1:C4`)":
|
||||
"Finds the largest number within A1:C4 that is greater than 4",
|
||||
},
|
||||
});
|
||||
|
||||
min.__documentation = JSON.stringify({
|
||||
name: "min",
|
||||
argc: 1,
|
||||
argnames: ["the range"],
|
||||
doc: "Gets the smallest cell's value in the range",
|
||||
examples: {
|
||||
"min(R`A1:C4`)": "Finds the smallest number within A1:C4",
|
||||
},
|
||||
});
|
||||
|
||||
minIf.__documentation = JSON.stringify({
|
||||
name: "min",
|
||||
argc: 1,
|
||||
argnames: ["condition", "the range"],
|
||||
doc: "Gets the smallest cell's value in the range which evaluates to true when passed to `condition`",
|
||||
examples: {
|
||||
"minIf(x => x > 4, R`A1:C4`)":
|
||||
"Finds the smallest number within A1:C4 that is greater than 4",
|
||||
},
|
||||
});
|
||||
|
||||
median.__documentation = JSON.stringify({
|
||||
name: "median",
|
||||
argc: 1,
|
||||
|
@ -73,6 +73,7 @@ describe("Statistics", () => {
|
||||
sheet.makeCurrent();
|
||||
|
||||
for (let i = 0; i < 10; ++i) sheet.setCell("A", i, `${i}`);
|
||||
for (let i = 0; i < 10; ++i) sheet.setCell("B", i, `${i * i}`);
|
||||
|
||||
test("sum", () => {
|
||||
expect(sum).toBeDefined();
|
||||
@ -104,6 +105,26 @@ describe("Statistics", () => {
|
||||
expect(averageIf(x => !Number.isNaN(x), R`A0:A10`)).toEqual(4.5);
|
||||
});
|
||||
|
||||
test("minIf", () => {
|
||||
expect(minIf).toBeDefined();
|
||||
expect(minIf(x => x > 25, R`B0:B9`)).toEqual(36);
|
||||
});
|
||||
|
||||
test("min", () => {
|
||||
expect(min).toBeDefined();
|
||||
expect(min(R`B0:B9`)).toEqual(0);
|
||||
});
|
||||
|
||||
test("maxIf", () => {
|
||||
expect(maxIf).toBeDefined();
|
||||
expect(maxIf(x => x > 25, R`B0:B9`)).toEqual(81);
|
||||
});
|
||||
|
||||
test("max", () => {
|
||||
expect(max).toBeDefined();
|
||||
expect(max(R`B0:B9`)).toEqual(81);
|
||||
});
|
||||
|
||||
test("median", () => {
|
||||
expect(median).toBeDefined();
|
||||
expect(median(R`A0:A9`)).toEqual(4.5);
|
||||
|
Loading…
Reference in New Issue
Block a user