2017-09-14 39 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等,并用该单元格的值执行特定操作(网页是静态的)。

我希望这可以帮助你。

相关问题