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