How to use Application.GetCustomListNum in the xlwings API way
The GetCustomListNum member of the Application object in Excel is a method used to retrieve the index number of a custom list that has been defined in the Excel application. Custom lists are often utilized for sorting data in a user-defined order, such as days of the week or months, and they can also be used in functions like MATCH or VLOOKUP to align data with these custom sequences. In xlwings, this functionality is accessible through the api property, which provides direct access to the underlying Excel object model. This method is particularly useful when you need to programmatically determine the position of a specific list within Excel’s custom list collection, enabling dynamic interactions with list-based operations.
FunctionalityGetCustomListNum returns a numeric value representing the index of a custom list based on a provided list array. If the specified list matches one of the custom lists defined in Excel, the method returns its index number (starting from 1 for the first custom list). If no match is found, it returns 0. This can assist in validating or identifying custom lists before performing operations like sorting or data alignment.
Syntax
In xlwings, the method is called via the Application object. The syntax is:
index = xw.apps[0].api.GetCustomListNum(list_array)
list_array: This is a required parameter that specifies the list to be checked. It should be passed as an array or range of values. In xlwings, you can use a Python list or an Excel range object. For example, a Python list like["Mon", "Tue", "Wed"]or an xlwings range likesheet.range("A1:A3").value.
Parameters and Values
The list_array parameter must be a one-dimensional array of strings or numbers that correspond to the custom list entries in Excel. Excel stores custom lists in a specific order, and the method compares the input array to these stored lists. Note that custom lists are case-insensitive in Excel, so the matching process ignores letter case. If the input array is empty or invalid, the method may return an error or 0.
Code Examples
Here are some xlwings API code instances demonstrating the use of GetCustomListNum:
- Basic Example with a Python List: Check if a custom list for weekdays exists and get its index.
import xlwings as xw
# Connect to the active Excel instance
app = xw.apps.active
# Define a list to check (e.g., weekdays)
list_to_check = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
# Get the custom list index
list_index = app.api.GetCustomListNum(list_to_check)
print(f"The custom list index is: {list_index}")
# Output might be 1 if this is the first custom list, or 0 if not found.
- Using an Excel Range as Input: Retrieve data from a worksheet and check if it matches a custom list.
import xlwings as xw
# Open a workbook and reference a sheet
wb = xw.Book("example.xlsx")
sheet = wb.sheets["Sheet1"]
# Get values from a range (e.g., cells A1:A5)
range_values = sheet.range("A1:A5").value
# Ensure it's a flat list (xlwings returns a list of lists for 2D ranges)
if isinstance(range_values[0], list):
range_values = [item for sublist in range_values for item in sublist]
# Check for custom list match
app = xw.apps.active
list_index = app.api.GetCustomListNum(range_values)
if list_index > 0:
print(f"Custom list found at index: {list_index}")
else:
print("No matching custom list found.")
- Dynamic List Validation: Before sorting data, verify that a custom list exists to avoid errors.
import xlwings as xw
app = xw.apps.active
custom_list = ["Low", "Medium", "High"] # Example priority list
index = app.api.GetCustomListNum(custom_list)
if index == 0:
print("Warning: Custom list not defined. Consider adding it in Excel options.")
else:
# Proceed with sorting or other operations using the list index
print(f"Using custom list index {index} for sorting.")







