改变 if 语句中的值

Changing value in if statement

我正在尝试遍历数字列并更改单元格的背景颜色(如果单元格具有特定背景颜色)。

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

不幸的是,这没有任何作用。脚本只是停止而没有错误。请帮忙!

这是我开始的 table,将一个单元格的背景着色为洋红色,即 {65535,0,65535}

然后我运行这个代码:

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 中的错误。我已经向 Apple 提交了错误报告。我建议你也这样做。

Numbers 应用程序似乎存在错误,无法正确报告颜色。我将 A 列和 B 列的背景颜色设置为您选择的值 {17990, 47031, 42919},但是当我要求脚本 return 颜色时,它 returned 值 {17990, 47030 , 42919}。因此,我让脚本检查这两个值并采取相应的行动。

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

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: {17990, 47031, 42919} --> {17990, 47030, 42919}

看起来像是在将整数转换为浮点数并再次返回时引入了舍入错误。 (AppleScript 可以追溯到 QuickDraw 时代,它将 RGB 值表示为 UInt16,而 Numbers 是一个 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