def cell_range(self, start_or_string_cellrange, end=None):
        if isinstance(start_or_string_cellrange, basestring) and end is None:
            start_and_end = cell_range_as_string_to_coordinates(start_or_string_cellrange)
            if start_and_end is None:
                raise ValueError("Invalid cell range '%s'" % (start_or_string_cellrange, ))
            return CellRange(self, *start_and_end)

        def convert_if_needed(cell_ref):
            if isinstance(cell_ref, basestring):
                return cell_name_to_coordinates(cell_ref)
            else:
                return cell_ref
        start_tuple = convert_if_needed(start_or_string_cellrange)
        end_tuple = convert_if_needed(end)
        if start_tuple is None:
            if end_tuple is None:
                raise ValueError('Neither %s nor %s are valid cell locations' % (start_or_string_cellrange, end))
            raise ValueError('%s is not a valid cell location' % (start_or_string_cellrange, ))
        if end_tuple is None:
            raise ValueError('%s is not a valid cell location' % (end, ))
        return CellRange(self, start_tuple, end_tuple)
Esempio n. 2
0
    def test_cell_range_as_string_to_coordinates(self):
        self.assertEquals(cell_range_as_string_to_coordinates('A2:C4'), ((1, 2), (3, 4)))
        self.assertEquals(cell_range_as_string_to_coordinates('AA10000:ZZ99999'),
                          ((27, 10000), (26 * 27, 99999)) )

        self.assertEquals(cell_range_as_string_to_coordinates('C4:A2'), ((3, 4), (1, 2)))

        self.assertIsNone(cell_range_as_string_to_coordinates('A0:C4'))
        self.assertIsNone(cell_range_as_string_to_coordinates('A2:C0'))
        self.assertIsNone(cell_range_as_string_to_coordinates('A2::C4'))
        self.assertIsNone(cell_range_as_string_to_coordinates(':A2:C4'))
        self.assertIsNone(cell_range_as_string_to_coordinates('AA:C4:'))
        self.assertIsNone(cell_range_as_string_to_coordinates('22:C4'))
        self.assertIsNone(cell_range_as_string_to_coordinates('A2:CC'))
        self.assertIsNone(cell_range_as_string_to_coordinates('A2:44'))