if self.data_sheet.cell(row=1, column=1).value is None: self.data_sheet.cell(row=1, column=1, value="PEGA-料号") if self.data_sheet.cell(row=1, column=2).value is None: self.data_sheet.cell(row=1, column=2, value="數量") if self.data_sheet.cell(row=1, column=3).value is None: self.data_sheet.cell(row=1, column=3, value="日期") if self.data_sheet.cell(row=1, column=4).value is None: self.data_sheet.cell(row=1, column=4, value="時間") if self.data_sheet.cell(row=1, column=5).value is None: self.data_sheet.cell(row=1, column=5, value="班別") if self.data_sheet.cell(row=1, column=6).value is None: self.data_sheet.cell(row=1, column=6, value="登記人") if self.data_sheet.cell(row=1, column=7).value is None: self.data_sheet.cell(row=1, column=7, value="單價") if self.data_sheet.cell(row=1, column=8).value is None: self.data_sheet.cell(row=1, column=8, value="品名") if self.data_sheet.cell(row=1, column=9).value is None: self.data_sheet.cell(row=1, column=9, value="儲位") if self.data_sheet.cell(row=1, column=10).value is None: self.data_sheet.cell(row=1, column=10, value="入庫單號")這怎麽優化
时间: 2024-03-27 08:41:00 浏览: 72
datasheet
可以将这段代码改写为一个循环,避免重复的 if 语句。例如:
```
headers = ["PEGA-料号", "數量", "日期", "時間", "班別", "登記人", "單價", "品名", "儲位", "入庫單號"]
for col in range(1, 11):
if self.data_sheet.cell(row=1, column=col).value is None:
self.data_sheet.cell(row=1, column=col, value=headers[col-1])
```
这样代码更简洁,也更易于维护。
阅读全文