我是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

  • 选择值范围(行,列) . 如果没有选择脚本将显示消息 .

  • 从范围选择行和列重复数据直到数据结束 . 结果是字符串 .

  • 读取数据的最后位置并删除“,”

  • 设置AppleScripts分隔符“,”

  • 删除“,”并打印到变量

  • 包含表属性的新表

  • 在第1行和第A列到第H列中设置 Headers 值

  • 收到变量并写入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

数字中的数据示例

enter image description here

期待结果

enter image description here