如何使用applescript从数字中删除小数点

时间:2013-11-04 09:01:00

标签: automation applescript applescript-excel

我正在尝试使用applescript访问excel单元格,并且单元格编号一直在变化,当我将单元格值设置为例如20时,它将采用20.0,这会导致错误。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

我发现了一个符合您需求的脚本(来自http://www.macosxautomation.com/applescript/sbrt/sbrt-02.html),所以您可能想尝试一下:

my round_truncate(20.0, 0)

on round_truncate(this_number, decimal_places)
    if decimal_places is 0 then
        set this_number to this_number + 0.5
        return number_to_text(this_number div 1)
    end if

    set the rounding_value to "5"
    repeat decimal_places times
        set the rounding_value to "0" & the rounding_value
    end repeat
    set the rounding_value to ("." & the rounding_value) as number

    set this_number to this_number + rounding_value

    set the mod_value to "1"
    repeat decimal_places - 1 times
        set the mod_value to "0" & the mod_value
    end repeat
    set the mod_value to ("." & the mod_value) as number

    set second_part to (this_number mod 1) div the mod_value
    if the length of (the second_part as text) is less than the decimal_places then
        repeat decimal_places - (the length of (the second_part as text)) times
            set second_part to ("0" & second_part) as string
        end repeat
    end if

    set first_part to this_number div 1
    set first_part to number_to_text(first_part)
    set this_number to (first_part & "." & second_part)

    return this_number
end round_truncate

on number_to_text(this_number)
    set this_number to this_number as string
    if this_number contains "E+" then
        set x to the offset of "." in this_number
        set y to the offset of "+" in this_number
        set z to the offset of "E" in this_number
        set the decimal_adjust to characters (y - (length of this_number)) thru -1 of this_number as string as number
        if x is not 0 then
            set the first_part to characters 1 thru (x - 1) of this_number as string
        else
            set the first_part to ""
        end if
        set the second_part to characters (x + 1) thru (z - 1) of this_number as string
        set the converted_number to the first_part
        repeat with i from 1 to the decimal_adjust
            try
                set the converted_number to the converted_number & character i of the second_part
            on error
                set the converted_number to the converted_number & "0"
            end try
        end repeat
        return the converted_number
    else
        return this_number
    end if
end number_to_text

答案 1 :(得分:0)

我的第一个建议是你应该举一些例子来接近你需要的答案。 你怎么得到这个“20.0”?这看起来像是Excel的自动化,而不是Applescript。 因此,只要您使用Applescript,就可以调用

set myLineNumber to 20
get value of cell (A & myLineNumber)

现在,如果您从Excel获得“20”值,那么另一件事是因为Excel可能会因数字值而变得非常讨厌。 我这里没有我的Mac,但尝试像

这样的东西
set myLineNumber to valueFromExcel mod 1 -- "mod" is for modulo

应该做你需要的。

您还可以使用“强制转换”,这意味着将“20”等文本值转换为20之类的数字。 对于你的情况,这将是:

set myLineNumber to (valueFromExcel as integer) -- valueFromExcel being "20" or 20.0 for instance