简体   繁体   中英

Autocompletion in Excel data validation using Python

I want to create an Excel sheet using openpyxl and Python 3 with data validation feature - please see my Python code below. My problem is that I need the data validation to be more than selection from a list. Suppose that the list is very long, then it will be inconvenient for user to select from a list. Therefore, is there a way to generate this excel sheet from Python, where auto completion feature is allowed? By auto completion, I mean that while the user is typing a match to the list is performed, and suggestions to match to the list is given.

from openpyxl import Workbook
from openpyxl.worksheet.datavalidation import DataValidation

# Create the workbook and worksheet we'll be working with
wb = Workbook()
ws = wb.active

# Create a data-validation object with list validation
dv = DataValidation(type="list", formula1='"Dog,Cat,Bat"', allow_blank=True)

# Optionally set a custom error message
dv.error ='Your entry is not in the list'
dv.errorTitle = 'Invalid Entry'

# Optionally set a custom prompt message
dv.prompt = 'Please select from the list'
dv.promptTitle = 'List Selection'

# Add the data-validation object to the worksheet
ws.add_data_validation(dv)

c1 = ws["A1"]
c1.value = "Dog"
dv.add(c1)
c2 = ws["A2"]

c2.value = "An invalid value"
dv.add(c2)

# Or, apply the validation to a range of cells

# Write the sheet out. If you now open the sheet in Excel, you'll find that
# the cells have data-validation applied.
wb.save("test.xlsx")

What openpyxl created is just normal spreadsheet files, they're container of data.

Assuming users will access these files using a modern Excel(or alike) application, that autocompletion function will be provided by that application(client app). It is independent of the program that generated the.xlsx files. And for example, let's assume that the user's client-side spreadsheet app doesn't have autocompletion, I don't think you can add this just through.xlsx files. You'll have to develop your own.xlsx file accessing app with this autocompletion function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM