XlsxWriter在编写公式时添加随机字符(@)
我正在尝试使用 Python 中的 XlsxWriter 将公式写入 xlsx 文档。
我试图写入单元格的是:
=SUM(IF($F$5:$F$130=$F$4,I$5:I$130))
但是当我尝试使用 XlsxWriter 插入公式时,它会这样写:
=SUM(IF(@$F$5:$F$130=$F$4,I$5:I$130))
如您所见,XlsxWriter 中有一个随机 @ ,这会导致 #VALUE错误。一旦我手动删除@,公式就会按预期工作。
我将公式字符串打印到终端,其打印如下:
=SUM(IF($F$5:$F$130=$F$3,I$5:I$130))
如何解决此问题?
相关代码片段包含在下面...变量被审查为通用
--- 片段 1 ---
for j in range(0,len(LIST_A)):
if VAR_A == LIST_VAR_A[1]:
formula = formula_function(row, col, row_start, LIST_B, LIST_C, j)
print(formula) # this is where I printed the formula to check for an @
worksheet.write_formula(row, col, formula, LIST_FORMAT[0])
else: worksheet.write(row, col, CLASS.LIST_MEMBER[j], LIST_FORMAT[0])
col += 1
--- 片段 2 ---
def formula_function(row, col, row_start, LIST_B, LIST_C, j):
# rctc ... from xlsxwriter.utility import xl_rowcol_to_cell as rctc
CELL_A = rctc(row, col-3-spread_depth, row_abs=True, col_abs=True)
CELL_B1 = rctc(row_start+1+len(LIST_B), col-3-j, row_abs=True, col_abs=True)
CELL_B2 = rctc(row_start-1+len(LIST_C), col-3-j, row_abs=True, col_abs=True)
CELL_C1 = rctc(row_start+1+len(LIST_B), col, row_abs=True)
CELL_C2 = rctc(row_start-1+len(LIST_C), col, row_abs=True)
formula = '=SUM(IF(' + CELL_B1 + ':' + CELL_B2 + '=' + CELL_A + ',' + \
CELL_C1 + ':' + CELL_C2 + '))'
return formula
I am trying to write formulas to an xlsx document using XlsxWriter in Python.
What I am trying to write to a cell is:
=SUM(IF($F$5:$F$130=$F$4,I$5:I$130))
But when I try to use XlsxWriter to insert the formula it writes this:
=SUM(IF(@$F$5:$F$130=$F$4,I$5:I$130))
As you can see, there is a random @ from XlsxWriter which causes the #VALUE error. Once I manually remove the @ the formula works as intended.
I printed the formula string to the terminal and it printed as the following:
=SUM(IF($F$5:$F$130=$F$3,I$5:I$130))
How can I fix this?
Relevant snippets of the code are included below...variables are censored to generic
--- Snippet 1 ---
for j in range(0,len(LIST_A)):
if VAR_A == LIST_VAR_A[1]:
formula = formula_function(row, col, row_start, LIST_B, LIST_C, j)
print(formula) # this is where I printed the formula to check for an @
worksheet.write_formula(row, col, formula, LIST_FORMAT[0])
else: worksheet.write(row, col, CLASS.LIST_MEMBER[j], LIST_FORMAT[0])
col += 1
--- Snippet 2 ---
def formula_function(row, col, row_start, LIST_B, LIST_C, j):
# rctc ... from xlsxwriter.utility import xl_rowcol_to_cell as rctc
CELL_A = rctc(row, col-3-spread_depth, row_abs=True, col_abs=True)
CELL_B1 = rctc(row_start+1+len(LIST_B), col-3-j, row_abs=True, col_abs=True)
CELL_B2 = rctc(row_start-1+len(LIST_C), col-3-j, row_abs=True, col_abs=True)
CELL_C1 = rctc(row_start+1+len(LIST_B), col, row_abs=True)
CELL_C2 = rctc(row_start-1+len(LIST_C), col, row_abs=True)
formula = '=SUM(IF(' + CELL_B1 + ':' + CELL_B2 + '=' + CELL_A + ',' + \
CELL_C1 + ':' + CELL_C2 + '))'
return formula
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XlsxWriter 不会将
@
插入到公式中,Excel 365 会像这样显示它,以指示公式中在可以返回范围或数组时隐式返回单个值的位置。来自 XlsxWriter 文档 动态数组 - 隐式交集运算符“@”:
如果您的情况应该使用动态数组公式:
输出:
XlsxWriter isn't inserting
@
into the formulas, Excel 365 is displaying it like that to indicate a position in a formula that is implicitly returning a single value when a range or an array could be returned.From the XlsxWriter docs on Dynamic Arrays - The Implicit Intersection Operator "@":
If your case you should use a dynamic array formula:
Output: