在if语句中更改值

时间:2018-10-20 11:11:02

标签: 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

不幸的是,这没有任何作用。该脚本会立即停止而不会出现错误。请帮忙!

3 个答案:

答案 0 :(得分:2)

Numbers应用程序中似乎存在一个错误,该错误无法正确报告颜色。我将A和B列的背景颜色设置为您选择的值{17990、47031、42919},但是当我要求脚本返回颜色时,它返回了值{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

enter image description here

答案 1 :(得分:1)

这是我开始使用的桌子,上面涂了一个单元格洋红色的背景,即{65535,0,65535}

Table 1 in Numbers on macOS before running code

然后我运行了这段代码:

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

我期望大多数细胞会变成黄色,而洋红色细胞会变成白色:

Table 1 in Numbers on macOS after running code

嗯...

我的品红色单元格仍然看起来太品红色。所以我决定检查一下它到底是洋红色的:

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}

Zilch。它们仍然非常阳光明媚。

结论

AppleScript中的错误。我已经向Apple提交了错误报告。我建议你也这样做。

答案 2 :(得分:1)

@ 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