简体   繁体   English

xlwt无法解析公式中的IF语句

[英]xlwt can't parse IF statement in formula

i'm trying to write a formula to a series of cells by building the string. 我正在尝试通过构建字符串为一系列单元格编写公式。 for instance i want to display the word "true" in a cell if the corresponding cell in column G contains the word "monkey" in it. 例如,如果列G中的相应单元格中包含单词“ monkey”,则我想在单元格中显示单词“ true”。

for rowi in range(totalRows):
    thisRow = str(rowi)
    functionString = r'IF(ISNUMBER(SEARCH("monkey",G'+thisRow+')),("true")

this will produce the string "IF(ISNUMBER(SEARCH("monkey",G1)),("true")" -- if i then pass that to xlwt.Formula... 这将产生字符串“ IF(ISNUMBER(SEARCH(” monkey“,G1)),(” true“)”-如果我将其传递给xlwt.Formula ...

    ws.write(rowi+1, 0, xlwt.Formula(functionString))

i get this error... 我得到这个错误...

File "C:\Python27\Lib\site-packages\xlwt\ExcelFormula.py", line 22, in __init__
    raise ExcelFormulaParser.FormulaParseException, "can't parse formula " + s
xlwt.ExcelFormulaParser.FormulaParseException: can't parse formula IF(ISNUMBER(SEARCH("bitmap",G2)),("true")

is it possible to use this type of function with xlwt? 是否可以在xlwt中使用这种类型的功能?

Ignoring the impossibility that you typed in monkey and G1 whereas the error message mentions bitmap and G2 ... 忽略在monkeyG1键入的可能性,而错误消息中提到了bitmapG2 ...

I suggest that you fire up Excel, select a cell, type = and paste your formula ... what happens? 我建议您启动Excel,选择一个单元格,键入=并粘贴您的公式...会发生什么? This check is always advisable before going public, and is available even if your internet connection is broken. 此检查始终建议在公开之前进行,即使您的互联网连接断开,该检查也仍然可用。

Notes: 笔记:

(1) the parentheses around "true" are redundant (1)围绕"true"的括号是多余的

(2) simpler: ISNUMBER(SEARCH("bitmap",G2)) (2)更简单: ISNUMBER(SEARCH("bitmap",G2))

(3) Are you really expecting that a user will open the resulting XLS file and type text into column G? (3)您是否真的希望用户打开生成的XLS文件并在G列中键入文本?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM