【问题标题】:Applescript to copy images from a folder on my desktop to excel column AApplescript将图像从我桌面上的文件夹复制到excel列A
【发布时间】:2019-09-06 15:15:24
【问题描述】:

总结问题:

我想创建一个 Applescript 将图像从我桌面上的文件夹粘贴到 Excel 列。有数千张图片,因此手动将照片添加到 Excel 中是行不通的。

假设该文件夹有 500 张图片,标题为: Image_1.jpg... Image_2.jpg.. ... Image_500.jpg

我想将这些实际图像放在 B 列中的 excel 文件中

看起来像这样:

Column A          Column B
Image_1.jpg       Actual image of Image_1.jpg
Image_2.jpg       Actual image of Image_2.jpg
...               ...
Image_500.jpg     Actual image of Image_500.jpg

我不需要任何花哨的东西来计算文件夹中有多少图像或任何东西,但我只是不知道如何告诉 excel 粘贴图像 1,然后向下移动一行,粘贴第二个等等。应该也以单元格为中心。

我可以手动更改行数.. 所以如果我有 382 张图像或 1382 张图像,我可以手动将其输入到该特定文件夹的脚本中,并在下次运行时更改数量(如果它不同)照片数量。

我基本上是在 Excel 上制作规格表,我可以在其中做标题,在下一栏中,是实际照片。

提供背景,包括您已经尝试过的内容

我已经在 filePath、imagefolder、Imagelist 上尝试了一些选项,但都没有。

显示一些代码

到目前为止,唯一可行的方法是粘贴一张单独的图片。我打算这样做,然后复制粘贴同一行,将其从 Image_1.jpg 更改为 2,3,4,...500... 但可能太耗时了。

此方法也仅将照片插入为一个小方形样本,而不是清晰的图像。

set theFilePath to "Macintosh HD:Users:Christian:Desktop:ImageFolder:Image_1.jpg"

tell application "Microsoft Excel"
    tell active sheet
make new picture at end with properties {file name:theFilePath}

    end tell
end tell

描述预期和实际结果,包括任何错误消息

我不断遇到的错误是:

Microsoft Excel 出现错误:无法制作班级图片。" 从图片到班级的数字 -2710

【问题讨论】:

  • 问题:Excel 实际上并不将图片存储在单元格中。它可以做的最好的事情是在工作表中与特定单元格对齐的图片。这足够了吗?它将是纯视觉的(例如,您将无法以编程方式查询单元格以检索图像)。此外,还必须对图像进行一些缩放。您希望电子表格中的目标大小是多少?

标签: excel applescript


【解决方案1】:

这个脚本或多或少应该可以解决问题。

set imageFolder to POSIX path of (choose folder)

tell application "System Events"
    set imageList to POSIX path of files of folder imageFolder whose visible is true
end tell

set targetImageHeight to 140
set largestImageWidth to 0

set imageCount to count of imageList
set idx to 1
repeat with imageFile in imageList
    set targetImageWidth to widthOfPic(imageFile, targetImageHeight)
    tell application "Microsoft Excel"
        activate
        set nameCell to cell ("A" & idx as string) of worksheet 1 of workbook 1
        set value of nameCell to my imageName(imageFile)
        set imageCell to cell ("B" & idx as string) of worksheet 1 of workbook 1
        set theLeft to left position of imageCell
        set theTop to (top of imageCell)

        set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name:POSIX file imageFile, height:targetImageHeight, width:targetImageWidth, top:theTop, left position:theLeft, placement:placement move}

        set row height of imageCell to targetImageHeight
        if targetImageWidth > largestImageWidth then
            set largestImageWidth to targetImageWidth
            -- this next line is purely cosmetic, to make it look
            -- like the image is in the cell
            set column width of imageCell to largestImageWidth * 0.1625
        end if
    end tell
    set idx to idx + 1
end repeat

tell application "Microsoft Excel"
    tell worksheet 1 of workbook 1
        tell column "A:A"
            autofit
            set vertical alignment to vertical alignment center
        end tell
    end tell
end tell

on imageName(imagePath)
    tell application "System Events"
        return name of file imagePath
    end tell
end imageName

on widthOfPic(picPath, targetH)
    set sipsOutput to do shell script "sips -g pixelHeight -g pixelWidth " & quoted form of picPath
    set h to (last word of paragraph 2 of sipsOutput) as integer
    set w to (last word of paragraph 3 of sipsOutput) as integer
    return targetH * w / h
end widthOfPic

【讨论】:

  • 对不起,我错过了您之前的评论 - 是的,这是完美的,您太棒了。谢谢一百万!
猜你喜欢
  • 2017-04-30
  • 1970-01-01
  • 2022-01-02
  • 1970-01-01
  • 2010-10-28
  • 2011-06-12
  • 1970-01-01
  • 2018-05-17
  • 2014-05-26
相关资源
最近更新 更多