Archive for the 'Excel 2011' Category

04/30 Microsoft Office v2011 14.2.1アップデートで辞書に変更なし

先日公開された「Microsoft Office 2011 14.2.1 更新プログラム」でアップデートされた、Excel、Outlook、PowerPoint、WordのAppleScript用語辞書にとくに変更がないことを確認しました。

04/21 Office v.2011 Service Pack 2による変更点

2012/4/13にOffice v.2011 サービスパック2(バージョン14.2.0)がオンライン配布開始となりました。

例によって、アップデート後にAppleScript用語辞書をAS Dictionaryで書き出して,前バージョンの用語辞書の内容と比較を行ってみました。

 Excel 14.1.2→14.2 変更なし
 Outlook 14.1.2→14.2 変更あり(大規模な変更)
 PowerPoint 14.1.4→14.2 変更あり(「player」関連のオブジェクト、命令が追加に)
 Word 14.1.4→14.2 変更なし

……Outlookについては、日常的に使用していないので用語辞書変更の影響度合いはなんとも言えないのですが、SP2で加えるレベルではないほどの追加(あくまで個人的な意見)が行われています。

03/06 Excelの表中のデータを拾ってセルに色を付ける

Excelの表中のデータを拾ってセルに色を付けるAppleScriptです。

Excelの表の中にR,G,Bのデータが1〜5セット、空きのセットには-1,-1,-1が入っている状態で表のI〜W列を拾って、A〜E列のセルに色を付けます。行は1行目から41行目までサーチしますが、このあたりはお好きに直して使うとよろしいでしょう。

xls.jpg

スクリプト名:表中のデータを拾ってセルに色を付ける
set colCellList to {“A”, “B”, “C”, “D”, “E”}
set dataCellList to {{“I”, “J”, “K”}, {“L”, “M”, “N”}, {“O”, “P”, “Q”}, {“R”, “S”, “T”}, {“U”, “V”, “W”}}

tell application “Microsoft Excel”
  tell sheet 1
    repeat with i from 1 to 41
      
      
–1行分のループ
      
repeat with j from 1 to 5
        
        
set aColCell to (contents of item j of colCellList) & i as string
        
        
set aDataRangeBeginStr to (contents of first item of item j of dataCellList) & i as string
        
set aDataRangeEndStr to (contents of last item of item j of dataCellList) & i as string
        
        
set aRange to range (aDataRangeBeginStr & “:” & aDataRangeEndStr)
        
set aData to value of aRange
        
        
if aData = {{-1.0, -1.0, -1.0}} then –データが入っていない箇所には-1を入れてある
          exit repeat
        end if
        
        
set color of interior object of cell aColCell to first item of aData
        
      end repeat
      
    end repeat
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

02/29 Excel 2011で現在表示中のウィンドウのすべてのタブのズーム倍率を変更する

Excel 2011で現在表示中のウィンドウのすべてのシートのズーム倍率を変更するAppleScriptです。

Windowsユーザーの方からExcelの書類をメールでいただいたりすると……表示倍率が75%だったり50%だったりで、そのままMacBook Pro 17インチの1920×1200ドットの本体画面で表示すると、小さすぎて……見えません。

下手にタブ(シート)が大量に存在していたりすると、表示倍率を変更するのがうっとおしいことこの上ありません。そこで、すべてのシートを順次表示させて表示倍率を変更するAppleScriptを書いてみました。

Excelのファイルオープンの動作すべてをトラップして、ファイルオープン後に必ずこの処理を加えたくなるほどです。PFiddleSoft(旧称:PreFab Software)の「UI Actions」を併用して、メニュー項目にフォルダアクションよろしくScriptを仕掛けて、「ファイルを開く」メニュー項目の実行後に本Scriptを実行できるとよいのですが……

スクリプト名:Excel 2011で現在表示中のウィンドウのすべてのタブのズーム倍率を変更する
set zoomNum to 150 –表示倍率(10〜400)

