Applescript将值作为字符串获取并放入变量

时间:2018-03-11 14:18:47

标签: string applescript getvalue iwork

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

  1. 选择值范围(行,列)。如果没有选择 脚本将显示消息。
  2. 从范围选择行和列重复数据直到数据结束。结果是字符串。
  3. 阅读数据的最后位置并删除“,”
  4. 设置AppleScripts分隔符“,”
  5. 删除“,”并打印到变量
  6. 包含表格属性的新表格
  7. 在第1行设置标题,在第A列设置为H
  8. 收到变量并写入G列并循环直到结束 数据。
  9. 示例源代码

    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

0 个答案:

没有答案