Microsoft Excelで、指定画像を指定セルに貼り込むAppleScriptです。指定セルの座標を求めて、そこを左上の位置として画像を貼り込みます。
前バージョンが「動かない」という話があって、「そんなバカな?!」と、半信半疑でユーザーと同じドライブ名、フォルダ名、ファイル名、画像形式を採用したら(JPEG形式)同様にエラーになりました。自分がテストしたのは内蔵SSDで、画像形式はPNGだったのですが……
そこで、書き換えてJPEG画像でも、外付けドライブでもエラーにならないように書き換えてみました。
AppleScript名:指定セルに指定画像を貼り込む v2.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 –Excel Cell Address (A1) –> Column No, & Row No. set targCellAddr to "C5" 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 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 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 1 times, 1 visits today)