Numbersの表を、書籍掲載用に体裁をととのえるAppleScriptのアップデート版です。書籍掲載時には、同じ値が横方向に連続している箇所は1つにまとめたいところなので、その作業を自動で行います。
新型のNumbers/Excelセルアドレス変換ルーチンを実際に使用してみました。モジュールを入れ替えただけです。
▲対象の表のいずれかのセルを選択してAppleScriptを実行
AppleScript名:Numbersで選択中の表を書籍掲載用にセル内容の整理(重複セルをまとめる)v2.scpt |
— – Created by: Takaaki Naganoya – Created on: 2024/12/03 — – Copyright © 2024 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" 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 headR to header row count set headC to header column count set rowC to count every row set colC to count every column repeat with y from (headR + 1) to (rowC) –1行分(ヘッダーカラムをのぞく)のデータを1D Listで取得 set tmpRangeStr to numberToCell(headC + 1) of AddressEncoder & (y as string) & ":" & numberToCell(colC) of AddressEncoder & (y as string) set tValList to value of cells of range tmpRangeStr –1D Listから値が連続している区間を求める set sameBlock to detectSameValueRepeatationBlockIn1DArray(tValList) of detectRepeatation –連続区間でループ repeat with i in sameBlock copy i to {startX, endX} –データ内の項目Indexであることに留意 –重複値のセルをクリアする(初出セルの値は残す) repeat with ii from (startX + 1) to endX set value of cell (ii + headC) of row y to "" end repeat –重複していたセルをマージする set mRangeStr to numberToCell(headC + startX) of AddressEncoder & (y as string) & ":" & numberToCell(headC + endX) of AddressEncoder & (y as string) set mRange to range mRangeStr merge mRange end repeat end repeat end tell end tell end tell end tell script detectRepeatation property parent : AppleScript use scripting additions use framework "Foundation" –巨大なデータの処理に向いていないかも? on detectSameValueRepeatationBlockIn1DArray(aList) set dList to returnDuplicatesOnly(aList) of me –> {"年間仕様", "YP仕様", "月間仕様"} set anArray to current application’s NSMutableArray’s arrayWithArray:aList anArray’s addObject:"" set resList to {} repeat with i in dList set j to contents of i set anIndex to (anArray’s indexOfObject:j) repeat with ii from (anIndex + 1) to ((length of aList)) set jj to (anArray’s objectAtIndex:ii) as string if jj is not equal to j then set the end of resList to {anIndex + 1, ii} exit repeat end if end repeat end repeat resList –> {{4, 6}, {7, 9}, {10, 17}} end detectSameValueRepeatationBlockIn1DArray on returnDuplicatesOnly(aList) set countedSet to current application’s NSCountedSet’s alloc()’s initWithArray:aList set simpleSet to current application’s NSSet’s setWithArray:aList countedSet’s minusSet:simpleSet return countedSet’s allObjects() as list end returnDuplicatesOnly end script script AddressEncoder property parent : AppleScript use scripting additions — 数値からセルアドレス(A1形式)への変換 on numberToCell(columnNumber) set columnAddress to "" set tempNumber to columnNumber — 列番号をA-Z形式に変換 repeat while tempNumber > 0 set remainder to (tempNumber – 1) mod 26 set columnAddress to (character (remainder + 1) of "ABCDEFGHIJKLMNOPQRSTUVWXYZ") & columnAddress set tempNumber to (tempNumber – 1) div 26 end repeat — A1形式のアドレスを返す return columnAddress end numberToCell — セルアドレス(A1形式)から数値への変換 on cellToNumber(cellAddress) set columnPart to "" set rowPart to "" — 列部分と行部分を分離 repeat with char in cellAddress if char is in "0123456789" then set rowPart to rowPart & char else set columnPart to columnPart & char end if end repeat — 列部分を数値に変換 set columnNumber to 0 repeat with i from 1 to length of columnPart set char to character i of columnPart using terms from scripting additions set columnNumber to columnNumber * 26 + (offset of char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ") end using terms from end repeat — 数値を返す return {columnNumber, (rowPart as integer)} end cellToNumber end script |
More from my site
(Visited 4 times, 4 visits today)