My_Play_Lists ~BlackCatWhiteTail’s blog~

Talk About My Favorite Songs

applescript + numbers > セルを結合する。

 セルの結合はあまり使用する機会がない。たまたま最近使用することがあったので書いておく。

 「用語説明」(shift + command + o もしくは メニューバー > ファイル > 用語説明を開く)で Numbers.appの用語説明を調べると以下のようにある。

merge v : Merge a specified range of cells.
 merge range

unmerge v : Unmerge all merged cells in a specified range.
 unmerge range

 上記にて、結合する範囲は "range" を用いる。たとえば、セルA1とセルB1を結合したいときは以下。

~~省略
    merge range "A1:B1"
~~省略

 繰り返し処理でやるときは以下。

~~
repeat with i from 1 to rowCount 
        merge range ("A" & i & ":" & "B" & i)
end repeat
~~

注意点

(その1)  ヘッダーのセル同士の結合はできるが、ヘッダーのセルとヘッダーでないセルの結合はできない。  なので、以下のコードでは、header column count =0header row count = 0でヘッダーを解除した。

(その2)  unmergeの挙動に注意が必要。  A1とB1を結合すると、A1とB1の値を連結した文字列がA1の値となる。  それをunmergeすると上記の連結した文字列かA1に収まり、B1は空となる。

tell application "Numbers"
    activate
    -- mak new document if there is no document
    if (count of document) as boolean then
    else
        set tgtDoc to make new document
    end if
    
    tell tgtDoc
        tell sheet 1
            tell table 1
                
                -- unset header column and header row
                set header column count to 0
                set header row count to 0
                
                -- get last row
                set rowCount to count of row of column "A"
                
                -- preparatian values
                repeat with i from 1 to rowCount
                    set (value of cell i in column "A") to (name of cell i in column "A")
                    set (value of cell i in column "B") to (name of cell i in column "B")
                end repeat
                
                
                -- *** merge cells in column A and column B ***
                repeat with i from 1 to rowCount
                    merge range ("A" & i & ":" & "B" & i)
                end repeat
                
                repeat with i from 1 to rowCount
                    unmerge range "A" & i
                end repeat
                
            end tell
        end tell
    end tell
end tell

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

 練習用のサイトです.

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

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

 

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