1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

『Excelで複数ファイルの特定のセルの値を一つの表にまとめる方法【AppleScript】』私はこう書いた

1
Posted at

Twitterのタイムラインで見かけたExcelのAppleScript。(Excelで複数ファイルの特定のセルの値を一つの表にまとめる方法【AppleScript】
自分ならどう書くかなぁ、と思ってちょっと挑戦。
実際の仕様はわかりませんが、できるだけ同じになるようにしたつもりです。

使い方

ソースコードをスクリプトエディタにペースト後、アプリケーション方式で保存します。
値を収集したいファイルをアイコンにドラッグ&ドロップすれば同じフォルダに収集したファイルが保存されます。

デフォルトではB2セルの値を収集するようにしてあるので、アイコンをダブルクリックでご自身の収集したいセルを指定してください。変更した値は保持されます。

解説

Droplet化

on runハンドラで取得するセルを指定しています。property宣言しているので入力した値は保持され次回以降同じセルが対象なら再指定は不要です。
また、オリジナルではExcelのフォルダや新規ファイルの指定、そして取得するセルの指定を必ずダイアログが出て指定していたので手数がへらせました。

まとめて処理

1ファイルごとに新規ファイルに処理を行っていたので、全部のファイルの値を取得してからに変更しました。よほどファイル数が増えない限りはオーバーフローは起こらないでしょう(多分……

sum関数で計算

これはオリジナルにはなかったのですがせっかく収集したデータなのでsumまでやって置いた方が利便性が上がるかなと考えてつけました。

ファイル保存

最後に処理したファイルと同じフォルダ内に{年月日}_収集.xlsxという名前で保存します。

(*
	複数ファイルの指定セルの値を収集して新規ファイルを作成  

*)


--
property getValCell : "B1"

on run
	set tmpCell to text returned of (display dialog "取得するセルを指定してください" default answer getValCell)
	if tmpCell is not "" then set getValCell to tmpCell
end run

on open fileList
	
	set valList to {}
	
	repeat with aFile in fileList
		
		set rowVal to {}
		
		set aFileInfo to info for aFile
		set aNameFull to name of aFileInfo
		set aExt to name extension of aFileInfo
		
		if aExt is "xlsx" or aExt is "xls" then
			tell application "Microsoft Excel"
				activate
				open workbook workbook file name aFile as string
				
				set aName to my regName(aNameFull, aExt)
				set rowVal to {aName} & {value of cell getValCell}
				
				close active workbook saving no
				
			end tell
		end if
		
		set valList to valList & {rowVal}
		
		set rowCount to count of valList
		
	end repeat
	
	my newFile(rowCount, valList)
	my setSum(rowCount)
	
	my saveFile(aFile, "収集")

	beep 2
	display dialog "作業終了" buttons {"OK"} default button "OK"
	
end open



--登録ファイル名取得
on regName(aNameFull, aExt)
	set serch to "^\\d+_(.+)\\." & aExt
	set replace to "$1"
	
	set cmd to "ruby -Ku -e " & quoted form of ("print %Q{" & aNameFull & "}.gsub(/" & serch & "/){$1}")
	return do shell script cmd
end regName

--新規ファイル作成
on newFile(rowCount, valList)
	tell application "Microsoft Excel"
		
		make new workbook
		
		set c1 to "A1"
		set c2 to ("B" & rowCount) as string
		set pasteArea to c1 & ":" & c2 as string
		set value of range pasteArea to valList
		
	end tell
end newFile

--計算フィールド設定
on setSum(rowCount)
	tell application "Microsoft Excel"
		set cell_sum to "B" & rowCount + 1
		set sum to "=SUM(B1:B" & rowCount & ")"
		
		set value of range cell_sum to sum
		
	end tell
end setSum



--ファイル保存
on saveFile(aFile, tmpName)
	tell application "Finder"
		set tmpFolder to (parent of aFile) as string
	end tell
	set saveName to do shell script "date +%y%m%d" & "_" & tmpName & ".xlsx"
	set savePath to tmpFolder & saveName
	
	tell application "Microsoft Excel"
		
		save as active sheet filename savePath file format workbook normal file format
		close active workbook saving no
		
	end tell
end saveFile

Excelのスクリプトはあまり書かないので課題があると楽しい(笑

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?