Numbers 更改if语句中的值

Numbers 更改if语句中的值,numbers,applescript,Numbers,Applescript,我试图遍历一列数字,如果单元格有特定的背景色,则更改单元格的背景色 repeat with i from 11 to the count of cells by 6 if background color of cell i is {17990, 47031, 42919} then set background color of cell i to {65535, 0, 0} end if end repeat

我试图遍历一列数字,如果单元格有特定的背景色,则更改单元格的背景色

repeat with i from 11 to the count of cells by 6

            if background color of cell i is {17990, 47031, 42919} then
                set background color of cell i to {65535, 0, 0}
            end if

end repeat

不幸的是,这没有任何作用。脚本只是停止,没有错误。救命啊

这是我开始使用的表格,它将一个单元格的背景染成了洋红色,即{65535,065535}

然后我运行了以下代码:

use NumbersApp : application "Numbers"

property document : a reference to document 1 of NumbersApp
property sheet : a reference to active sheet of my document
property table : a reference to table 1 of my sheet


repeat with c in (a reference to every cell of my table)
    if c's background color = missing value then ¬
        set c's background color to {65535, 65535, 0}
    
    if c's background color = {65535, 0, 65535} then ¬
        set c's background color to {65535, 65535, 65535}
end repeat
我原以为大部分细胞会变成黄色,我的品红色细胞会变成白色:

我的洋红细胞看起来还是太洋红了。所以我决定检查一下它到底有多红:

return the background color of cell "C7" of my table
    --> {64587, 609, 65480}
嗯,这不是我设定的,但它是相当的洋红色,虽然我现在明白为什么它没有变成白色

接下来,我决定检查其中一个黄色单元格的背景颜色,您刚才看到我以编程方式将其转换为一种非常特殊的黄色:

return the background color of cell "D10" in my table
    --> {65534, 65531, 2689}
同样,它是黄色的,但不是我告诉它的黄色

最后,我使用刚刚返回的颜色值尝试瞄准这些单元格并将其变黑:

set the background color of every cell in my table ¬
    whose background color is {65534, 65531, 2689} ¬
    to {0, 0, 0}
废话。它们仍然是非常阳光般的黄色

结论
AppleScript中的Bug。我已经向苹果公司提交了一份错误报告。我建议你也这样做。

这是我开始的表格,把一个单元格的背景染成了洋红色,即{65535,065535}

然后我运行了以下代码:

use NumbersApp : application "Numbers"

property document : a reference to document 1 of NumbersApp
property sheet : a reference to active sheet of my document
property table : a reference to table 1 of my sheet


repeat with c in (a reference to every cell of my table)
    if c's background color = missing value then ¬
        set c's background color to {65535, 65535, 0}
    
    if c's background color = {65535, 0, 65535} then ¬
        set c's background color to {65535, 65535, 65535}
end repeat
我原以为大部分细胞会变成黄色,我的品红色细胞会变成白色:

我的洋红细胞看起来还是太洋红了。所以我决定检查一下它到底有多红:

return the background color of cell "C7" of my table
    --> {64587, 609, 65480}
嗯,这不是我设定的,但它是相当的洋红色,虽然我现在明白为什么它没有变成白色

接下来,我决定检查其中一个黄色单元格的背景颜色,您刚才看到我以编程方式将其转换为一种非常特殊的黄色:

return the background color of cell "D10" in my table
    --> {65534, 65531, 2689}
同样,它是黄色的,但不是我告诉它的黄色

最后,我使用刚刚返回的颜色值尝试瞄准这些单元格并将其变黑:

set the background color of every cell in my table ¬
    whose background color is {65534, 65531, 2689} ¬
    to {0, 0, 0}
废话。它们仍然是非常阳光般的黄色

结论
AppleScript中的Bug。我已经向苹果公司提交了一份错误报告。我建议您也这样做。

数字应用程序中似乎存在错误,无法正确报告颜色。我将A列和B列的背景色设置为您选择的值{179904703142919},但是当我要求脚本返回颜色时,它返回了值{179904703042919}。因此,我让脚本检查这两个值,并相应地采取行动

我添加了一个弹出对话框,让您可以选择要更改单元格颜色的列

tell application "Numbers"
    set ifColor to {17990, 47031, 42919}
    set ifColor2 to {17990, 47030, 42919}
    set changeToColor to {65535, 0, 0}
    tell its document 1
        set theColumns to name of columns of table "Table 1" of active sheet
        set chosenColumn to item 1 of (choose from list theColumns with title "Choose The Column" with prompt "Choose The Column")
        set cellCount to count of cells of column chosenColumn of table "Table 1" of active sheet
        repeat with i from 11 to cellCount by 6
            set thisCell to cell ((chosenColumn & i) as string) of table "Table 1" of active sheet
            if background color of thisCell is ifColor or background color of thisCell is ifColor2 then
                set background color of thisCell to changeToColor
            end if
        end repeat
    end tell
end tell

数字应用程序中似乎存在错误,无法正确报告颜色。我将A列和B列的背景色设置为您选择的值{179904703142919},但是当我要求脚本返回颜色时,它返回了值{179904703042919}。因此,我让脚本检查这两个值,并相应地采取行动