tell application “Microsoft Excel”
  
  
tell window 1
    set curSheetNum to entry_index of active sheet –あとでこのシートの表示状態に戻す  
  end tell
  
  
tell workbook 1
    set sList to every sheet
    
repeat with i in sList
      set j to contents of i
      
      
–表示対象のWorksheet(タブ)を切り替える
      
tell j
        activate object
      end tell
      
      
–現在表示中のExcelのウィンドウの表示倍率を変更する
      
changeZoomOfCurrentExcelWindow(zoomNum) of me
    end repeat
  end tell
  
  
tell workbook 1
    activate object worksheet curSheetNum –初期表示状態に戻す
  end tell
  
end tell

–Excel 2011で現在表示中のウィンドウのズーム倍率を変更する
on changeZoomOfCurrentExcelWindow(aZoomPercent)
  tell application “Microsoft Excel”
    tell window 1
      set zoom to aZoomPercent
    end tell
  end tell
end changeZoomOfCurrentExcelWindow

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

02/23 Excel 2011ですべてのタブのデータを取得して1つにまとめる v3

Excel 2011で現在オープン中の書類のすべてのタブのデータを取得して新規ドキュメントで1つにまとめるAppleScriptの改良版です。

最初のバージョンでは、すべてのタブを順次表示しながらデータを集めていましたが、別にデータを集めるだけなら表示を切り替える必要はまったくないので、表示を切り替えるのをやめたのがこのバージョンです。

元のバージョンから比べると、かなり速くなっています(見てわかるほどに)。

スクリプト名:Excel 2011ですべてのタブのデータを取得して1つにまとめる v3
–アクティブなExcelワークブック(書類)中のすべてのワークシートの内容を取得して、新規作成したワークブック(書類)にまとめる

global aList, aList_r

set aList to {}
set aList_r to a reference to aList

tell application "Microsoft Excel"
  
  
–最前面のドキュメント中の各シート名を取得
  
tell active workbook
    set sList to name of every worksheet
    
set aCount to count every item of sList
  end tell
  
  
–各ワークシートの呼び出しループ
  
repeat with i from 1 to aCount
    set aList to aList_r & getCurrentSheetData(i, 100, "C", "L") of me –これ以上は存在していない筈の行数
  end repeat
  
end tell

–新規ワークシートを作成してそこに連結したデータを入れる
set dataLen to (length of aList)
tell application "Microsoft Excel"
  set newWS to make new workbook
  
tell newWS
    tell active sheet
      set tmpRangeStr to "A2:L" & ((dataLen + 1) as string)
      
set tmpRange to range tmpRangeStr
      
set value of tmpRange to aList
    end tell
  end tell
end tell

–現在表示中のワークシートから、データの入っている範囲を求めて中のデータを返す
on getCurrentSheetData(workSheetIndex, maxRowNum, checkColumnStr, endColumnStr)
  –Excelからデータを取得する
  
using terms from application "Microsoft Excel"
    tell application "Microsoft Excel"
      tell active workbook
        tell sheet workSheetIndex
          
          
set tmpRange to checkColumnStr & (maxRowNum as string)
          
set a to range tmpRange –B列にはかならずデータが入っている & 100行もデータは存在していない
          
set b to get end a direction toward the top
          
set endRow to first row index of b
          
          
set aRangeStr to "A2:" & endColumnStr & (endRow as string) –1行目には凡例が入っているためスキップ
          
set aRangeObj to range aRangeStr
          
          
set rangeDat to string value of aRangeObj
          
          
return rangeDat
          
        end tell
      end tell
    end tell
  end using terms from
end getCurrentSheetData

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

02/21 Excel 2011で現在オープン中の書類の選択中のシートをCSV書き出し

Excel 2011のCSV書き出しのサンプルを探していたら、現在オープン中の書類(Workbook)中のすべてのシート(Worksheet)をCSV書き出しするAppleScriptを見つけました。

