Assign properties to an item - maybe using handlers

Hi. I’m working on a script to clean up an Apple Numbers file. It is working but I was thinking it can be better in one point: avoid repeating the same thing for every item. Let me explain:

  1. I have some cells as categories:
    set value of cell “H8” to “Food”
    set value of cell “H9” to “Shopping”
    set value of cell “H10” to “Taxes”
    (go like this for 22 rows)

  2. I have some pre-define colors for them:
    set ColorFood to {65535, 65535, 43690}
    set ColorShopping to {65535, 64250, 52685}
    set ColorTaxes to {65535, 60138, 52942}
    (go like this for 22 rows)

  3. Each one has a color:
    set the background color of range “H8:I8” to ColorFood
    set the background color of range “H9:I9” to ColorShopping
    set the background color of range “H10:I10” to ColorTaxes
    (go like this for 22 rows)

  4. Later I check some cells and if they contains some text, it add a value to the next column and change the color:
    if theText starts with “DEB AUTOR” then
    set value of cell thisRangeCategoria to “Food”
    tell cell thisRangeCategoria
    set the background color to ColorFood
    end tell
    end if
    (go like this for 22 items)

I was trying to create a handler to pass all the values at once, but I got stuck. Is there a way to say: "okay, check “theText”, if it is “Food”, add the color for Food items and say “Food” in the next column.

As I said, it is working as I did, but maybe it can be made in a more smart way.

Any suggestion?

Thank you!

These are the basis of programming.

LOOPs

and CONDITIONAL statements
If. Then. Else

You just need to break your code
Into the proper flow.
Flow charts are a great place to start .

Also no one can help you with your code unless you provide example of what your doing and where it “errors” or may need improvement

1 Like

Just an example i’m not familiar with Excel’s scripting. But you want to do something like this:

set checkCells to range “J8:J30”
set currentRow to 8

repeat with aCheckCell in checkCells

set aCheckCellText to aCheckCell’s value as text

if aCheckCellText starts with “DEB AUTOR” then
set aNewCellLocation to “K” + currentRow
set aNewCell to cell aNewCellLocation
aNewCell’s set value to “Food”
aNewCell’s background color to ColorFood

end if

set currentRow to currentRow + 1

end repeat

Thank you, it is a good idea. I was spreading all the “properties” of my items in different parts of the code, and your suggestion will create more organized blocks. Thank you.