def func_linest(self): func = self.value.lower().strip('(') code = '{}({}'.format(func, self.comma_join_emit()) if not self.cell or not self.cell.excel: degree, coef = -1, -1 else: # linests are often used as part of an array formula spanning # multiple cells, one cell for each coefficient. We have to # figure out where we currently are in that range. degree, coef = get_linest_degree(self.cell) # if we are the only linest (degree is one) and linest is nested # return vector, else return the coef. if func == "linest": code += ", degree=%s)" % degree else: code += ")" if not (degree == 1 and self.parent): # pragma: no branch code += "[%s]" % (coef - 1) return code
def test_get_linest_degree(): # build a spreadsheet with linest formulas horiz and vert class Excel: def __init__(self, columns, rows): self.columns = columns self.rows = rows def get_formula_from_range(self, address): addr = AddressRange.create(address) found = addr.column in self.columns and str(addr.row) in self.rows return '=linest()' if found else '' class Cell: def __init__(self, excel): self.excel = excel self.address = AddressCell('E5') @property def sheet(self): return 'PhonySheet' @property def formula(self): return '=linest()' assert (1, 1) == get_linest_degree(Cell(Excel('E', '5'))) assert (4, 5) == get_linest_degree(Cell(Excel('E', '12345'))) assert (4, 4) == get_linest_degree(Cell(Excel('E', '23456'))) assert (4, 3) == get_linest_degree(Cell(Excel('E', '34567'))) assert (4, 2) == get_linest_degree(Cell(Excel('E', '45678'))) assert (4, 1) == get_linest_degree(Cell(Excel('E', '56789'))) assert (4, 5) == get_linest_degree(Cell(Excel('ABCDE', '5'))) assert (4, 4) == get_linest_degree(Cell(Excel('BCDEF', '5'))) assert (4, 3) == get_linest_degree(Cell(Excel('CDEFG', '5'))) assert (4, 2) == get_linest_degree(Cell(Excel('DEFGH', '5'))) assert (4, 1) == get_linest_degree(Cell(Excel('EFGHI', '5')))