そこまでの処理は必要なかったので、逆に機能を切り捨てて、最低限の機能に絞り込んでサンプルコードとして掲載しておくことに。

スクリプト名:Excel 2011で指定書類の現在のワークシートをCSV書き出し
–オリジナル作者および引用元:
– Excel Spreadsheet to CSV Files
– by Scott Wilcox <scott@dor.ky>
– https://github.com/dordotky/applescripts
– https://github.com/dordotky/applescripts/blob/master/Excel%20Worksheets%20to%20CSV.applescript

– CSV ファイルを書き出すフォルダを選択
set outFol to (choose folder with prompt "CVSファイルの書き出し先フォルダを選択:")

tell application "Microsoft Excel"
  activate
  
  
tell active sheet
    set wName to name
    
    
– Save the worksheet as a CSV file
    
set theSheetsPath to outFol & wName & ".csv" as string
    
save as filename theSheetsPath file format CSV file format with overwrite
  end tell
  
  
close every workbook saving no –クローズ
  
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

02/14 Excel 2011ですべてのタブのデータを取得して1つにまとめる

Excel 2011で最前面のドキュメント中のすべてのワークシートの内容を新規書類にまとめるAppleScriptです。

最前面のドキュメント(workbook)内のすべてのワークシート(worksheet)のデータ存在行範囲を(C列のデータをもとに)検出して、横方向にはA〜Lまでのデータを取り出します。

ex1.jpg

すべてのワークシート内のデータを取り出したあと、Excelで新規書類を作成して、そこにまとめたデータを入れます。

Excelで書類に大量のワークシート(タブ)を作って、快調にデータを入力していたところ、ワークシートに別れているのでデータの見通しがきかなくなって……手作業でまとめるのがいやだったので、その場でAppleScriptを組んでまとめさせるようにしてみました。

スクリプト名:Excel 2011ですべてのタブのデータを取得して1つにまとめる
–アクティブなExcelワークブック(書類)中のすべてのワークシートの内容を取得して、

global aList, aList_r

set aList to {}
set aList_r to a reference to aList

tell application "Microsoft Excel"
  
  
–現在表示中のワークシートのindex番号を取得する
  
tell window 1
    set curSheetNum to entry_index of active sheet –あとでこのシートの表示状態に戻す
  end tell
  
  
–最前面のドキュメント中の各シート名を取得
  
tell active workbook
    set sList to name of every worksheet
    
set aCount to count every item of sList
  end tell
  
  
repeat with i from 1 to aCount
    tell workbook 1
      activate object worksheet i –★★ 表示切り換え用の変な命令
    end tell
    
    
set aList to aList & getCurrentSheetData(100) of me –これ以上は存在していない筈の行数
    
  end repeat
  
  
–表示状態を元に戻す
  
tell workbook 1
    activate object worksheet curSheetNum –初期表示状態に戻す
  end tell
  
end tell

–新規ワークシートを作成してそこに連結したデータを入れる
set dataLen to (length of aList)
tell application "Microsoft Excel"
  set newWS to make new workbook
  
tell newWS
    tell active sheet
      set tmpRangeStr to "A2:L" & ((dataLen + 1) as string)
      
set tmpRange to range tmpRangeStr
      
set value of tmpRange to aList
    end tell
  end tell
end tell

aList

–現在表示中のワークシートから、データの入っている範囲を求めて中のデータを返す
on getCurrentSheetData(maxRowNum)
  –Excelからデータを取得する
  
using terms from application "Microsoft Excel"
    tell application "Microsoft Excel"
      tell active workbook
        tell active sheet
          set tmpRange to "C" & (maxRowNum as string)
          
set a to range tmpRange –B列にはかならずデータが入っている & 100行もデータは存在していない
          
set b to get end a direction toward the top
          
set endRow to first row index of b
          
          
set aRangeStr to "A2:" & "L" & (endRow as string)
          
set aRangeObj to range aRangeStr
          
          
set rangeDat to string value of aRangeObj
          
        end tell
      end tell
    end tell
  end using terms from
