Check if background value exists for JXA/Applescript for Numbers

63 Views Asked by At

I'm using JavasScript to write the script for Numbers automation. I believe its called JXA in this context? Based on: https://leancrew.com/all-this/2022/05/javascript-vs-applescript/

Anyway here's a function that colorize the row based on a condition:

function colorize(row) {
  const blue = [22085.295, 49609.995, 65535]

  const rs = row.cells[2].value()?.indexOf('PRUDENTIAL')

  if (foundAndDefined(rs)) {
    console.log(row.backgroundColor);
    row.backgroundColor = blue
  }
}

However, I don't want to override the color if a color is already defined.

But trying to console.log the backgroundColor throws Error -1700: Can't convert types.

How can we check if there's any existing value or console.log the value if any?

1

There are 1 best solutions below

0
hackerl33t On

Ok figured it out, simply call backgroundColor as a function like backgroundColor().

Full example:

function colorize(row) {
  const blue = [22085.295, 49609.995, 65535]

  const rs = row.cells[2].value()?.indexOf('PRUDENTIAL')

  if (foundAndDefined(rs)) {
    if (isNull(row.backgroundColor())) row.backgroundColor = blue
  }
}

Where isNull is a custom function not shown