def get_folder(self, folder_name): """Get a folder if it exists""" q = gdata.docs.service.DocumentQuery(categories=['folder'], params={'showfolders': 'true'}) for entry in (self.client.Query(q.ToUri()).entry or []): if _to_unicode(entry.title.text) == _to_unicode(folder_name): return entry return None
def get_column_index(self, wsheet, name): """Get the index of the column with the specified name, or 0 if no column matches""" header = self.get_header(wsheet) for i, column_name in enumerate(header): if _to_unicode(name) == _to_unicode(column_name): return int(i + 1) return 0
def get_cell_content(self, wsheet, row_start=0, col_start=0, row_end=0, col_end=0): """Get the text contents of the cells from the supplied spreadsheet and worksheet and from the specified cell range as a two-dimensional list. """ if str(row_start) == '0': row_start = '1' if str(col_start) == '0': col_start = '1' if str(row_end) == '0': row_end = wsheet.row_count.text if str(col_end) == '0': col_end = wsheet.col_count.text feed = (self.get_cell_feed(wsheet, row_start, col_start, row_end, col_end) or []) # Get the dimensions of the 2D-list cols = int(col_end) - int(col_start) + 1 content = [] for i, cell in enumerate(feed.entry): r = i // cols c = i - r * cols if c == 0: row = [] content.append(row) row.append(_to_unicode((cell.content.text or ""))) return content
def write_rows(self, wsheet, header, rows): """Write the supplied data rows to the worksheet, using the supplied column headers. """ # Get the keys ss_key = self.get_key(self.ssheet) ws_key = self.get_key(wsheet) try: # As a workaround for the InsertRow bugs with column names, # just use single lowercase letters as column headers to start with for i in range(0, len(header)): self.client.UpdateCell(1, i + 1, chr(97 + i), ss_key, ws_key) # Iterate over the rows and add the data to the worksheet for row in rows: row_data = {} for i, value in enumerate(row): row_data[chr(97 + i)] = unicode(value) self.client.InsertRow(row_data, ss_key, ws_key) # Lastly, substitute the one-letter header for the real string for i in range(0, len(header)): self.client.UpdateCell(1, i + 1, _to_unicode(header[i]), ss_key, ws_key) except: return False return True
def add_worksheet(self, name, rows=0, cols=0, append=False): """Add a new worksheet with the specified title to the specified spreadsheet. Will overwrite an existing worksheet with the same title unless append is True """ # Check if a worksheet with the same title exists ws = self.get_worksheet(name) if ws: # If we're appending, just return the first object in the feed if append: return ws # Otherwise, drop the existing worksheet self.client.DeleteWorksheet(ws) # Add the desired worksheet return self.client.AddWorksheet(_to_unicode(name), rows, cols, self.get_key(self.ssheet))