我是 AppleScript 的初学者。我尝试编写 AppleScript 以获取多行中的文本值并写入变量,并且它应该循环直到数据结束。编译并运行 AppleScript 后结果是正确的,但在没有编译的情况下运行 agin 后结果不正确。
这是第一轮运行后的结果:
{“3875.0", "2383.25", "missing value", "missing value", "missing value", "180.0", "300.0"}
跑完第二轮
{"3", "8", "7", "5", ".", "0", "", "", "2", "3", "8", "3", ".", "2", "5", "", "", "m", "i", "s", "s", "i", "n", "g", " ", "v", "a", "l", "u", "e", "", "", "m", "i", "s", "s", "i", "n", "g", " ", "v", "a", "l", "u", "e", "", "", "m", "i", "s", "s", "i", "n", "g", " ", "v", "a", "l", "u", "e", "", "", "1", "8", "0", ".", "0", "", "", "3", "0", "0", ".", "0"}
解释 AppleScript
- 选择值的范围(行、列)。如果不选择脚本将显示消息。
- 从范围选择行和列重复数据,直到数据结束。结果是字符串。
- 读取数据的最后位置,去掉“,”</li>
- 设置 AppleScripts 分隔符“,”</li>
- 删除“,”并打印到变量
- 用表的属性创建新表
- 将第 1 行和 A 列中的值设置为 H
- 接收到变量并写入 G 列并循环直到数据结束。
示例源代码
try
tell application "Numbers" to tell front document to tell active sheet
set selected_table to first table whose class of selection range is range
tell selected_table
set my_selection to the selection range
set begCol to address of first column of my_selection
set endCol to address of last column of my_selection
set begRow to address of first row of my_selection
set endRow to address of last row of my_selection
set delimiters to ","
set getVal to ""
repeat with j from begRow to endRow
repeat with i from begCol to endCol
set getVal to getVal & (value of cell j of column i of selected_table) & delimiters as string
set getVal to getVal
end repeat
end repeat
end tell
end tell
set getVal to (characters 1 thru -2 of getVal) as string
set AppleScript's text item delimiters to ","
set AmountVal to text items of getVal
tell application "Numbers"
activate
tell front sheet of front document
set myOriginalTable to front table
set setCols to 8
set myNewTable to make new table with properties ¬
{row count:(count of AmountVal) + 1, column count:setCols, header column count:0}
tell myNewTable
set value of cell 1 of column "A" to "cardNo"
set value of cell 1 of column "B" to "emCode"
set value of cell 1 of column "C" to "emName"
set value of cell 1 of column "D" to "itemCode"
set value of cell 1 of column "E" to "itemName"
set value of cell 1 of column "F" to "effDate"
set value of cell 1 of column "G" to "amt"
set value of cell 1 of column "H" to "remark"
set x to 2
repeat with eachAmount in AmountVal
set value of cell x of column "G" to eachAmount
set x to (x + 1)
end repeat
end tell
end tell
end tell
display notification "Already Done!" with title "Numbers"
on error
display dialog "Select a range first and then try again"
end try
数字数据示例
期待结果