【问题标题】:Applescript Loop/Repeat Functions in Excel with Firefox带有 Firefox 的 Excel 中的 Applescript 循环/重复函数
【发布时间】:2013-07-23 18:49:44
【问题描述】:

我是 applescript 的新手,对此的任何帮助将不胜感激。我认为对于比我有更多技能的人来说,这将是一个很好的挑战。所描述的所有内容都是在 Snow Leopard 中使用 MS Office 2011 完成的。

我有一个 URL 列表(从单元格 Q2 开始),并且我有 applescript 来执行以下一系列任务:

  1. 打开 MS Excel
  2. 创建新工作簿
  3. 从 MS Excel 单元格 Q2 复制 URL。
  4. 粘贴到 Firefox 地址栏即可。
  5. 点击 Firefox 菜单栏功能“View1”(来自插件)
  6. 点击 Firefox 菜单栏功能“View2”(来自插件)
  7. 点击 Firefox 菜单栏功能“复制所有表格”(来自插件)
  8. 在 Excel 中创建新工作簿
  9. 将复制的文本粘贴到新工作簿单元格 A1
  10. 将新工作簿另存为 workbook002.xlsx
  11. 关闭工作簿

我为此编写了下面的脚本,它可以工作。问题是我不能让它重复。需要repeat函数来重复整个脚本的执行,首先将单元格Q2更改为Q3,依此类推,直到最后一个单元格包含值0,即结束循环的信号,并用名称保存每个工作簿按顺序(workbook002 然后 workbook003 等)。我认为不需要更改 firefox 部分,因为步骤始终相同。

这是脚本:

do shell script "open -a /Applications/Microsoft\\ Office\\ 2011/Microsoft\
\ Excel.app ~/Desktop/KospiSection2.xlsx"
tell application "Microsoft Excel"
set sourceBook to workbook "Section2.xlsx"
set sourceRange to get range "Q2" of sheet 1 of sourceBook
copy range sourceRange
end tell
tell application "Firefox"
activate
tell application "System Events"
tell process "Firefox"
click menu item "PasteGo" of menu "Tools" of menu bar 1
delay 3
click menu item "View1" of menu "View" of menu bar 1
delay 10
click menu item "View2" of menu "View" of menu bar 1
delay 2
click menu item "Copy all Tables (2)" of menu "Edit" of menu bar 1
delay 3
click menu item "Close Tab" of menu file of menu bar 1
end tell
end tell
end tell
tell application "Microsoft Excel"
make new workbook
delay 2
tell active sheet of active workbook
paste worksheet destination range "A1"
delay 2
end tell
end tell
do shell script "save as -a /Applications/Microsoft\\ Office\\ 2011/Microsoft\\
Excel.app ~/Desktop/workbook002.xlsx"

如果有人能弄清楚如何做到这一点,真诚感谢。很长一段时间以来,我一直在为此头疼。 p.s.如果有人知道一本关于使用 applescript 运行 excel 的好书,请告诉我。

再次感谢!

【问题讨论】:

    标签: excel macos firefox applescript


    【解决方案1】:

    好吧... 你需要的东西:

    • 确定递归,即程序变化的地方...你提到了它:

    range "Q2" of sheet 1 of sourceBook 应该进化在range "Q3" of sheet 1 of sourceBook

    在 applescript 术语中,你会这样写:

    set sourceRange to get range ("Q" & i) of sheet 1 of sourceBook
    

    并处理“i”变量。 请注意,这个“i”变量也会出现在工作簿的名称中

    "workbook002.xlsx" 变为 ("workbook00" & i & ".xlsx")

    • 您还必须确定递归停止的位置。在我们的例子中,意思是“i”的最大值。 假设我们的示例为 20。你的代码变成了:

    tell application "Microsoft Excel"

    set sourceBook to workbook "Section2.xlsx"
    
    repeat with i from 2 thru 20
       set sourceRange to get range ("Q" & i) of sheet 1 of sourceBook
       ...
       set outputFileName to "workbook00" & i & ".xlsx" -- Well there should be some work on the name so that it looks like what you expect, but that's another thing)
       save workbook as active workbook filename ({path to desktop folder as string, outputFileName} as string) with overwrite -- keep the 'save as' action within the loop, and within the "tell Excel"     
       end -- repeat
    

    结束告诉

    这应该可以解决问题...

    【讨论】:

    • 这是一个很大的帮助。我遇到的唯一问题是我无法将新工作簿保存为输出文件名,因为我在 excel 字典中找不到它,但是我将它们保存为 sheet1、sheet2 等。只要它们在order 我知道哪个加上 excel 将它们保存在保存文件的最新位置。脚本运行完毕后,可以使用 automator 重命名文件。
    • 嘿@osbourne.cox!有一种方法可以在 Excel 字典中保存工作簿。命令是“将工作簿另存为”,例如:save workbook as active workbook filename ({path to desktop folder as string, "myOutputFileName"} as string) with overwrite 请不要忘记为答案投票 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2017-01-08
    • 2015-08-01
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多