Esempio n. 1
0
File: range.py Progetto: Afelio/PTVS
    def _set_from_collapsed_matrix(self, matrix):
        """Sets this range's cell values to reflect the contents of `matrix.` The matrix is sliced
        with the range's sheet-wide indices (not relative to the range). In other words,
        if this range contains sheet cell (r, c), and matrix[r, c] is a valid index,
        the cell (r, c) is set to matrix[r, c]"""
        for area, bounds in self._area_bounds:
            (row_start, row_stop), (col_start, col_stop) = bounds
            val_2d = matrix[row_start:row_stop + 1, col_start:col_stop + 1]

            # matrix may cover a subset of the range, e.g. setting A:A with [1,2,3]
            # This area of the full range may be completely outside the 
            # subset specified by matrix. GetResize fails if 0 is specified
            val_2d_rows = len(val_2d)
            if not val_2d_rows: continue
            val_2d_cols = len(val_2d[0])
            if not val_2d_cols: continue

            if val_2d_rows < (row_stop - row_start + 1) or val_2d_cols < (col_stop - col_start + 1):
                # We resize the area such that the top-left corner stays the same, but its size equals
                # that of the value array. Assigning to the un-resized area would change all cells, rather
                # than just those with a corresponding element in val_2d
                area = area.GetResize(val_2d_rows, val_2d_cols)
            
            CacheManager.invalidate_all_caches()    
            area.Value = val_2d
Esempio n. 2
0
    def _set_from_collapsed_matrix(self, matrix):
        """Sets this range's cell values to reflect the contents of `matrix.` The matrix is sliced
        with the range's sheet-wide indices (not relative to the range). In other words,
        if this range contains sheet cell (r, c), and matrix[r, c] is a valid index,
        the cell (r, c) is set to matrix[r, c]"""
        for area, bounds in self._area_bounds:
            (row_start, row_stop), (col_start, col_stop) = bounds
            val_2d = matrix[row_start:row_stop + 1, col_start:col_stop + 1]

            # matrix may cover a subset of the range, e.g. setting A:A with [1,2,3]
            # This area of the full range may be completely outside the 
            # subset specified by matrix. GetResize fails if 0 is specified
            val_2d_rows = len(val_2d)
            if not val_2d_rows: continue
            val_2d_cols = len(val_2d[0])
            if not val_2d_cols: continue

            if val_2d_rows < (row_stop - row_start + 1) or val_2d_cols < (col_stop - col_start + 1):
                # We resize the area such that the top-left corner stays the same, but its size equals
                # that of the value array. Assigning to the un-resized area would change all cells, rather
                # than just those with a corresponding element in val_2d
                area = area.GetResize(val_2d_rows, val_2d_cols)
            
            CacheManager.invalidate_all_caches()    
            area.Value = val_2d
Esempio n. 3
0
 def _reserve_column_space(self, range):
     """Reserve at least the requested range for new Table columns. The given range
     is assumed to be adjacent (on the right) of this Table. If unable to insert the given range,
     (e.g. because it would break a table further to the right), full (worksheet) columns are inserted instead."""
     CacheManager.invalidate_all_caches()   
     # xlFormatFromLeftOrAbove encourages consistent formatting with the original table (to the left)
     try:
         range._full_xlRange.Insert(CopyOrigin=com_utils.constants.xlFormatFromLeftOrAbove, Shift=com_utils.constants.xlToRight)
     except com_utils.com_error:
         # Oops, insert failed. This is probably because Excel is refusing to break a right-adjacent table
         # We try again, inserting a whole column. This also breaks things in many cases, but at Excel doesn't complain
         range._full_xlRange.EntireColumn.Insert(CopyOrigin=com_utils.constants.xlFormatFromLeftOrAbove, Shift=com_utils.constants.xlToRight)
Esempio n. 4
0
 def _reserve_column_space(self, range):
     """Reserve at least the requested range for new Table columns. The given range
     is assumed to be adjacent (on the right) of this Table. If unable to insert the given range,
     (e.g. because it would break a table further to the right), full (worksheet) columns are inserted instead."""
     CacheManager.invalidate_all_caches()   
     # xlFormatFromLeftOrAbove encourages consistent formatting with the original table (to the left)
     try:
         range._full_xlRange.Insert(CopyOrigin=com_utils.constants.xlFormatFromLeftOrAbove, Shift=com_utils.constants.xlToRight)
     except com_utils.com_error:
         # Oops, insert failed. This is probably because Excel is refusing to break a right-adjacent table
         # We try again, inserting a whole column. This also breaks things in many cases, but at Excel doesn't complain
         range._full_xlRange.EntireColumn.Insert(CopyOrigin=com_utils.constants.xlFormatFromLeftOrAbove, Shift=com_utils.constants.xlToRight)
Esempio n. 5
0
File: tools.py Progetto: xNUTs/PTVS
def filter(func, range):
    """Filters rows or columns by applying `func` to the given range.
    `func` is called for each value in the range. If it returns False,
    the corresponding row / column is hidden. Otherwise, the row / column is
    made visible.

    `range` must be a row or column vector. If it is a row vector, columns are hidden, and vice versa.
    
    Note that, to unhide rows / columns, `range` must include hidden cells. For example, to unhide a range:
       xl.filter(lambda v: True, some_vector.including_hidden)"""
    # $$$ maybe we should kill scalar ranges
    if not (range.shape in (Scalar, RowVector, ColumnVector)):
        raise ValueError("range must be a vector or scalar")

    hide_dim = range._vector_dim.other
    with CacheManager.caching_disabled():
        for cell in range.itercells():
            assert cell.shape is Scalar
            visible = bool(func(cell.get()))
            hide_dim.entire(cell._full_xlRange).Hidden = not visible
Esempio n. 6
0
def filter(func, range):
    """Filters rows or columns by applying `func` to the given range.
    `func` is called for each value in the range. If it returns False,
    the corresponding row / column is hidden. Otherwise, the row / column is
    made visible.

    `range` must be a row or column vector. If it is a row vector, columns are hidden, and vice versa.
    
    Note that, to unhide rows / columns, `range` must include hidden cells. For example, to unhide a range:
       xl.filter(lambda v: True, some_vector.including_hidden)"""
    # $$$ maybe we should kill scalar ranges
    if not (range.shape in (Scalar, RowVector, ColumnVector)):
        raise ValueError("range must be a vector or scalar")

    hide_dim = range._vector_dim.other
    with CacheManager.caching_disabled():
        for cell in range.itercells():
            assert cell.shape is Scalar
            visible = bool( func(cell.get()) )
            hide_dim.entire(cell._full_xlRange).Hidden = not visible