Home > OS >  Conditionally set text in Google AppSheet
Conditionally set text in Google AppSheet

Time:11-16

I am evaluating Google AppSheet. I have a Google Sheet with the following structure:

| items                                           |
| date       | time | category | name             |
|------------|------|----------|------------------|
|            |      | sports   | leather football |
|            |      | sports   | basketball       |
| 11-11-2021 | 9:00 | sports   | tennis ball      |

I'm working to display this sheet as a card view with a list layout. The title should be the date/time. The subtitle should be the name. My challenge, as shown in the table above, is that sometimes, I do not have a date or time. In those scenarios, I would like to render the title as "Unknown". Basically, the psuedocode below shows what I want to do:

if (date exists) {
  title = 'Arriving on '   date;
} else {
  title = 'Unknown';
}

I do not see a way to do this in a Google AppSheet though. I have to believe I'm missing something. I assumed I would be able to do it via a Format Rule. However, it doesn't seem like I can specify conditional text. I can only set things like the color, font, etc. But, I do not see a way to add something like the conditional I showed above.

How can I conditionally set text in a Google AppSheet?

CodePudding user response:

Solution:

If you get the value of a cell and it's a valid date, it should return a Date object. So you can use this condition to validate the object type:

var date = range.getValue();

if (date.constructor.toString().indexOf("Date") > -1) {
  title = 'Arriving on '   date;
} else {
  title = 'Unknown';
}

Sample check:

enter image description here

Reference:

Check if object type is Date

  • Related