2014-02-10 42 views
1

我尝试使用DataNitro自动执行一些Excel任务,但我无法掌握如何为与其他人相关的多行编写新值,如何在Excel中使用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 

感谢和抱歉,如果这个问题太笨!

回答

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" 
+0

这是伟大的本,谢谢! 有没有什么办法可以从第一个单元格到最后一个填充单元格设置CellRange? 在:difference = CellRange((2,2),(#last,2))。值 – user3293796

+0

有:您可以使用Cell.vertical获取一列(和Cell.horizo​​ntal获取一行)。例如。差=单元(2,2)。垂直 –

相关问题