1

我正在尝试使用 DataNitro 来自动化一些 excel 任务,但我无法掌握如何将新值写入与其他行相关的多行,

例如:我希望它从第 1 列中读取值,并根据条件在第 2 列中写入响应。

简化示例:

difference = CellRange((2,2),(120,2)).value
Status = CellRange((2,3),(120,3)).value

for x in difference:
   if x < -10:
      Status = "Paused"
      ###here i don't know what to put
   else:
      Status = "Active"
      ###here i don't know what to put

如果问题太愚蠢,谢谢和抱歉!

4

1 回答 1

0

最好的方法是在循环中保留一个计数器:

difference = CellRange((2,2),(120,2)).value
# there's no need to read in the current Status value

i = 0
for x in difference:
   if x < -10:
      Status = "Paused"
      Cell((2 + i), 3).value = "Paused"
   else:
      Status = "Active"
      Cell((2 + i), 3).value = "Active"
   i += 1

在 Python 中执行此操作的更好方法是使用enumerate关键字,它会自动跟踪计数器:

difference = CellRange((2,2),(120,2)).value

i = 0
for i, x in enumerate(difference):
   if x < -10:
      Status = "Paused"
      Cell((2 + i), 3).value = "Paused"
   else:
      Status = "Active"
      Cell((2 + i), 3).value = "Active"
于 2014-02-10T19:12:36.803 回答