0

I have a script that uses xlsxwriter to generate an excel spreadsheet. Most of the cells are populated with the formatting and values one cell at a time using loops. However, we've recently had to introduce a new formula that behaves differently in that it populates an entire column with an entry into a single cell. An example is below:

=UNIQUE(FILTER('Sheet2'!A2:A10000,'Sheet2'!A2:A10000<>"")) 

This causes an issue when we try to apply the background color to all of the cells in this column (aside from the first cell in row 2). None of the other cells are getting the background color applied

I attempted to use if statements to selectively just apply background color to the rest of the column when the first row has "=UNIQUE(FILTER" in its formula, but I then hit the following error on the spam_catch in the if switch statement near the bottom of the script.

    434                     # Write record to current sheet.
    435                     if switch:
--> 436                       spam_catch = current_sheet.write(
    437                           f"{setting['column']}{row_number}",
    438                           workbook.add_format(setting["format"]),
TypeError: Unsupported type <class 'xlsxwriter.format.Format'> in write()

Code:

record_settings = []
record_dict = {
        "column": "B",
        "formula": "=UNIQUE(FILTER('Sheet2'!A2:A10000,'Sheet2'!A2:A10000<>""))",
        "format": {"bg_color": "#FDE9D9", "border": 1},
}
record_settings.append(record_dict)
# If a record is populated use data from table
if records:
    row_count = records.count()
else:
    row_count = 0
if row_count > 0:
    switch = False
    missing_column_set = set()
    for row in records.toLocalIterator():
        for index, setting in enumerate(record_settings):
            # Create validation rule if one is populated
       if setting["formula"]:
            if ("UNIQUE(FILTER" in setting["formula"] and row_number == 2) or "UNIQUE(FILTER" not in setting["formula"]:

                cell_output = setting["formula"].format(
                    row_number=row_number, prev_row_number=row_number - 1
                )
              else:
                switch = True                   
     
            # Write record to current sheet.
            if switch:
              spam_catch = current_sheet.write(
                  f"{setting['column']}{row_number}",
                  workbook.add_format(setting["format"]),
              )
              switch = False
            else:
              spam_catch = current_sheet.write(
                  f"{setting['column']}{row_number}",
                  cell_output,
                  workbook.add_format(setting["format"]),
              )
        # Increment row number
        row_number += 1

Any idea what might be wrong?

Vitalizzare
  • 4,496
  • 7
  • 13
  • 32
  • For the line where the error occurs you need to add some token to to the `write()` statement such as a blank string. The syntax is `write(row, col, token, format)`. You are missing the token. – jmcnamara Oct 21 '22 at 21:42

0 Answers0