fix: 修复多占位符识别,补充测试
This commit is contained in:
parent
1f51665e42
commit
504bd2f65f
@ -13,10 +13,10 @@ def parse_template(file_path: str) -> dict:
|
|||||||
for row in ws.iter_rows():
|
for row in ws.iter_rows():
|
||||||
for cell in row:
|
for cell in row:
|
||||||
if cell.value and isinstance(cell.value, str):
|
if cell.value and isinstance(cell.value, str):
|
||||||
m = PLACEHOLDER_RE.search(cell.value)
|
matches = PLACEHOLDER_RE.findall(cell.value)
|
||||||
if m:
|
for name in matches:
|
||||||
placeholders.append({
|
placeholders.append({
|
||||||
"name": m.group(1),
|
"name": name,
|
||||||
"sheet": sheet_name,
|
"sheet": sheet_name,
|
||||||
"cell": cell.coordinate,
|
"cell": cell.coordinate,
|
||||||
})
|
})
|
||||||
|
|||||||
@ -5,6 +5,7 @@ ws = wb.active
|
|||||||
ws.title = "Sheet1"
|
ws.title = "Sheet1"
|
||||||
ws["B3"] = "{{编号}}"
|
ws["B3"] = "{{编号}}"
|
||||||
ws["C5"] = "{{姓名}}"
|
ws["C5"] = "{{姓名}}"
|
||||||
ws["D7"] = "normal_value" # not a placeholder, should NOT be detected
|
ws["D7"] = "normal_value" # 非占位符,不应被识别
|
||||||
|
ws["E9"] = "{{客户名}}和{{编号}}" # 同一单元格内有两个占位符
|
||||||
wb.save("tests/fixtures/sample_template.xlsx")
|
wb.save("tests/fixtures/sample_template.xlsx")
|
||||||
print("fixture created")
|
print("fixture created")
|
||||||
|
|||||||
BIN
tests/fixtures/sample_template.xlsx
vendored
BIN
tests/fixtures/sample_template.xlsx
vendored
Binary file not shown.
@ -26,3 +26,12 @@ def test_placeholder_has_cell_info():
|
|||||||
biaohao = next(p for p in result["placeholders"] if p["name"] == "编号")
|
biaohao = next(p for p in result["placeholders"] if p["name"] == "编号")
|
||||||
assert biaohao["sheet"] == "Sheet1"
|
assert biaohao["sheet"] == "Sheet1"
|
||||||
assert biaohao["cell"] == "B3"
|
assert biaohao["cell"] == "B3"
|
||||||
|
|
||||||
|
def test_multiple_placeholders_in_one_cell():
|
||||||
|
result = parse_template(FIXTURE)
|
||||||
|
names = [p["name"] for p in result["placeholders"]]
|
||||||
|
assert "客户名" in names
|
||||||
|
assert "编号" in names # 已在其他地方存在,但也在 E9 中
|
||||||
|
# 验证 E9 中的两个占位符都被检测到
|
||||||
|
e9_placeholders = [p for p in result["placeholders"] if p["cell"] == "E9"]
|
||||||
|
assert len(e9_placeholders) == 2
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user