如何使用AppleScript设置Numbers单元格中的小数位数?

时间:2016-06-29 04:38:31

标签: numbers applescript spreadsheet

我可以使用以下方式在Numbers电子表格中设置单元格的格式:

tell application "Numbers"
    activate
    tell document 1
        tell active sheet
            set the selectedTable to ¬
                (the first table whose class of selection range is range)
        end tell
        tell selectedTable
            tell column "J"
                set the format of cell 3 to number
            end tell
        end tell
    end tell
end tell

但是如何设置单元格的小数位数?

3 个答案:

答案 0 :(得分:2)

不幸的是,我没有数字,因此我无法直接查看字典,但理论上你应该为小数位的单元格提供一个属性。

This页面讨论了如何在应用程序中手动更改小数位。因此,您可以在脚本中执行类似操作。

你是否尝试过按照这些方式做某事......

tell application "Numbers"
    tell document 1
        tell selectedTable
            tell column "J"
                set props1 to properties of cell 3
                set props2 to properties of format of cell 3 
            end tell
        end tell
    end tell
end tell

看看前面代码示例中的props1和props2包含的内容。运气好的话,其中一个会引用"十进制位置" (或类似的东西)。对于我的代码中的任何错误,很抱歉,正如我所提到的,我没有数字来实际测试任何错误。

答案 1 :(得分:2)

令人遗憾的是,我认为这是不可能的,至少不是使用Numbers v3.6.2。数字字典显示a'小数' cellrange

不存在属性

拍摄你的剧本并随之运行:

 tell application "Numbers"
     activate
     tell document 1
         tell active sheet
             set the selectedTable to ¬
                 (the first table whose class of selection range is range)
         end tell
         tell selectedTable
             tell column "J"
                 set the format of cell 3 to number
                 get properties of cell 3
             end tell
         end tell
     end tell
 end tell

显示属性:

{vertical alignment:top, row:row "3" of table 1 of sheet 1 of document id "6F1021A0-BA54-47A2-A2DE-15B7E8DAFCED" of application "Numbers", class:cell, font name:"Helvetica", formatted value:"1.2345E+02", background color:missing value, formula:missing value, name:"J3", text wrap:true, text color:{0, 0, 0}, alignment:auto align, column:column "J" of table 1 of sheet 1 of document id "6F1021A0-BA54-47A2-A2DE-15B7E8DAFCED" of application "Numbers", format:scientific, font size:10.0, value:123.45}

..没有一个看起来像小数的属性: - (

答案 2 :(得分:2)

不幸的是Apple的数字字典不支持设置小数位,但是你可以用GUI脚本来完成。

由于GUI脚本很大程度上取决于软件版本,因此适用于Numbers 3.6.2

tell application "Numbers"
    activate
    tell document 1
        tell active sheet
            set the selectedTable to ¬
                (the first table whose class of selection range is range)
        end tell
        tell selectedTable
            tell column "J"
                set the format of cell 3 to number
            end tell
        end tell
    end tell
end tell

tell application "System Events"
    tell process "Numbers"
        tell radio group 1 of window 1
            if value of radio button "Cell" is 0 then
                click radio button "Cell"
                repeat until value of radio button "Cell" is 1
                    delay 0.2
                end repeat
            end if
        end tell
        tell text field 1 of scroll area 4 of window 1
            set value of attribute "AXFocused" to true
            set value to "2"
            keystroke return
        end tell
    end tell
end tell

在英语以外的系统语言中,文字字符串Cell可能已本地化。