2013-07-23 26 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