예제 #1
0
    def get_named_range(self, range_string):
        """
        Returns a 2D array of cells, with optional row and column offsets.

        :param range_string: `named range` name
        :type range_string: string

        :rtype: tuples of tuples of :class:`openpyxl.cell.Cell`
        """
        named_range = self.parent.get_named_range(range_string)
        if named_range is None:
            msg = '%s is not a valid range name' % range_string
            raise NamedRangeException(msg)
        if not isinstance(named_range, NamedRange):
            msg = '%s refers to a value, not a range' % range_string
            raise NamedRangeException(msg)

        result = []
        for destination in named_range.destinations:
            worksheet, cells_range = destination

            if worksheet is not self:
                msg = 'Range %s is not defined on worksheet %s' % \
                    (cells_range, self.title)
                raise NamedRangeException(msg)

            for row in self.iter_rows(cells_range):
                result.extend(row)

        return tuple(result)
예제 #2
0
    def get_named_range(self, range_name):
        """
        Returns a 2D array of cells, with optional row and column offsets.

        :param range_name: `named range` name
        :type range_name: string

        :rtype: tuple[tuple[openpyxl.cell.Cell]]
        """
        defn = self.parent.defined_names[range_name]
        if defn.localSheetId and defn.localSheetId != self.parent.get_index(
                self):
            msg = "{0} not available in this worksheet".format(range_name)
            raise KeyError(msg)

        if defn.type != "RANGE":
            msg = '{0} refers to a value, not a range'.format(range_name)
            raise NameError(msg)

        result = []
        for title, cells_range in defn.destinations:
            ws = self.parent[title]
            if ws != self:
                raise NamedRangeException(
                    "Range includes cells from another worksheet")

            rows = ws[cells_range]
            if isinstance(rows, Cell):
                rows = [(rows, )]

            for row in rows:
                result.extend(row)

        return tuple(result)
예제 #3
0
def split_named_range(range_string):
    """Separate a named range into its component parts"""

    for range_string in SPLIT_NAMED_RANGE_RE.split(range_string)[1::2]: # Skip first and from there every second item

        match = NAMED_RANGE_RE.match(range_string)
        if match is None:
            raise NamedRangeException('Invalid named range string: "%s"' % range_string)
        else:
            match = match.groupdict()
            sheet_name = match['quoted'] or match['notquoted']
            xlrange = match['range']
            sheet_name = sheet_name.replace("''", "'") # Unescape '
            yield sheet_name, xlrange