-1

In numbers I have columns with data like this:

enter image description here

I want to iterate through cells and for ex. if value of cell in row 6 is not equal to value of. cell in row 5 then add an empty row above the row 6 (below 5)

this is my code but doesn't work:

    tell column "A"
        set nbr to count rows
        
        set x to 2
        repeat with r from x to nbr
            set aVal to value of cell r
            
            repeat with s from x + 1 to nbr
                set aNVal to value of cell s
                
                if aNVal is not equal to aVal then
                    add row above s
                    exit repeat
                end if
                
            end repeat
            
            set x to x + 1
        end repeat
    end tell
4
  • How does it not work? No empty rows or too many empty rows? Do you want to add an extra empty row if row 6 and 12 are not the same or only if the row above is not the same? Tip: start at the bottom, adding rows affects the row indexes. Commented Feb 11 at 4:39
  • I have an error: Numbers - error: Cannot convert 6 to type range. I want empty row between rows with different values 5 ad 6, 11 and 12 Commented Feb 11 at 6:31
  • Do not modify your question to include your solution. If you have figured out the answer, put it in the Answer field. Or just delete the question. Commented Feb 11 at 8:25
  • Your command is to add a row above s but what is that? It is an integer. You need to flesh it out, eg row s. The reason for the error is that '6' is not a range. Commented Feb 12 at 2:14

1 Answer 1

1

You have to flesh things out when you specify stuff, for example: value of cell 1 of row r.

As suggested by @Willeke, this begins looping at the bottom — otherwise the added row will mess up your repeat loop. It ends at row 3 to keep it from inserting a row above row 2, which otherwise would likely always occur because your header will be different than your data.

You don't actually need the second loop because you are only comparing one row to its predecessor.

tell application "Numbers"
    tell column "A" of table 1 of sheet 1 of document 1
        
        set nbr to count rows
        set x to 3
        
        repeat with r from nbr to x by -1
            if value of cell 1 of row r is not equal to value of cell 1 of row (r - 1) then
                add row above row r
            end if
        end repeat
    end tell
end tell
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.