Applescript - 在Excel中插入图像时保留图像尺寸

时间:2016-09-28 21:09:43

标签: excel applescript automator

我目前正处于创建工作流程的最后阶段,这个工作流程涉及使用Excel中的数据插入图像这一非常繁琐且耗尽的每周流程。

我已经设法制定了Automator和Applescript的组合,以实现一个工作流程,在excel中获取图像,在适当的位置,但使用我当前的AppleScript,它们以设定的大小,100 x 100 < / p>

问题在于:

我需要所有图像都以100px的高度进入,但我需要宽度灵活。我似乎无法找出最后一部分,我认为这将是一个用于锁定宽高比的Applescript命令。

以下是我用于实际图像导入的当前AppleScript:

tell application "Finder"  
    set imageFolder to "Macintosh HD:Users:adlife:Desktop:Temporary_Image_Hold - Copyright Filings"  
end tell

tell application "Finder" 
    set imageList to files of folder imageFolder
end tell

tell application "Microsoft Excel"  
    set imageHeight to 100  
    set imageWidth to 100  
    set imageCount to count of imageList  
    set theRange to active cell of worksheet 1 of workbook 1  
    set thisStep to 0  
    set theLeft to left position of active cell  

repeat with imageFile in imageList

    set theTop to (top of active cell)

    set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:(imageFile as text), height:imageHeight, width:imageWidth, top:theTop, left position:theLeft, placement:placement move}


    set thisStep to thisStep + 1
end repeat  
end tell

我在El Capitan上运行Microsoft Excel 2011

非常感谢任何帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

没有图像属性来强制纵横比。用户界面中的复选框用于在用户移动或调整图像大小时锁定配给。

解决方案是获取图像尺寸并将比率(宽度/高度)应用于您定义的图像高度(= 100)。使用“图像事件”说明提供尺寸。然后,您可以在“制作新图片”中使用新宽度作为“宽度”属性。

set imageFolder to "Macintosh HD:Users:adlife:Desktop:Temporary_Image_Hold - Copyright Filings"as alias

tell application "Finder" to set imageList to files of folder imageFolder

tell application "Microsoft Excel"
set imageHeight to 100
set imageWidth to 100 -- just a default value, not really needed
set imageCount to count of imageList
set theRange to active cell of worksheet 1 of workbook 1
set thisStep to 0
set theLeft to left position of active cell

repeat with imageFile in imageList

    set theTop to (top of active cell)
    tell application "Image Events" -- open the image and get dimensions
        set theFile to imageFile as alias
        set myImage to open theFile -- don't worry, it will not open the fiel for user, only for the script !
        set {W, H} to dimensions of myImage
        set imageWidth to imageHeight * W / H -- set width using the proportion of image = W/H
    end tell
    set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:(imageFile as text), height:imageHeight, width:imageWidth, top:theTop, left position:theLeft, placement:placement move}

    set thisStep to thisStep + 1
end repeat --next image to place
end tell

我测试了那个脚本(当然还有我自己的图像文件夹!),它适用于El Capitain和Excel 2011。

如您所见,我还更改了代码的第一行:

要设置imageFolder,您无需在tell“Finder”块中执行此操作。但是,必须将其设置为“别名”。

要获取所有文件,您可以在单行中告诉“Finder”,而不使用tell / end tell块。 (更简单)