如何在 xlsxwriter 中的数据验证中添加超过 255 个字符 Python

How to add more than 255 characters in a data validation in xlsxwriter Python

我正在创建一个 excel sheet,我必须在其中下拉所有状态。代码如下所示:

excel_file_dir = os.path.join(os.path.dirname(__file__), 'template')
name = 'Template.xlsx'
excel_file = os.path.join(os.path.dirname(__file__), 'template', name)
workbook = xlsxwriter.Workbook(excel_file)
sheet = workbook.add_worksheet("Details")

sheet.write("C1", "Address", style_center)
sheet.write("D1", "State", style_center)

sheet.data_validation(1, 3, 5000, 3, {'validate': 'list', 'source': ["Alabama-AL", "Alaska-AK", "Arizona-AZ", "Arkansas-AR", "California-CA", "Colorado-CO", "Connecticut-CT",
                                              "Delaware-DE", "District of Columbia-DC", "Florida-FL", "Georgia-GA", "Hawaii-HI", "Idaho-ID", "Illinois-IL", "Indiana-IN",
                                              "Iowa-IA", "Kansas-KS", "Louisiana-LA", "Maine-ME", "Maryland-MD", "Massachusetts-MA", "Michigan-MI", "Minnesota-MN", "Mississippi-MS",
                                              "Missouri-MO", "Montana-MT", "Nebraska-NE", "Nevada-NV", "New Hampshire-NH", "New Jersey-NZ", "New Mexico-NM", "New York-NY",
                                              "North Carolina-NC", "North Dakota", "Ohio", "Oklahoma-OK", "Oregon-OR", "Pennsylvania-PA", "Rhode Island-RI", "South Dakota-SD",
                                              "Tennessee-TN", "Texas-TX", "Utah-UT", "Vermont-VT", "Virginia-VA", "Washington-WA", "West Virginia-WV", "Wisconsin-WI", "Wyoming-WY"]})
                                              
workbook.close()

我遇到以下错误:

UserWarning: Length of list items exceeds Excel's limit of 255, use a formula range instead

我无法理解公式范围。我试图找到一种方法来做到这一点,在其他一些文章中,它说在其他一些单元格中添加一些数据让我们说 E1 并在说 E2 中添加其余信息,然后组合这些单元格并将其放在您想要的位置excel.

所以我试着做了

sheet.write("K10", "Alabama-AL, Alaska-AK, Arizona-AZ", border_format)
sheet.write("L11", "Arkansas-AR, California-CA, Colorado-CO, Connecticut-CT", border_format)

我在K10和L11写了一些资料,然后尝试合并:

sheet.data_validation(1, 3, 5000, 3, {'validate': 'list', 'source': '=K10&" "&L11'})

但它实际上没有用,并显示空的下拉列表。

如何在 Python

中使用 xlsxwriter 将超过 255 个字符的值放入下拉列表中

编辑:

state_list = ["Alabama-AL", "Alaska-AK", "Arizona-AZ", "Arkansas-AR", "California-CA", "Colorado-CO", "Connecticut-CT",
             "Delaware-DE", "District of Columbia-DC", "Florida-FL", "Georgia-GA", "Hawaii-HI", "Idaho-ID", "Illinois-IL", "Indiana-IN",
             "Iowa-IA", "Kansas-KS", "Louisiana-LA", "Maine-ME", "Maryland-MD", "Massachusetts-MA", "Michigan-MI", "Minnesota-MN", "Mississippi-MS",
             "Missouri-MO", "Montana-MT", "Nebraska-NE", "Nevada-NV", "New Hampshire-NH", "New Jersey-NZ", "New Mexico-NM", "New York-NY",
             "North Carolina-NC", "North Dakota", "Ohio", "Oklahoma-OK", "Oregon-OR", "Pennsylvania-PA", "Rhode Island-RI", "South Dakota-SD",
             "Tennessee-TN", "Texas-TX", "Utah-UT", "Vermont-VT", "Virginia-VA", "Washington-WA", "West Virginia-WV", "Wisconsin-WI", "Wyoming-WY"]
             
i = 10
for state in state_list:
    sheet.write("K{}".format(i), state, border_format)
    i = i + 1

sheet.data_validation(1, 3, 5000, 3, {'validate': 'list', 'source': '=Details!K10:K61'})

需要说明的是,list/string 验证的 255 个字符限制是 Excel 限制。 XlsxWriter 只是强制执行该限制。

Excel(和 XlsxWriter)文档中建议的解决方法是将验证列表数据添加到工作表中,并使用像 =Details!$K:$K.

这样的公式范围来引用它

您在更新中添加的编辑示例应该可以工作。这是一个基于它的工作示例:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet("Details")


state_list = ["Alabama-AL", "Alaska-AK", "Arizona-AZ", "Arkansas-AR",
              "California-CA", "Colorado-CO", "Connecticut-CT",
              "Delaware-DE", "District of Columbia-DC", "Florida-FL",
              "Georgia-GA", "Hawaii-HI", "Idaho-ID", "Illinois-IL", "Indiana-IN",
              "Iowa-IA", "Kansas-KS", "Louisiana-LA", "Maine-ME", "Maryland-MD",
              "Massachusetts-MA", "Michigan-MI", "Minnesota-MN", "Mississippi-MS",
              "Missouri-MO", "Montana-MT", "Nebraska-NE", "Nevada-NV",
              "New Hampshire-NH", "New Jersey-NZ", "New Mexico-NM", "New York-NY",
              "North Carolina-NC", "North Dakota", "Ohio", "Oklahoma-OK",
              "Oregon-OR", "Pennsylvania-PA", "Rhode Island-RI", "South Dakota-SD",
              "Tennessee-TN", "Texas-TX", "Utah-UT", "Vermont-VT",
              "Virginia-VA", "Washington-WA", "West Virginia-WV",
              "Wisconsin-WI", "Wyoming-WY"]

worksheet.write_column('K10', state_list)
# Same as the following with row/col notation:
# worksheet.write_column(9, 10, state_list)

worksheet.data_validation(1, 3, 5000, 3,
                          {'validate': 'list',
                           'source': '=Details!$K:$K'})


workbook.close()

输出: