最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

AppleScript and Numbers - adding rows - Stack Overflow

programmeradmin1浏览0评论

In numbers I have columns with data like this:

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

In numbers I have columns with data like this:

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
Share Improve this question edited Feb 11 at 8:23 matt 536k93 gold badges933 silver badges1.2k bronze badges asked Feb 10 at 17:03 emerogemerog 793 silver badges10 bronze badges 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. – Willeke 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 – emerog 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. – matt 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. – Mockman Commented Feb 12 at 2:14
Add a comment  | 

1 Answer 1

Reset to default 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
发布评论

评论列表(0)

  1. 暂无评论