オープン中のNumbers書類のうち最前面のものを対象に、すべてのシート上のテーブル1のデータを連結して新規書類に入れるAppleScriptです。
(↑)Numbers書類中のテーブルのカラム数がすべて同じことが本Scriptの前提条件。
データ結合まではすぐに処理が終わるものの、結合したデータを新規Numbers書類にAppleScriptから順次突っ込むと、データ数の増加にともなって処理時間が大幅に増えるので、そのやり方はやるべきではありません(経験のない素人がやりそうな処理)。
なので、本ScriptではCSVファイルを組み立てて、デスクトップに書き出し、それをNumbersにオープンさせます。
これだと処理は一瞬です。開発環境では、シート9枚&総合計263行のデータを連結してCSVに書き出してNumbers上でオープンするのに1.6秒しかかかりません。
AppleScript名:オープン中のNumbers書類のすべてのシート上のテーブルのデータを連結して新規書類に入れる v2 |
— Created 2018-07-09 by Takaaki Naganoya — 2018 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use bPlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html script spd property allData : {} –Numbersから取得したデータをストアしておく property all2DData : {} –取得したデータを2D Arrayに変換した際にデータをストアしておく end script set (allData of spd) to {} set (all2DData of spd) to {} set dataWidth to 2 tell application "Numbers" tell front document set aList to every sheet repeat with i in aList tell i tell table 1 –テーブル中のセルの値をすべて取得(1D Listで帰ってくる) set aList to value of every cell –1D ListをSweep(missing valueの入っているアイテムを削除) set bList to sweep1DList(aList) of me –取得してSweepしたデータを連結 set (allData of spd) to (allData of spd) & (bList as list) end tell end tell end repeat end tell end tell –1D Listを2D Listに変換 set (all2DData of spd) to conv1DListTo2DList((allData of spd), dataWidth) of me –まとめたデータをCSVファイルとして書き出し set aPath to ((path to desktop) as string) & (do shell script "uuidgen") & ".csv" saveAsCSV((all2DData of spd), aPath) of me do shell script "sync" –SSDで処理している場合には必要ないが、、、 set anAlias to aPath as alias –書き出したCSVファイルをNumbersでオープン tell application "Numbers" open anAlias activate end tell on sweep1DList(aList) load framework return (current application’s SMSForder’s arrayByDeletingBlanksIn:aList) as list end sweep1DList on conv1DListTo2DList(aList, groupingNum) load framework return (current application’s SMSForder’s subarraysFrom:aList groupedBy:groupingNum |error|:(missing value)) as list end conv1DListTo2DList –2D List to CSV file on saveAsCSV(aList, aPath) set crlfChar to (string id 13) & (string id 10) set LF to (string id 10) set wholeText to "" repeat with i in aList set newLine to {} –Sanitize (Double Quote) repeat with ii in i set jj to ii as text set kk to repChar(jj, string id 34, (string id 34) & (string id 34)) of me –Escape Double Quote set the end of newLine to kk end repeat –Change Delimiter set aLineText to "" set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to "\",\"" set aLineList to newLine as text set AppleScript’s text item delimiters to curDelim set aLineText to repChar(aLineList, return, "") of me –delete return set aLineText to repChar(aLineText, LF, "") of me –delete lf set wholeText to wholeText & "\"" & aLineText & "\"" & crlfChar –line terminator: CR+LF end repeat if (aPath as string) does not end with ".csv" then set bPath to aPath & ".csv" as Unicode text else set bPath to aPath as Unicode text end if writeToFile(wholeText, bPath, false) of me end saveAsCSV on writeToFile(this_data, target_file, append_data) tell current application try set the target_file to the target_file as text set the open_target_file to open for access file target_file with write permission if append_data is false then set eof of the open_target_file to 0 write this_data to the open_target_file starting at eof close access the open_target_file return true on error error_message try close access file target_file end try return error_message end try end tell end writeToFile on repChar(origText as text, targChar as text, repChar as text) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to targChar set tmpList to text items of origText set AppleScript’s text item delimiters to repChar set retText to tmpList as string set AppleScript’s text item delimiters to curDelim return retText end repChar |
More from my site
(Visited 64 times, 1 visits today)