我添加了一个弹出对话框,让您可以选择要更改单元格颜色的列

tell application "Numbers"
    set ifColor to {17990, 47031, 42919}
    set ifColor2 to {17990, 47030, 42919}
    set changeToColor to {65535, 0, 0}
    tell its document 1
        set theColumns to name of columns of table "Table 1" of active sheet
        set chosenColumn to item 1 of (choose from list theColumns with title "Choose The Column" with prompt "Choose The Column")
        set cellCount to count of cells of column chosenColumn of table "Table 1" of active sheet
        repeat with i from 11 to cellCount by 6
            set thisCell to cell ((chosenColumn & i) as string) of table "Table 1" of active sheet
            if background color of thisCell is ifColor or background color of thisCell is ifColor2 then
                set background color of thisCell to changeToColor
            end if
        end repeat
    end tell
end tell
@wch1zpink:{179904703142919}->{179904703042919}

看起来像是将整数转换为浮点数并再次转换时引入的舍入错误。AppleScript可以追溯到QuickDraw的时代,它将RGB值表示为UInt16,而数字是Cocoa应用程序,Cocoa的NSColor使用CGFloat。这是不可避免的,因为这是CPU进行数学运算的基本限制,例如0.7*0.7=0.49->false

解决方案是检查数字是否在可接受的误差范围内相等:

on areRealsEqual(n1, n2, toleranceMargin)
  return n1 > n2 - toleranceMargin and n1 < n2 + toleranceMargin
end areRealsEqual

on areColorsEqual(c1, c2)
  repeat with i from 1 to length of c1
    if not areRealsEqual(item i of c1, item i of c2, 5) then return false
  end repeat
  return true
end areColorsEqual

set expectedColor to {17990, 47031, 42919}
set foundColor to {17990, 47030, 42919}

areColorsEqual(expectedColor, foundColor)
--> true
@wch1zpink:{179904703142919}->{179904703042919}

看起来像是将整数转换为浮点数并再次转换时引入的舍入错误。AppleScript可以追溯到QuickDraw的时代,它将RGB值表示为UInt16,而数字是Cocoa应用程序,Cocoa的NSColor使用CGFloat。这是不可避免的,因为这是CPU进行数学运算的基本限制,例如0.7*0.7=0.49->false

解决方案是检查数字是否在可接受的误差范围内相等:

on areRealsEqual(n1, n2, toleranceMargin)
  return n1 > n2 - toleranceMargin and n1 < n2 + toleranceMargin
end areRealsEqual

on areColorsEqual(c1, c2)
  repeat with i from 1 to length of c1
    if not areRealsEqual(item i of c1, item i of c2, 5) then return false
  end repeat
  return true
end areColorsEqual

set expectedColor to {17990, 47031, 42919}
set foundColor to {17990, 47030, 42919}

areColorsEqual(expectedColor, foundColor)
--> true

使用调试器验证它是否进入循环,如果进入循环,则背景颜色与您期望的相同。使用调试器验证它是否进入循环,如果进入循环,背景色是您所期望的。与实际背景色值{179904703142919}->{179904703042919}的返回结果相比,我不确定您的解决方案是否涵盖了响应用户定义的背景色值的所有基础因为如果您要求脚本返回更改为{65535,0,0}的单元格的值的颜色,在我看来,如果用户选择的背景颜色与最初选择的不同,则返回的值为{64633,466,1797},这些数字可能相差很远,而一个一个的错误可能是固有的FP不精确的结果,更大
偏差明确指出了用户脚本、应用程序和/或底层操作系统框架中的实现缺陷。假设是后两者之一,你可以在include number和AppleScript文件中提交雷达报告,清楚地显示错误,但最好不要屏住呼吸等待修复,因为AppleScript在几年前进入了维护模式,因此除安全漏洞之外的任何其他问题都将处于非常低的优先级。与实际背景颜色值的返回结果相比,我不确定您的解决方案是否涵盖了响应用户定义的背景颜色值的所有基础{179904703142919}->{179904703042919}因为如果您要求脚本返回更改为{65535,0,0}的单元格的值的颜色,在我这边,返回的值是{646334661797}如果用户选择与他最初选择的背景颜色不同的背景颜色,则数字可能会相差很远,而“一对一”错误可能是由于固有的FP不精确性造成的,较大的偏差肯定指向用户脚本、应用程序和/或底层操作系统框架中的实现错误。假设它处于启用状态在后两种情况中,你可以在include number和AppleScript文件中提交雷达报告,清楚地说明错误,但最好不要屏息等待修复,因为AppleScript在几年前进入了维护模式,所以除了安全漏洞以外的任何东西都将是非常低优先级的。非常感谢-我还尝试让AppleScript设置C颜色并返回它们,收到了相同的行为。非常不幸,但我想我必须找到另一种方法来选择我的数据,而不是颜色。无论如何,这不是最好的主意,我猜非常感谢-我还尝试让applescript设置颜色并返回它们,收到了相同的行为。非常不幸,但我想我不得不这样做找到另一种方法来选择我的数据而不是颜色。我想这不是最好的主意