end getCurrentSheetData

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

11/09 Excel 2011で表示中のワークブック内のすべてのシートの表示状態を変更する v2

Excel 2011で、表示中の書類(ワークブック)内のすべてのシートの表示状態を変更するAppleScriptです。

Windowsユーザーから受け取ったExcel書類を開くと、なぜか高確率で表示倍率が100%以下になっており、しかも表示形式がページ区切りになっていたりで、ドットの細かいMacBook Pro 17インチあたりでオープンすると、かなり見づらいです。さらに、それをすべて手作業で変更するのはけっこう手間がかかります。

そこで、全ワークシートの表示倍率を100%に、表示形式を通常表示に切り換えるAppleScriptを作った次第です。

スクリプト名:Excel 2011で表示中のワークブック内のすべてのシートの表示状態を変更する v2

–最前面の(ドキュメントの)Windowの表示状態になっているシート名を取得
tell application “Microsoft Excel”
  tell window 1
    set curSheetNum to entry_index of active sheet –あとでこのシートの表示状態に戻す
  end tell
end tell

tell application “Microsoft Excel”
  –最前面のドキュメント中の各シート名を取得
  
tell active workbook
    set sList to count every worksheet
  end tell
  
  
–ループで各シートに指定
  
repeat with i from 1 to sList
    
    
–表示対象のワークシートを切り換え
    
tell workbook 1
      activate object worksheet i –★★ 表示切り換え用の変な命令
    end tell
    
    
–表示倍率などを設定
    
tell window 1
      set view to normal view –レイアウト表示を通常表示に
      
set zoom to 100 –表示ズーム倍率設定
    end tell
    
  end repeat
  
end tell

–最前面の(ドキュメントの)Windowの表示状態になっているシート名を取得
tell application “Microsoft Excel”
  tell workbook 1
    activate object worksheet curSheetNum –初期表示状態に戻す
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

07/10 Excelの最前面のウィンドウの表示倍率を連続して変更する

Microsoft Excel 2011の最前面のウィンドウの表示倍率を連続して変更するAppleScriptです。

こういうこともできる、というデモぐらいの意味合いのものです。

Excel 2011では、ポップアップメニューから200%までの表示を指定可能
excel2011_1.jpg

100%表示
excel2011_2.jpg

200%表示
excel2011_3.jpg

300%表示
excel2011_4.jpg

400%表示
excel2011_5.jpg

400%以上の指定はできないようになっています。
excel2011_6.jpg

スクリプト名:Excelの最前面のウィンドウの表示倍率を連続して変更する 
tell application "Microsoft Excel"
  tell window 1
    repeat with i from 100 to 400 by 100
      set zoom to i
      
delay 1
    end repeat
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/10 FileMaker Proで書き出したTab区切りテキストファイルをExcelで読み込む

FileMaker Proで書き出したTab区切りテキストファイルをExcel 2011で読み込むAppleScriptです。

Tab区切りテキストであればとくにFileMaker Proが書き出したものでなくてもかまいません。

スクリプト名:ExcelでTab区切りテキストを読み込む
set aFile to choose file
set aFile to aFile as string

tell application "Microsoft Excel"
  –指定のTab区切りテキストファイルを読み込む
  
open text file filename aFile data type delimited with tab
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

06/05 Excelのアクティブなワークシートをコピーして新規ワークブックを作成

Office 2011のExcel(Excel 2011)で、現在アクティブなワークシート(表示中のワークシート)をコピーして、そのデータで別のワークブックを新規作成するAppleScriptです。

このようなExcelの書類があったときに、本Scriptを実行すると……

ex1s.jpg

このように、元のSheetの内容を使って新規Workbookが作成されます。

ex2s.jpg

スクリプト名:Excelのアクティブなワークシートをコピーして新規ワークブックを作成
tell application “Microsoft Excel”
  tell active workbook
    tell active sheet
      copy worksheet
    end tell
  end tell
end tell

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に