Numbersで縦方向に行単位で任意の並べ替え(Ascending、Descending)を行うのは問題ないのですが、横方向に並べ替えをする機能がなかったので、作ってみました。
▲初期状態。左側のデータが新しく、右端が一番古いデータ。並べ替えを行う範囲を選択
▲実行後。横方向に逆順に並べ替えを行った。左端が一番古く、右端が一番新しいデータ
標準機能で欲しいところです。
データ取得は速いものの、スプレッドシートへのデータの書き込みに時間のかかるNumbers。本ScriptもApple Silicon Macで実行すればそこそこの速度で実行してくれますが、おそらくExcelに対して同様の処理を実行したら1,000倍ぐらい高速です。
それほど大量のデータを処理すると、非同期処理が途中でおかしくなるので、数千セルぐらいを上限としておいたほうがよいでしょう。
AppleScript名:Numbersで選択中の範囲を横方向に逆順に並べて、選択範囲に再設定.scptd |
— – Created by: Takaaki Naganoya – Created on: 2025/01/14 — – Copyright © 2025 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" script spd property aList : {} property bList : {} property cList : {} end script set (aList of spd) to get2DListFromNumbersSelection() of me if (aList of spd) = "" then return "No Selection" set (bList of spd) to {} repeat with i in (aList of spd) set tmpList to contents of i set tmpList to reverse of tmpList set the end of (bList of spd) to tmpList end repeat set (bList of spd) to FlattenList((bList of spd)) of me tell application "Numbers" tell front document tell active sheet tell table 1 set (cList of spd) to cell of selection range set iCount to 1 repeat with i in (cList of spd) set newVal to contents of item iCount of (bList of spd) if (newVal as string) is equal to "missing value" then set newVal to "" ignoring application responses set value of i to newVal end ignoring set iCount to iCount + 1 end repeat end tell end tell end tell end tell on get2DListFromNumbersSelection() –Numbersで選択範囲を縦に区切ったリストを返す tell application "Numbers" tell front document tell active sheet try set theTable to first table whose class of selection range is range on error return "" –何も選択されてなかった場合 end try tell theTable set selList to value of every cell of selection range –選択範囲のデータを取得 set selName to name of selection range –選択範囲のrange情報を取得 set {s1, s2} to parseByDelim(selName, ":") of me –始点の情報を取得する set s1Row to (address of row of range s1) as integer set s1Column to (address of column of range s1) as integer –終点の情報を取得する set s2Row to (address of row of range s2) as integer set s2Column to (address of column of range s2) as integer –選択範囲の情報を取得する set selHeight to s2Row – s1Row + 1 –高さ(Height of selection range) set selWidth to s2Column – s1Column + 1 –幅(Width of selection range) end tell end tell end tell end tell set aLen to length of selList set aaLen to selHeight set bList to {} repeat with i from 1 to aaLen set aHoriList to {} repeat with ii from 1 to selWidth set j1 to ii + (i – 1) * selWidth set tmpCon to contents of item j1 of selList set aClass to class of tmpCon if aClass = number or aClass = integer or aClass = real then set tmpCon to tmpCon as integer end if set the end of aHoriList to tmpCon end repeat set the end of bList to aHoriList end repeat return bList end get2DListFromNumbersSelection –テキストを指定デリミタでリスト化 on parseByDelim(aData, aDelim) set aText to current application’s NSString’s stringWithString:aData set aList to aText’s componentsSeparatedByString:aDelim return aList as list end parseByDelim –By Paul Berkowitz –2009年1月27日 2:24:08:JST –Re: Flattening Nested Lists on FlattenList(aList) set oldDelims to AppleScript’s text item delimiters set AppleScript’s text item delimiters to {"????"} set aString to aList as text set aList to text items of aString set AppleScript’s text item delimiters to oldDelims return aList end FlattenList |