如何使用DataNitro在Excel中为多个单元格写入值

时间:2014-02-10 17:00:06

标签: excel datanitro

我正在尝试使用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

如果问题太愚蠢,谢谢,对不起!

1 个答案:

答案 0 :(得分: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"