使用 openpyxl 将数据验证应用于不同工作表中的特定列

问题描述 投票:0回答:1

我正在尝试使用 openpyxl 将不同的数据验证应用于不同选项卡中的不同列。

当我只指定一张纸时,我的代码可以工作,但是当我同时定义三张纸时,它会变得混乱。 chatgpt 和 google bard 给了我错误的答案,只是发明了无法编译的代码,非常令人沮丧。有人可以帮忙吗?

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

# Anonymized state lists
state_list_1 = ["State1", "State2", "State3"]  # Anonymized list of financial institutions
state_list_2 = ["Yes", "No"]  # Anonymized list of yes/no options
state_list_3 = ["USD", "EUR", "CAD"]  # Anonymized list of currency codes

# Create a workbook
workbook = Workbook()
workbook.create_sheet('Tab 1')
workbook.create_sheet('Tab 2')
workbook.create_sheet('Tab 3')
workbook.create_sheet('List')
sheet_one = workbook['Tab 1']
sheet_two = workbook['Tab 2']
sheet_three = workbook['Tab 3']
sheet_four = workbook['List']

# Populate sheets with anonymized state lists
state_lists = [state_list_1, state_list_2, state_list_3]
for i, state_list in enumerate(state_lists, start=1):
    for idx, state in enumerate(state_list, start=1):
        sheet_four.cell(row=idx, column=i, value=state)

# Create data validations
data_1 = DataValidation(type="list", formula1='=List!$A:$A')
data_2 = DataValidation(type="list", formula1='=List!$B:$B')
data_3 = DataValidation(type="list", formula1='=List!$C:$C')

# Define columns to apply DataValidations
columns_to_validate = {
    'Tab 1': {5: data_2},
    'Tab 2': {8: data_2, 9: data_2, 14: data_3, 15: data_1, 23: data_2},
    'Tab 3': {6: data_3, 7: data_3, 8: data_1}
}

# Apply DataValidations to specified columns in respective sheets
for sheet_name, columns in columns_to_validate.items():
    sheet = workbook[sheet_name]
    for col_num, data in columns.items():
        column_letter = get_column_letter(col_num)
        sheet.add_data_validation(data)
        for row in range(3, sheet.max_row + 1):
            cell = f"{column_letter}{row}"
            data.add(sheet[cell])

# Saving the workbook
workbook.save('mwe.xlsx')

excel openpyxl
1个回答
0
投票

感谢您的评论。最后,我通过为每个工作表定义不同的 DataValidation 对象解决了我的问题(通过使用双循环函数,代码可能会更短,但它有效)。

`data_1_sheet_one = DataValidation(type="list", formula1='=List!$A:$A')
data_2_sheet_one = DataValidation(type="list", formula1='=List!$B:$B')
data_3_sheet_one = DataValidation(type="list", formula1='=List!$C:$C')
data_1_sheet_two = DataValidation(type="list", formula1='=List!$A:$A')
data_2_sheet_two = DataValidation(type="list", formula1='=List!$B:$B')
data_3_sheet_two = DataValidation(type="list", formula1='=List!$C:$C')
data_1_sheet_three = DataValidation(type="list", formula1='=List!$A:$A')
data_2_sheet_three = DataValidation(type="list", formula1='=List!$B:$B')
data_3_sheet_three = DataValidation(type="list", formula1='=List!$C:$C')

# Apply data validation to entire columns in different sheets
for row in range(1, 100): 
    cell = f"E{row}"
    data_2_sheet_one.add(sheet_one[cell])

sheet_one.add_data_validation(data_2_sheet_one)

for row in range(1, 100): 
    cell = f"O{row}"
    data_1_sheet_two.add(sheet_two[cell])

for row in range(1, 100): 
    cell = f"H{row}"
    data_2_sheet_two.add(sheet_two[cell])

for row in range(1, 100): 
    cell = f"I{row}"
    data_2_sheet_two.add(sheet_two[cell])

for row in range(1, 100): 
    cell = f"H{row}"
    data_2_sheet_two.add(sheet_two[cell])

for row in range(1, 100): 
    cell = f"N{row}"
    data_3_sheet_two.add(sheet_two[cell])

for row in range(1, 100): 
    cell = f"W{row}"
    data_2_sheet_two.add(sheet_two[cell])

sheet_two.add_data_validation(data_1_sheet_two)
sheet_two.add_data_validation(data_2_sheet_two)
sheet_two.add_data_validation(data_3_sheet_two)

for row in range(1, 100): 
    cell = f"F{row}"
    data_3_sheet_three.add(sheet_three[cell])

for row in range(1, 100): 
    cell = f"G{row}"
    data_3_sheet_three.add(sheet_three[cell])

for row in range(1, 100): 
    cell = f"H{row}"
    data_1_sheet_three.add(sheet_three[cell])

sheet_three.add_data_validation(data_1_sheet_three)
sheet_three.add_data_validation(data_2_sheet_three)
sheet_three.add_data_validation(data_3_sheet_three)` 
© www.soinside.com 2019 - 2024. All rights reserved.