【Excel2008】スクリプト2件ご紹介
今回はExcel用のAppleScript2件です.
お客様からいただいたCSVやTSVを加工して色々する作業が多いのですが,もとの値がどうなっていたかを確認するために,それらをExcelで開いて確認することが多いです.
そのとき,それぞれの列に適した列幅を設定したり,オートフィルタで整理した状態にしたりと色々するのですが,ファイルが多いとそれも大変です.
1 コラム幅を設定し,適用する
setWidthPrefList内に入力した設定の通りに,列幅を変更します.
setWidthPrefList内のリストは,列のインデックスも合わせて入力する仕様なので,順番通りでなくても大丈夫です.
[applescript]
tell application "Microsoft Excel"
tell active workbook
set widthPrefList to setWidthPrefList() of me –コラム幅設定用のリストを作成
setColumnWidth(widthPrefList) of me –設定リストに従いコラム幅を設定
end tell
end tell
–設定リストに従いコラム幅を設定
on setColumnWidth(widthPrefList)
set aConstant to 2.5142281 –変換用定数
tell application "Microsoft Excel"
tell active workbook
tell active sheet
repeat with aData in widthPrefList
set {aNum, aWidth} to aData
tell column aNum
if aWidth is "auto" then
autofit
else
set aWidth to aWidth / aConstant
set column width to aWidth
end if
end tell
end repeat
end tell
end tell
end tell
end setColumnWidth
–コラム幅設定用のリストを作成
on setWidthPrefList()
set widthPrefList to {} –定義
(*{インデックス, 幅}で設定,"auto"でautofit.数字はダイアログに入力する数字*)
set the end of widthPrefList to {1, "auto"}
set the end of widthPrefList to {2, "auto"}
set the end of widthPrefList to {3, "auto"}
set the end of widthPrefList to {4, "auto"}
set the end of widthPrefList to {5, 100}
return widthPrefList
end setWidthPrefList
[/applescript]
この状態が
こうなります.
シートにオートフィルタを設定し,指定の列の指定の値でフィルタをかけます.
サンプルでは,2列目を「野菜」でフィルタしています.
[applescript]
tell application "Microsoft Excel"
tell active workbook
tell active sheet
setAutofilter() of me
setCriteria(2, "野菜") of me –オートフィルタのフィルタ内容を変更
end tell
end tell
end tell
–オートフィルタのフィルタ内容を変更
on setCriteria(aNum, c1)
tell application "Microsoft Excel"
tell active workbook
tell active sheet
tell used range
if c1 is not "" then
autofilter range field aNum criteria1 c1
else
autofilter range field aNum –第2パラメータが空なら全てを表示
end if
end tell
end tell
end tell
end tell
end setCriteria
–used rangeにオートフィルタを設定
on setAutofilter()
tell application "Microsoft Excel"
tell active workbook
tell active sheet
set enable autofilter to true
tell used range
autofilter range
end tell
end tell
end tell
end tell
end setAutofilter
[/applescript]
xml構造をリアルタイムでチェックする
InDesignCS5からeventにafterSelectionChangedが追加された。
例えばテキストを触って選択状態が変更すると、eventを発行する。
CS2の頃からやってみたかったことがあった。
IDでxmlを扱うとき、オペレータが誤ってxml構造を壊してしまうことがある。
オペ中にリアルタイムで構造チェックをしたかった。
(積極的にxmlの組版をしてこなかった理由もこんなところにある)
このeventを使えば、構造をオペ中にチェックできるのではないか? と思った次第。
サンプルのxmlはこんな感じ。
dtdはこんな感じ。
タイトルは1つ無くてはいけない。ここを消すとアラートを出すようにしたい。
まず、イベントを発生させるスクリプトは、
tell application "Adobe InDesign CS5"
make event listener with properties {event type:"afterSelectionChanged",handler:"SnowLeopard:Users:yukio:Desktop:xmlValidationCheck.applescript"}
end tell
テストなのでhandlerの場所指定は、デスクトップ(SnowLeopard:Users:yukio:Desktop:)にxmlValidationCheck.applescriptという名称で置いています。
そのhandlerのスクリプトは、
tell application "Adobe InDesign CS5"
tell document 1
set validErrors to validate XML element 1
if (length of validErrors) > 0 then
set errMessage to ""
set errCnt to 0
repeat with anvalidError in validErrors
set errCnt to errCnt + 1
tell anvalidError
tell element
set errTag to name of markup tag
select XML content
end tell
set errMessage to errMessage & (errCnt as text) & "<" & errTag & ">" & error message & return
end tell
end repeat
display alert errMessage
end if
end tell
end tell
動作はこんな感じになる。クリックするとムービー始まります。
全てのeventを削除するのは、
tell application "Adobe InDesign CS5"
repeat with myCounter from 1 to (count event listeners)
delete event listener 1
end repeat
end tell
辞書を引く.appを作ってみた
ルビについて調べていたら、ルビとは無関係だが、辞書ということで、次のようなページにいきあたった。
http://sakito.jp/mac/dictionary.html#url-scheme
http://macscripter.net/viewtopic.php?id=26661
「辞書.app」って使ったことがなかった。
コピーした文字列を辞書で調べるってアプリ、上の情報からできるなっと思って作ってみた。
調べたい言葉をコピーして、アプリを実行すると、ダイアログにこんな感じで表示します。
メインのapplesript部分は、
--* 自身のバンドル・パスを得る
set self to path to me
set scriptBundlePath to (path to resource "Scripts" in bundle self) as Unicode text
set iconPath to (path to resource "applet.icns" in bundle self) as Unicode text
--* スクリプトファイルのフルパスを作成する
set pyScriptFile to scriptBundlePath & "dict2.py"
--* Unix用のパスに変換する
set pyscriptpath to quoted form of (POSIX path of pyScriptFile)
--* paste値を変数にセット
set searchword to do shell script "pbpaste"
--* shell コマンド
set command to "/usr/bin/python2.5 " & pyscriptpath & " " & quoted form of searchword
--* コマンド実行
set theResult to do shell script command
--* 表示
display dialog theResult with title searchword buttons {"Close"} default button {"Close"} with icon file iconPath
Pythonスクリプトは、
#!/usr/bin/python2.5
import sys
from DictionaryServices import *
def main():
try:
searchword = sys.argv[1].decode('utf-8')
except IndexError:
errmsg = 'You did not enter any terms to look up in the Dictionary.'
print errmsg
sys.exit()
wordrange = (0, len(searchword))
dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
if not dictresult:
errmsg = "'%s' not found in Dictionary." % (searchword)
print errmsg.encode('utf-8')
else:
print dictresult.encode('utf-8')
if __name__ == '__main__':
main()
弊社ダウンロードサイトから取得できます。
https://www.web-cte.co.jp/tools/
日本地図センター
先日(とは言っても昨年の9月の事ですが…)
“(財)日本地図センター”という所に行ってきました。
以前勤めていた会社の仕事でGIS(地理情報システム:
Geographic Information System)に携わっていた時に初めて知って以来、
その存在が気になっていたので、夏季休暇を利用して訪れることにしました。
(ここは土日祭日が休業日なので、基本的に平日でないと行く機会が無いので)
センターの中は日本全国の各種地形図をはじめ、道路地図、
地図に関する様々な書籍・グッズなどが陳列されており、
恐らく地図に関する物であれば、何でも置いてあるのではないかと思いました。
私が訪れた当初は東日本大震災から間もない頃だったので、
災害対策のハザードマップなどが平積みで置かれていたのが印象的でした。
その他ですと、近頃の乗り鉄・撮り鉄ブームを反映してか「廃線跡をめぐる」等の
趣旨の書籍がふんだんに置かれていたのが目立ちました。
場所は東急田園都市線の池尻大橋から歩いて5分程の所です。
駅から地図センターまで歩いていると、玉川通り(国道246)の脇に
首都高の大橋ジャンクションが出没します。
(コンクリートで出来た円柱型のループがある所です)
(財)日本地図センター http://www.jmc.or.jp/
Perl Script 習作
Perlのブラッシュアップのためにサブルーチン化した習作を投下します。元はInDesignから書き出したxmlテキストをタブ区切りテキストに成形するスクリプトの一部。内容は順不同に出現するフィールド名のタグで挟まれた部分を抽出して並び替えて揃えるというものです。↓こんな感じのタブ区切りテキストを表組に使えるテキストに加工するのに作りました。
↓
ちょっと記述が長いかもしれないので、折りたたんでおきます。
(シンタックスハイライトを使って整形しました。2012/2/19)