【发布时间】:2017-09-14 21:22:41
【问题描述】:
我正在寻求有关 AppleScript 的帮助(不幸的是,由于工作原因仅限于 AppleScript)。具体来说,我无法弄清楚如何在复制和粘贴该列中的每个单元格时循环遍历 excel 列。
这是我目前所拥有的:
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
我目前收到一条错误消息,提示 Excel“无法继续 populateVal”。如果我去掉 A2 周围的括号,我会收到一条错误消息,指出 A2“未定义”。任何帮助将不胜感激!
【问题讨论】:
标签: excel loops if-statement applescript