Microsoft Excelで、指定画像を指定セルに貼り込むAppleScriptです。指定セルの座標を求めて、そこを左上の位置として画像を貼り込みます。
本Scriptで、セルアドレスの指定時に「B4」などとアルファベット大文字で指定してください。英小文字で指定するとエラーになります(以後のバージョンでは、ignoring caseで判断部分を囲って対処しています)。
AppleScript名:指定セルに指定画像を貼り込む.scpt |
— – Created by: Takaaki Naganoya – Created on: 2024/12/05 — – 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 –Excel Cell Address (A1)–+ Column No, & Row No. set targCellAddr to "A2" set {targCellCol, targCellRow} to cellToNumber(targCellAddr) of AddressEncoder log {targCellCol, targCellRow} –Column No, & Row No. –> x position & y position set {targCellX, targCellY} to retExcelCellPositiont(targCellCol, targCellRow) of me log {targCellX, targCellY} –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|() –Place image on worksheet and resize and relocate it tell application "Microsoft Excel" activate tell active workbook tell active sheet set aPicShape to make new shape at the beginning set width of aPicShape to (overlaySize’s |width|) set height of aPicShape to (overlaySize’s |height|) set top of aPicShape to targCellY set left position of aPicShape to targCellX user picture of aPicShape picture file anImagePOSIX end tell end tell end tell –指定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 set columnNumber to columnNumber * 26 + (offset of char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ") end using terms from end repeat — 数値を返す return {columnNumber, (rowPart as integer)} end cellToNumber end script |
More from my site
(Visited 14 times, 4 visits today)