使用Firefox在Excel中的Applescript循环/重复功能

时间:2013-07-23 18:49:44

标签: firefox loops applescript repeat applescript-excel

我是Apple的新手,对此的任何帮助都将受到高度赞赏。我认为对于拥有比我更多技能的人来说,这将是一个很好的挑战。所有描述都是在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. 关闭工作簿
  12. 我已将下面的脚本放在一起,这样就可以了。麻烦的是我无法重复它。重复函数需要重复执行整个脚本,首先将单元格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"
    

    真诚地感谢任何人都可以弄清楚如何做到这一点。很长一段时间以来,我一直在思考这个问题。 附:如果有人知道一本关于使用applescript运行excel的好书,请告诉我。

    再次感谢!

1 个答案:

答案 0 :(得分:0)

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

  • 识别递归,即程序改变的地方......你提到了它:

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

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

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