简体   繁体   English

Python XLwings 在单元格中编写 COUNTIF 公式

[英]Python XLwings writing COUNTIF formula in a cell

I am using XLwings to write in my excel file.我正在使用 XLwings 写入我的 excel 文件。 I need to write the formal =COUNTIF(warnings!$F:$F;1) in a cell in excel.我需要在 excel 的单元格中编写正式的=COUNTIF(warnings!$F:$F;1)

I am doingthe following, my code is working fine before adding overview_sheet.range('A9').formula = '=COUNTIF(warnings!$F:$F;1)'我正在执行以下操作,在添加overview_sheet.range('A9').formula = '=COUNTIF(warnings!$F:$F;1)'之前我的代码工作正常

import xlwings as xw
...
wb = xw.Book(template)
ws = wb.sheets['warnings']
overview_sheet = wb.sheets['Overview']
overview_sheet.range('A9').formula = '=COUNTIF(warnings!$F:$F;1)'
...

here is the error:这是错误:

Traceback (most recent call last):
  File "C:/Users/XX/XXX/XXX/Main.py", line 109, in <module>
    overview_sheet.range('A9').value = '=COUNTIF(warnings!$F:$F;1)'

if I write a simple formula like ' =SUM(1;3)' it works , but with my COUNTIF formula not如果我写一个像' =SUM(1;3)'这样的简单公式,它可以工作,但我的 COUNTIF 公式不行

How can I write it?我该怎么写?

I'm not an expert on Excel so your formula may be valid, however to me it does not match the COUNTIF function syntax of =COUNTIF(<range>, <criteria>) and if I just paste your formula into a cell it is rejected as being an incorrect formula.我不是 Excel 专家,所以你的公式可能是有效的,但对我来说,它与 =COUNTIF(<range>, <criteria>) 的 COUNTIF 函数语法不匹配,如果我只是将你的公式粘贴到单元格中,那就是被拒绝为不正确的公式。 Any 'correct' =COUNTIF formula I enter into a cell using your code works OK.我使用您的代码输入单元格的任何“正确”=COUNTIF 公式都可以正常工作。

However assuming the formula is how you want it;但是,假设公式是您想要的; from a few tests it seems xlwings rejects the line when it is determined to be a formula ie if the preceding '=' exists, so removing the = allows the formula to be written without error to cell, but of course won't do anything.从一些测试来看,当它被确定为公式时,xlwings 似乎会拒绝该行,即如果前面的“=”存在,因此删除 = 允许将公式写入单元格而不会出错,但当然不会做任何事情.
Since the formula in any cell is just text an alternative to writing to the cell would be to use '.value'由于任何单元格中的公式只是文本,因此写入单元格的替代方法是使用“.value”

overview_sheet.range('A9').value = '=COUNTIF(...)'

Using '.value' instead of '.formula' allows the formula to be written without error however Excel converts it to text by placing a single quote in front so this may be why you are using .formula.使用 '.value' 而不是 '.formula' 可以正确编写公式,但是 Excel 通过在前面放置一个单引号将其转换为文本,因此这可能就是您使用 .formula 的原因。

The following might be a work around.以下可能是一种解决方法。
I can't confirm it will absolutely work since as stated Excel rejects your formula as having incorrect syntax.我无法确认它绝对有效,因为如前所述,Excel 拒绝您的公式,因为它的语法不正确。 However if it is valid for your sheet you can try making use of xlwings cell edit feature and write to the cell this way;但是,如果它对您的工作表有效,您可以尝试使用 xlwings 单元格编辑功能并以这种方式写入单元格;
Put a character in front of the formula, it should be able to be any character that Excel would not otherwise recognise as another cell function, I have used 'X' in the example.在公式前面放一个字符,它应该可以是 Excel 不会识别为另一个单元格函数的任何字符,我在示例中使用了“X”。 This allow the formula to be written without error.这允许编写公式而不会出错。 Then on the next line of code, remove the first character 'X' from the cell leaving the formula as you want it.然后在下一行代码中,从单元格中删除第一个字符“X”,留下您想要的公式。 This again should complete without error and the sheet can then be saved.这应该再次完成而没有错误,然后可以保存工作表。

overview_sheet.range('A9').value = "X=COUNTIF(warnings!$F:$F;1)"
overview_sheet.range('A9').characters[0].api.Delete()

In my sheet the formula is changed by Excel to text again在我的工作表中,Excel 再次将公式更改为文本

'=COUNTIF(warnings!$F:$F;1)

but if its a valid formula on your sheet it may not be changed.但如果它在您的工作表上是有效的公式,则可能不会更改。


Additional details额外细节
Refresh the worksheet (F9)刷新工作表 (F9)

overview_sheet.api.Calculate()

Change the formula calculation option to manual and back to auto将公式计算选项更改为手动并返回自动

wb.api.Calculation = -4135  # -4135 is xlCalculationManual 
wb.api.Calculation = -4105  # -4105 is xlCalculationAutomatic

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

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