def add_sheet(self, worksheet, context): get_in = sh_utl.get_nested_dicts if isinstance(worksheet, str): book = get_in(self.books, context['excel'], BOOK) worksheet = book[_get_name(worksheet, book.sheetnames)] context = sh_utl.combine_dicts(context, base={'sheet': worksheet.title.upper()}) d = get_in(self.books, context['excel'], SHEETS, context['sheet']) if 'formula_references' not in d: d['formula_references'] = formula_references = { k: v['ref'] for k, v in worksheet.formula_attributes.items() if v.get('t') == 'array' and 'ref' in v } else: formula_references = d['formula_references'] if 'formula_ranges' not in d: d['formula_ranges'] = { Ranges().push(ref, context=context) for ref in formula_references.values() } return worksheet, context
def __and__(self, other): # Intersection. r = [] for rng in other.ranges: r.extend(_intersect( rng, self.ranges, format_range=self.format_range )) values = sh_utl.combine_dicts(self.values, other.values) is_set = self.is_set or other.is_set return Ranges(r, values, is_set, self.all_values and other.all_values)
def __or__(self, other): # Union. base = self.ranges for r0 in other.ranges: stack = [r0] for b in base: s = stack.copy() stack = [] for r in s: stack.extend(_split(b, r, format_range=self.format_range)) base += tuple(stack) values = sh_utl.combine_dicts(self.values, other.values) return Ranges(base, values, True, self.all_values and other.all_values)
def push(self, ref, value=sh_utl.EMPTY, context=None): context = context or {} m = _re_range.match(ref).groupdict().items() m = {k: v for k, v in m if v is not None} if 'ref' in m: raise ValueError i = sh_utl.combine_dicts(context, m) rng = dict(self.format_range(i, ['name', 'n1', 'n2'])) self.ranges += rng, if value is not sh_utl.EMPTY: if not isinstance(value, Array): value = np.asarray(value, object) shape = _shape(**rng) value = _reshape_array_as_excel(value, shape) self.values[rng['name']] = (rng, value) else: self.all_values = False return self
def _split(base, rng, intersect=None, format_range=_range2parts().dsp): z = _have_intersect(base, rng) if not z: return rng, if intersect is not None: intersect.update(z) ranges = [] rng = sh_utl.selector(('excel', 'sheet', 'n1', 'n2', 'r1', 'r2'), rng) rng['r1'], rng['r2'] = int(rng['r1']), int(rng['r2']) for i in ('n1', 'n2', 'r1', 'r2'): if z[i] != rng[i]: n = 1 - 2 * (int(i[1]) // 2) j = '%s%d' % (i[0], 2 - int(i[1]) // 2) r = sh_utl.combine_dicts(rng, {j: z[i] - n}) r['r1'], r['r2'] = str(r['r1']), str(r['r2']) r = dict(format_range(r, ['name', 'n1', 'n2'])) ranges.append(r) rng[i] = z[i] return tuple(ranges)
def __add__(self, other): # Expand. ranges = self.ranges[1:] + other.ranges rng = sh_utl.selector(self.input_fields, self.ranges[0]) for k in ('r1', 'r2', 'n1', 'n2'): rng[k] = int(rng[k]) for r in ranges: if not _has_same_sheet(rng, r): raise RangeValueError('{}:{}'.format(self, other)) else: rng['r1'] = min(rng['r1'], int(r['r1'])) rng['n1'] = min(rng['n1'], int(r['n1'])) rng['r2'] = max(rng['r2'], int(r['r2'])) rng['n2'] = max(rng['n2'], int(r['n2'])) rng = dict(self.format_range(rng, ['name', 'n1', 'n2'])) all_values, values = self.all_values and other.all_values, None if all_values: values = sh_utl.combine_dicts(self.values, other.values) value = _assemble_values(rng, values) return Ranges().push(rng['name'], value) return Ranges((rng,), all_values=False)
def add_cell(self, cell, context, references=None, formula_references=None, formula_ranges=None, external_links=None): get_in = sh_utl.get_nested_dicts if formula_references is None: formula_references = get_in(self.books, context['excel'], SHEETS, context['sheet'], 'formula_references') if formula_ranges is None: formula_ranges = get_in(self.books, context['excel'], SHEETS, context['sheet'], 'formula_ranges') if references is None: references = get_in(self.books, context['excel'], 'references') if external_links is None: external_links = get_in(self.books, context['excel'], 'external_links') context = sh_utl.combine_dicts(context, base={'external_links': external_links}) crd = cell.coordinate crd = formula_references.get(crd, crd) cell = Cell(crd, cell.value, context=context).compile() if cell.output in self.cells: return if cell.value is not sh_utl.EMPTY: if any(not (cell.range - rng).ranges for rng in formula_ranges): return cell.update_inputs(references=references) if cell.add(self.dsp, context=context): self.cells[cell.output] = cell return cell
def __call__(self, *cells): base = self.range.ranges[0] values = sh_utl.combine_dicts(*(c.values for c in cells)) return _assemble_values(base, values, sh_utl.EMPTY)