fix: Excel export prohibits inputting external links or formulas#3106
fix: Excel export prohibits inputting external links or formulas#3106shaohuzhang1 merged 1 commit intomainfrom
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| cell.value = col | ||
| # 创建HttpResponse对象返回Excel文件 | ||
| return workbook | ||
|
|
There was a problem hiding this comment.
Your code looks generally correct, but there is one small issue that could cause confusion:
- The
isinstance(col, str)check seems unnecessary since you're immediately assigning it to a new variable (col). You can simplify this by directly using the input parameter.
Here's an optimized version of your function:
import re
from openpyxl import Workbook
ILLEGAL_CHARACTERS_RE = re.compile(r'[^\w.@]')
def get_workbook(data_dict, document_dict):
worksheet_name = ''.join(x.capitalize() for x in document_dict['sheetName'])
with Workbook() as workbook:
worksheet = workbook.create_sheet(title=worksheet_name)
max_col = len(list(data_dict[document_dict['sheetName']][0].keys())) - 1
row_idx = 0
# 获取数据并填充工作表单元格
for data_row in data_dict[document_dict['sheetName']:
col_idx = 1
max_col = 0
for col, value in zip(document_dict['headers'], map(str, data_row)):
if not ILLEGAL_CHARACTERS_RE.match(col):
if col.startswith(('=', '+', '-@')):
col = '\ufeff' + col
cell = worksheet.cell(row=row_idx + 1, column=col_idx)
cell.value = col
col_idx += 1
row_idx += 1
# 创建HttpResponse对象返回Excel文件
return workbookKey Changes:
- Simplified Instance Check: Removed the
isinstancecheck since we already knowcolis used within its scope. - Efficient Column Indexing: Moved the initial loop through headers and calculate
max_colonly once at the start. - Improved Efficiency with Mapping: Used
map(str, data_row)to convert all values to strings efficiently inside the inner loop.
These changes should make the function more concise and potentially faster while maintaining functionality.
fix: Excel export prohibits inputting external links or formulas