문제

I'm trying out DataNitro to automate some excel tasks, but I'm failing to grasp how to write a new value to multiple lines that relate to others,

For instance: I want it to read value from column 1, and based on a condition, to write a response in column 2.

Simplified example:

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

Thanks and sorry if the question is too stupid!

도움이 되었습니까?

해결책

The best way to do this is to keep a counter in your loop:

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

A better way to do this in Python is to use the enumerate keyword, which tracks the counter automatically:

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"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top