def trim_columns(self): """ Attempt to trim off any extraneous columns """ self.get_cell_feed() for col in range(1, int(self.cols) + 1): if self.data["1"].get(str(col)) is not None: continue print('Column Delete Candidate %s' % (col, )) found_data = False for row in range(1, int(self.rows) + 1): _v = self.data.get(str(row), {}).get(str(col)) if _v not in [None, 'n/a', 'did not collect']: found_data = True print(('ERROR row: %s has data: %s') % (row, self.data[str(row)][str(col)])) if not found_data: print('Deleting column %s' % (col, )) if col == int(self.cols): self.drop_last_column() return True # Move columns left updateFeed = spdata.build_batch_cells_update( self.spread_id, self.id) for col2 in range(int(col), int(self.cols)): for row in range(1, int(self.rows) + 1): updateFeed.add_set_cell( str(row), str(col2), self.get_cell_value(row, col2 + 1)) self.cell_feed = exponential_backoff(self.spr_client.batch, updateFeed, force=True) # Drop last column self.refetch_feed() self.drop_last_column() return True return False
def trim_columns(self): """ Attempt to trim off any extraneous columns """ self.get_cell_feed() for col in range(1, int(self.cols)+1): if self.data["1"].get(str(col)) is not None: continue print 'Column Delete Candidate %s' % (col,) found_data = False for row in range(1, int(self.rows)+1): if self.data.get(str(row), {}).get(str(col)) is not None: found_data = True print 'ERROR row: %s has data: %s' % (row, self.data[str(row)][str(col)]) if not found_data: print 'Deleting column %s' % (col,) if col == int(self.cols): self.drop_last_column() return True # Move columns left updateFeed = spdata.build_batch_cells_update(self.spread_id, self.id) for col2 in range(int(col), int(self.cols)): for row in range(1, int(self.rows)+1): updateFeed.add_set_cell(str(row), str(col2), self.get_cell_value(row, col2 + 1)) self.cell_feed = self.spr_client.batch(updateFeed, force=True) # Drop last column self.refetch_feed() self.drop_last_column() return True return False
def del_column(self, label): """ Delete a column from the worksheet that has a given label this also zeros out any data in the column too """ self.get_cell_feed() for col in range(1, int(self.cols)+1): if self.get_cell_value(1, col) == label: print 'Found %s in column %s, deleting column' % (label, col) entry = self.get_cell_entry(1, col) entry.cell.input_value = "" self.spr_client.update(entry) updateFeed = spdata.build_batch_cells_update(self.spread_id, self.id) for row in range(1, int(self.rows)+1): updateFeed.add_set_cell(str(row), str(col), "") self.cell_feed = self.spr_client.batch(updateFeed, force=True) self.refetch_feed() while self.trim_columns(): print 'Trimming Columns!'
def del_column(self, label, sloppy=False): """ Delete a column from the worksheet that has a given label this also zeros out any data in the column too Args: label (str): the column label based on the first row's value sloppy (bool): should we only find that the contents start the value """ self.get_cell_feed() worked = False for col in range(1, int(self.cols) + 1): if self.get_cell_value(1, col) != label and not sloppy: continue if sloppy and not self.get_cell_value(1, col).startswith(label): continue worked = True print('Found %s in column %s, deleting column' % (label, col)) entry = self.get_cell_entry(1, col) entry.cell.input_value = "" exponential_backoff(self.spr_client.update, entry) updateFeed = spdata.build_batch_cells_update( self.spread_id, self.id) for row in range(1, int(self.rows) + 1): updateFeed.add_set_cell(str(row), str(col), "") self.cell_feed = exponential_backoff(self.spr_client.batch, updateFeed, force=True) if not worked: print("Error, did not find column |%s| for deletion" % (label, )) print("The columns were:") for col in range(1, int(self.cols) + 1): print(" %2i |%s|" % (col, self.get_cell_value(1, col))) return self.refetch_feed() while self.trim_columns(): print('Trimming Columns!')
def del_column(self, label, sloppy=False): """ Delete a column from the worksheet that has a given label this also zeros out any data in the column too Args: label (str): the column label based on the first row's value sloppy (bool): should we only find that the contents start the value """ self.get_cell_feed() worked = False for col in range(1, int(self.cols)+1): if self.get_cell_value(1, col) != label and not sloppy: continue if sloppy and not self.get_cell_value(1, col).startswith(label): continue worked = True print 'Found %s in column %s, deleting column' % (label, col) entry = self.get_cell_entry(1, col) entry.cell.input_value = "" exponential_backoff(self.spr_client.update, entry) updateFeed = spdata.build_batch_cells_update(self.spread_id, self.id) for row in range(1, int(self.rows)+1): updateFeed.add_set_cell(str(row), str(col), "") self.cell_feed = exponential_backoff(self.spr_client.batch, updateFeed, force=True) if not worked: print("Error, did not find column |%s| for deletion" % (label,)) print("The columns were:") for col in range(1, int(self.cols)+1): print(" %2i |%s|" % (col, self.get_cell_value(1, col))) return self.refetch_feed() while self.trim_columns(): print 'Trimming Columns!'