async def setAlignment(message, embed, creator, payload): url = embed.author.url ranges = (await getDetailFromHistory(message, creator)).upper() while " " in ranges: ranges = ranges.replace(" ", " ") if (ranges.split(" ")[0] not in ["ALL", "NONE"]): ranges = [ x.strip() for x in re.sub(r'[^A-Z0-9: ]', '', ranges).split(" ") ] # keep good chars, split into sep ranges for i in range(len(ranges)): ranges[i] = numericToA1(a1ToNumeric(ranges[i])) ranges = ", ".join(ranges) else: ranges = ranges.title() if (payload.emoji.name == RandomSupport.numberEmojis[5]): embed = updateDetailInURL(embed, "leftAligned", ranges.replace(" ", "")) elif (payload.emoji.name == RandomSupport.numberEmojis[6]): embed = updateDetailInURL(embed, "rightAligned", ranges.replace(" ", "")) elif (payload.emoji.name == RandomSupport.numberEmojis[7]): embed = updateDetailInURL(embed, "centered", ranges.replace(" ", "")) ranges = ranges.split(",") for i in range(len(ranges)): if (ranges[i][-1] == "0"): ranges[i] = ranges[i][:-1] embed = updateFieldValue(embed, payload.emoji.name, spaceChar * 2 + " " + "".join(ranges)) await message.edit(embed=embed)
async def setTableRange(message, embed, creator): moBotMember = message.guild.get_member(moBot) url = embed.author.url tableDetails = getTableFromEmbedURL(url) workbook = await openUserSpreadsheet(tableDetails.workbookKey, message.channel, creator, moBotMember) if (workbook is not None): tableRange = await getDetailFromHistory(message, creator) if (tableRange is not None): tableRange = numericToA1( a1ToNumeric(tableRange)) # validated range embed = updateDetailInURL(embed, "tableRange", tableRange) embed = updateFieldValue( embed, "Table Range", spaceChar * 2 + " " + (tableRange if tableRange[-1] != "0" else tableRange[:-1])) await message.edit(embed=embed) else: await messageNotFound(message, creator, "Table Range", RandomSupport.numberEmojis[2]) else: return
def createTable(tableDetails, workbook): worksheetID = tableDetails.worksheetID tableRange = tableDetails.tableRange if ("-1" not in [worksheetID, tableRange]): # if we have 2 valid inputs sheet = None for worksheet in workbook.worksheets(): if (str(worksheet.id) == str(worksheetID)): sheet = worksheet rnge = None if (sheet is not None): tableRange = a1ToNumeric(tableRange) cols = tableRange[0] rows = tableRange[1] cols[1] = sheet.col_count if cols[1] == 0 else cols[1] rows[1] = sheet.row_count if rows[1] == 0 else rows[1] rnge = sheet.range(rows[0], cols[0], rows[1], cols[1]) table = RandomSupport.arrayFromRange( rnge) # table[row[col, ...], row[col, ...]] / table[row][col] maxWidths = [1 for cell in table[0]] # set widths to 1 for i in range(len(table)): # loop rows, skipping the headers if ("".join([table[i][k].value for k in range(len(table[i]))]) == ""): break for j in range(len(table[0])): # loop columns maxWidths[j] = len(table[i][j].value) if len( table[i][j].value) > maxWidths[j] else maxWidths[j] headerWidths = [[1 for cell in row] for row in table[:tableDetails.headers]] for i in range(tableDetails.headers): # loop row j = 0 while j < len(headerWidths[i]): # loop col countAdjBlanks = 0 for k in range(j + 1, len(table[i])): if (table[i][k].value.strip() == ""): countAdjBlanks += 1 else: break if (countAdjBlanks > 0 and tableDetails.smartMerging == 1): headerWidths[i][j] = sum( maxWidths[j:j + 1 + countAdjBlanks]) + countAdjBlanks * 3 for k in range(j + 1, j + 1 + countAdjBlanks): table[i][k].value = "MoBot Merged" else: # need to figure out when header width > table width headerWidths[i][j] = maxWidths[j] j += 1 tables = [ ] # will be completed tables seperated to fit in a single message lines = [] for row in table: isHeader = table.index(row) < tableDetails.headers if ("".join([cell.value for cell in row]) == ""): # if blank row break line = "" for cell in row: tempLines = [ ["" for i in range(3)] for i in range(3) ] # templines[i][0 and 1] are for top and bottom border, but not in use right now... if (not isHeader): for r in tableDetails.leftAligned: if ( RandomSupport.cellInRange( cell, a1ToNumeric(r)) and not any( RandomSupport.cellInRange( cell, a1ToNumeric(r2)) for r2 in tableDetails. leftAligned[:tableDetails. leftAligned.index(r)]) ): # in range but not in any other ranges before this one... tempLines[0][1] += " %s " % (cell.value.ljust( maxWidths[row.index(cell)], " ")) tempLines[0][1] += '|' if ( row.index(cell) + 1 != len(row)) else "" for r in tableDetails.rightAligned: if ( RandomSupport.cellInRange( cell, a1ToNumeric(r)) ): # in range but not in any other ranges before this one... tempLines[1][1] += " %s " % (cell.value.rjust( maxWidths[row.index(cell)], " ")) tempLines[1][1] += '|' if ( row.index(cell) + 1 != len(row)) else "" for r in tableDetails.centered: if ( RandomSupport.cellInRange( cell, a1ToNumeric(r)) and not any( RandomSupport.cellInRange( cell, a1ToNumeric(r2)) for r2 in tableDetails. centered[:tableDetails.centered. index(r)]) ): # in range but not in any other ranges before this one... tempLines[2][1] += " %s " % (cell.value.center( maxWidths[row.index(cell)], " ")) tempLines[2][1] += '|' if ( row.index(cell) + 1 != len(row)) else "" else: if (cell.value != "MoBot Merged"): tempLines[2][1] += " %s " % (cell.value.center( headerWidths[table.index(row)][row.index( cell)], " ")) try: tempLines[2][1] += '|' if table[table.index( row)][row.index(cell) + 1:][0].value != "MoBot Merged" else "" except IndexError: # when last cell in row pass line += tempLines[-1][1] if tempLines[-1][1] != "" else ( tempLines[-2][1] if tempLines[-2][1] != "" else tempLines[-3][1]) line = "`%s`" % line if (isHeader): line += "\n` %s `" % "".center(len(line) - 4, "-") # borders for headers, if (len("\n".join(lines) + line) < 2000): lines.append(line) else: tables.append("\n".join(lines)) lines = [line] tables.append("\n".join(lines)) return tables