指定範囲(30×30)のセルの位置に画像を配置するAppleScriptです。
画像配置Scriptに対して「特定のセルを指定すると配置できずにエラーになる」という反応があったので、テストを行うために書いたものです。
結局、セルアドレスを「B5」ではなく「b5」と小文字で書いたことがエラーの原因でした。Excelのセルアドレスを小文字で書くなんて聞いたことがないのですが、たしかにこれに対処できないのは問題です。
なので、セルのcolumnを計算するルーチンで大文字/小文字を無視するように書き換えました。
AppleScript名:指定範囲のセルの上に画像を配置.scpt |
— – Created by: Takaaki Naganoya – Created on: 2024/12/06 — – Copyright © 2024 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions property |NSURL| : a reference to current application’s |NSURL| property NSImage : a reference to current application’s NSImage –Select Image file and get width and height set anImagePath to choose file set anImagePOSIX to POSIX path of anImagePath set aURL to |NSURL|’s fileURLWithPath:anImagePOSIX set aImage to NSImage’s alloc()’s initWithContentsOfURL:aURL set overlaySize to aImage’s |size|() repeat with targCellRow from 1 to 30 repeat with targCellCol from 1 to 30 –Column No, & Row No. –> x position & y position set {targCellX, targCellY} to retExcelCellPositiont(targCellCol, targCellRow) of me –Place image on worksheet and resize and relocate it tell application "Microsoft Excel" activate tell active workbook tell active sheet ignoring application responses set newPic to make new picture at beginning with properties {file name:(anImagePOSIX), height:(overlaySize’s |height|), width:(overlaySize’s |width|), top:targCellY, left position:targCellX} end ignoring end tell end tell end tell end repeat end repeat –指定Row, ColumnのCellのpositionを返す on retExcelCellPositiont(x as integer, y as integer) tell application "Microsoft Excel" tell active workbook tell active sheet tell row y tell cell x set xMin0 to left position set yMin0 to top set xWidth0 to width set yHeight0 to height end tell end tell end tell end tell end tell return {xMin0, yMin0} end retExcelCellPositiont 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 ignoring case set columnNumber to columnNumber * 26 + (offset of char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ") end ignoring end using terms from end repeat — 数値を返す return {columnNumber, (rowPart as integer)} end cellToNumber end script |