2017-09-14 66 views
0

我正在尋找關於AppleScript的幫助(不幸的是,工作原因僅限於AppleScript)。具體來說,我無法弄清楚如何在Excel列中複製並粘貼每個單元格時循環訪問excel列。AppleScript,循環遍歷列(直到空),複製並粘貼到網絡表格

這裏是我做到目前爲止有:

to clickID(theId) -- clickID function start 
    tell application "Safari" 
    do JavaScript "document.getElementById('" & theId & "').click();" in document 1 
    end tell 
end clickID 
--__________________________________________ 
tell application "Microsoft Excel" 
    activate 
    open "Filepath/Starfox Tester.xlsx" --changed filename for privacy 
    get active workbook 
    select cell "A2" --Headers will be in column A1, hence why selecting A2 
    tell application "System Events" to tell process "Microsoft Excel" to keystroke "c" using command down 
end tell 

do shell script "open -a Safari 'https://google.com'" 
    delay 2 --for website to load 
    clickID("lst-ib") 
    tell application "System Events" to tell process "Safari" to keystroke "v" using command down 

到目前爲止,這工作;我瞭解如何手動執行此操作,但由於每次運行此腳本時,我的列都有不同數量的行,因此我需要它能夠循環直到找到空單元格。

任何幫助將不勝感激!謝謝!

更新: 我認爲最簡單的方法可能是if else循環。我的基本想法是運行一個處理程序(函數),如果引用中的單元格不是空白的。由於我可能不會超過50個單元格,因此我計劃在(A1 - A50)中對其進行硬編碼並每次運行處理程序。

我目前正試圖弄清楚如何編寫處理程序。以下是我迄今爲止:

to populateVal(cellValue) 
    tell application "Microsoft Excel" 
    get active workbook 
    select cellValue 
    tell application "System Events" to tell process "Microsoft Excel" to keystroke "c" using command down 
     activate Safari 
     clickID("lst-ib") 
     tell application "System Events" to tell process "Safari" to keystroke "c" using command down 
    end tell 
end populateVal 

do shell script "open -a Safari 'https://google.com'" 

tell application "Microsoft Excel" 
activate 
open "Filepath/Starfox Tester.xlsx" 

    if cell "A2" ≠ "" then 
    populateVal("A2") 
    else 
    display dialog "Done!" 
    end if 

end tell 

我目前得到一個錯誤說,創先爭優「無法繼續populateVal」。如果我拿掉A2的圓括號,我會得到一個錯誤,說A2「沒有定義」。任何幫助,將不勝感激!

回答

0

經過一番修補之後,我想出了一個可行的解決方案。

on selectVal(cellValue) 
tell application "Microsoft Excel" 
    activate 
    get active workbook 
    select cell cellValue 
    tell application "System Events" to tell process "Microsoft Excel" to keystroke "c" using command down 
    delay 0.5 
    tell application "System Events" to tell process "Microsoft Excel" 
     key code 124 
    end tell 
    tell application "Safari" 
     activate 
     my clickID("lst-ib") 
     tell application "System Events" to tell process "Safari" to keystroke "v" using command down 
    end tell 
end tell 
end selectVal 
---- 

do shell script "open -a Safari 'https://google.com'" 
delay 2 --for website to load 

tell application "Microsoft Excel" 
open "Filepath/Starfox Tester.xlsx" 
end tell 

selectVal("A2") 
delay 1.5 
selectVal("B2") 
delay 1.5 

從本質上講,我創建了一個處理器/函數複製一個值,手動進入下一個單元格,執行對網頁中的特定動作。我背後的思想過程是:構建一個處理單個數據行的處理程序(我將設置列,但可變的行),然後調用該處理程序爲每一行數據,從A2開始,移動到B2,C2等,並用該單元格的值執行特定操作(網頁是靜態的)。

我希望這可以幫助你。

相關問題