My_Play_Lists ~BlackCatWhiteTail’s blog~

Talk About My Favorite Songs

applescript+numbers > 繰り返し処理

繰り返し処理の利用.

(1)準備
  • 検証のため,A1からA10に"cell name"を入れる.
  • "cell name"とは,例えば"A1"のようなアルファベット+数字 (AppleScriptの用語説明参照).
tell application "Numbers"
    activate
    make document
    tell document 1
        tell sheet 1
            tell table 1
                -- HERE INSERT CODE (see article below)
            end tell
        end tell
    end tell
end tell
(2)セル"A1"に"cell name"をいれる.(変数の利用)
  • セルを指定するときは cell "A1" のようにセルアドレスをダブルクオートで挟むので,下記コードのtheAddressの前後にも必要に感ずるが,cell theAddress の前後にダブルクオートは不要.
set theAddress to "A" & 1
set (value of cell theAddress) to (name of cell theAddress)
(1) repeat until 構文を利用
set i to 1
repeat while i ≤ 10
    set theAddress to "A" & i
    set (value of cell theAddress) to (name of cell theAddress)
    set i to i + 1
end repeat
(2) repeat while 構文を利用
set i to 1
repeat until i > 10
    set theAddress to "A" & i
    set (value of cell theAddress) to (name of cell theAddress)
    set i to i + 1
end repeat
(3) repeat with i from 1 to 10 構文を利用
repeat with i from 1 to 10
    set theAddress to "A" & i
    set (value of cell theAddress) to (name of cell theAddress)
end repeat
(4) ひとつ飛ばしでデ―タをいれる.
  • ExelVBAでは"For i = 1 To 10 Step 2 (~なんらかの処理~) Next i"で一つ飛ばしの繰り返しができる.
  • applescriptには一つ飛ばしで繰り返す構文はない.
  • 仕方ないのでので2で割った余りで場合分けする.
  • 2で割って余りが0のときはthen以下を実行,下記ではthen以降がないので何も実行されまい.2で割って余りが1のときelse以降が実行される.
repeat with i from 1 to 10
    if i mod 2 = 0 then
    else
        set theAddress to "A" & i
        set (value of cell theAddress) to (name of cell theAddress)
    end if
end repeat
(5) 繰り返し回数が不明の場合:try~on error, exit repeat を利用する.
  • 繰り返し回数が不明の場合でも使える.
  • try と end try で囲まれた箇所でエラーがでるとon error 以下が実行される.
  • 下のコードでは i を足して行って,最後の行をこえたらエラーが発生し,exit repeatが実行される.
set i to 1
repeat
    try
        set theAddress to "A" & i
            set (value of cell theAddress) to (name of cell theAddress)
            set i to i + 1
            on error
                exit repeat
    end try
end repeat

(6) repeat のネスト. - セル"A1"には 1,1 ,"B1" には 1,2 と入れていく. - cell index を使えば,列を表現するのにアルファベットではなく数字を用いることができて,繰り返し処理において便利.

repeat with i from 1 to (count of every cell of column 1)
    repeat with j from 1 to (count of every cell of row i)
        set (value of cell j of row i) to (i & "," & j as string)
    end repeat
end repeat
参考

(繰り返し処理(For Next)|ExcelマクロVBA入門:https://excel-ubara.com/excelvba1/EXCELVBA316.html)
(鳶嶋工房 / AppleScript / Tips / スクリプトの中断:http://tonbi.jp/AppleScript/Tips/MISC/Interrupt.html)

 このウェブサイトについて

 練習用のサイトです.

 自分の好きな曲について感想を述べます.

 また,世に役に立つかもと考えたことも書きます.

 

TOP 1 To 5

Title Artist Album
ロックバンド 中村中 私を抱いて下さい
犬の生活 SEX MACHINEGUNS SEX MACHINEGUNS
Megalovania Toby Fox UNDERTALE Soundtrack
Bokura No Fly Away Base Ball Bear Bokura No Fly Away - single
人魚の檻 陰陽座 迦陵頻伽(Ka Ryo Bin Ga