2013-07-23 25 views
0

我是新来的applescript,对此有任何帮助将不胜感激。我认为对于技能比我更高的人来说,这将是一个很好的挑战。所有这一切说明已经在雪豹完成与MS Office 2011使用Firefox的Excel中的Applescript循环/重复函数

我的URL(在单元格Q2开始)的名单,我已经得到了AppleScript的执行以下一系列任务:

  1. 打开MS Excel
  2. 创建新工作簿
  3. 从MS Excel单元格Q2复制URL。
  4. 粘贴到Firefox地址栏并去。
  5. 点击Firefox菜单栏功能“视图1”(由附加)
  6. 点击Firefox菜单栏功能“视图2”(从附加)
  7. 点击Firefox菜单栏功能“复制所有表”(从附件)
  8. 在Excel中创建新工作簿
  9. 粘贴复制的文本到新的工作簿单元格A1
  10. 保存新的工作簿作为workbook002.xlsx
  11. 关闭工作簿

我已经把下面的脚本放在一起,它工作。麻烦的是我不能重复它。重复执行整个脚本需要重复功能,首先将单元格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.如果有人知道一本关于运行exceles的好书,请告诉我。

再次感谢!

回答

0

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

  • 识别递归,即在程序改变的地方......你mentionned它:

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 

和工作在“我”变量。 注意,这个“我”变量也将出现在工作簿

"workbook002.xlsx"的名称变为("workbook00" & i & ".xlsx")

  • 你还必须确定递归结束的地方。在我们的例子中,意思是“我”的最大值。 假设我们的例子是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 

end tell

这应该做的伎俩......

+0

这是一个很大的帮助。 我唯一的问题是,我无法将新工作簿保存为输出文件名,因为我无法在Excel字典中找到该工作簿,但是我将它们保存为sheet1,sheet2等。只要他们'为了知道哪些是excel将它们保存在最近保存文件的位置。脚本运行完毕后,文件可以用automator重命名。 –

+0

嘿@ osbourne.cox! 有一种方法可以在Excel字典中保存工作簿。 这个命令是“save workbook as”,你可以这样使用它,例如: 请保存工作簿作为活动工作簿文件名吨忘记投票的答案:) – Zitoun