f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/main/js/formulas/math/DIVIDE.ts
-rw-r--r--
1193
 1import { DivException } from "../../errors/DivException";
 2import { F7Exception } from "../../errors/F7Exception";
 3import { SheetColumnRowKey } from "../../models/common/SheetColumnRowKey";
 4import { Complex } from "../../models/common/Types";
 5import { Converters } from "../../utils/Converters";
 6import { AbstractFormula } from "../AbstractFormula";
 7import { FormulaName } from "../FormulaName";
 8
 9export class DIVIDE extends AbstractFormula {
10  static SELF: DIVIDE = new DIVIDE();
11  NAME = FormulaName.DIVIDE;
12
13  internal(origin: SheetColumnRowKey, ...values: Array<Complex>) {
14    AbstractFormula.checkLength(values.length, 2, this.NAME);
15    const first = Converters.first(this.collateralLookup(origin, values[0]));
16    const second = Converters.first(this.collateralLookup(origin, values[1]));
17    if (first instanceof F7Exception) {
18      return first;
19    }
20    if (second instanceof F7Exception) {
21      return second;
22    }
23    const firstNumber = Converters.toNumber(first);
24    const secondNumber = Converters.toNumber(second);
25    if (secondNumber === 0) {
26      throw new DivException("Formula DIVIDE parameter 2 cannot be zero.");
27    }
28    return firstNumber / secondNumber;
29  }
30}