XlsxWriter在编写公式时添加随机字符(@)

发布于 2025-01-13 09:14:04 字数 1603 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坠似风落 2025-01-20 09:14:04

XlsxWriter 不会将 @ 插入到公式中,Excel 365 会像这样显示它,以指示公式中在可以返回范围或数组时隐式返回单个值的位置。

来自 XlsxWriter 文档 动态数组 - 隐式交集运算符“@”

...

如果您是第一次遇到隐式交集运算符“@”,那么可能是从“为什么 Excel/XlsxWriter 在我的公式中添加 @s”的角度出发。实际上,如果您遇到此运算符,并且您不希望它出现在那里,那么您可能应该使用 write_array_formula()write_dynamic_array_formula()

如果您的情况应该使用动态数组公式:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write_dynamic_array_formula(0, 0, 0, 0, '=SUM(IF($F$5:$F$130=$F$4,I$5:I$130))')

workbook.close()

输出

在此处输入图像描述

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 you are encountering the Implicit Intersection Operator “@” for the first time then it is probably from a point of view of “why is Excel/XlsxWriter putting @s in my formulas”. In practical terms if you encounter this operator, and you don’t intend it to be there, then you should probably write the formula as a CSE (Ctrl+Shift+Enter) or dynamic array function using write_array_formula() or write_dynamic_array_formula().

If your case you should use a dynamic array formula:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

worksheet.write_dynamic_array_formula(0, 0, 0, 0, '=SUM(IF($F$5:$F$130=$F$4,I$5:I$130))')

workbook.close()

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文