[英]Using a Python list to populate in a drop down validation
I am using openpyxl
to manipulate a spreadsheet from Python.我正在使用openpyxl
从 Python 操作电子表格。
I am trying to create a drop-down validation in a workbook tab called organisation
.我正在尝试在名为organisation
的工作簿选项卡中创建下拉验证。 Is it possible to use a Python list to populate the elements in the drop down selection?是否可以使用 Python 列表来填充下拉选择中的元素?
When I hardcode the drop down options to into the DataValidation
line like so:当我将下拉选项硬编码到DataValidation
行时,如下所示:
dv = DataValidation(type="list", formula1="The", "earth", "revolves", "around", "sun", allow_blank=True)
The drop down is created in the spreadsheet tab and populated with the options as expected.下拉列表是在电子表格选项卡中创建的,并按预期填充了选项。
However when I try to add the drop down options using Python list and then pass to the DataValidation
line like so:但是,当我尝试使用 Python 列表添加下拉选项,然后像这样传递到DataValidation
行时:
valid = ['"The,earth,revolves,around,sun"']
dv = DataValidation(type="list", formula1=valid, allow_blank=True)
the drop down list is not created.未创建下拉列表。
For extra information please see the full script:有关更多信息,请参阅完整脚本:
def addValidationDropDowns(path):
valid = ['"The,earth,revolves,around,sun"']
wb = openpyxl.load_workbook(path)
ws = wb['organisation']
dv = DataValidation(type="list", formula1=valid, allow_blank=True)
ws.add_data_validation(dv)
for x in range(0, 3):
dv.add(ws["A"+str(x+10)])
wb.save(path)
return
I struggle with this the first time i did it.我第一次这样做时就为此苦苦挣扎。 It is curious that if the 'type' paramater or DataValidation is "list" you think ¡ok, let's use a list.奇怪的是,如果“类型”参数或 DataValidation 是“列表”,您认为“好吧,让我们使用一个列表”。 but no.但不是。 it is expecting a string.它期待一个字符串。 I think your example will work if you remove the square brackets to the 'valid' variable.我认为,如果您删除“有效”变量的方括号,您的示例将起作用。
valid = '"The,earth,revolves,around,sun"'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.