positive.bunny
positive.bunny

Reputation: 21

How can I add get the value of a cell containing an emoji in Google Appscript?

In appscript, when I check my logs, it tells me that in this case P2 is: Jan 1, 2023, 2:11:20 PM Info 👟

This part of the appscript is meant to add a note on the cell with any occurance of the emoji with the text "Move"

function onEdit(e) {
  
   var notes = {
    '👟': 'Move',
    'option 2': 'Easy Slow-Cooker Chipotle Chicken Chili',
    'option 3': 'Easy 30-Minute Swiss Pan Burgers With Rosemary-Mushroom Sauce',
    'option 4': 'Black Bean Edamame Burger',
    'option 5': 'Sriracha-Cashew Chicken Fried Rice',
    'option 6': 'Shrimp Fried Quinoa',
    'option 7': 'Chocolate Chip Brownie Cookies',
    'option 8': 'Dark Chocolate Chip And Sea Salt Cookies',
    'option 9': 'Easy 30-Minute Homemade Chicken Noodle Soup'

  }
  if (notes[e.value]) {
    return e.range.setNote(notes[e.value]);
  } 
}

it is working for the texts (option 2) for example. But not for the emojis. I've also tried picking up the emojis using stringFromCodePoint and by using the =CHAR reference to no avail.

[by the way, the code I'm using I found online- thank you to whoever shared it!]

Do you know of any ways for me to get the value of a cell with an emoji please?

Upvotes: 0

Views: 117

Answers (1)

doubleunary
doubleunary

Reputation: 19040

The code you show appears to work fine when you manually enter 👟 in a cell — it will set the cell note to Move. It does not support copy and paste of cells, and does not attempt to find keys among other text, so it will do nothing when you paste text that contains 👟 in a cell. This has nothing to do with whether the keys contain emojis.

To make the function insert a note when the text entered or pasted in the cell contains one of the keys, use Range.getDisplayValue() and String.includes(), like this:

/**
* Simple trigger that runs each time the user hand edits the spreadsheet.
*
* @param {Object} e The onEdit() event object.
*/
function onEdit(e) {
  const notes = {
    '👟': 'Move',
    'option 2': 'Easy Slow-Cooker Chipotle Chicken Chili',
    'option 3': 'Easy 30-Minute Swiss Pan Burgers With Rosemary-Mushroom Sauce',
    'option 4': 'Black Bean Edamame Burger',
    'option 5': 'Sriracha-Cashew Chicken Fried Rice',
    'option 6': 'Shrimp Fried Quinoa',
    'option 7': 'Chocolate Chip Brownie Cookies',
    'option 8': 'Dark Chocolate Chip And Sea Salt Cookies',
    'option 9': 'Easy 30-Minute Homemade Chicken Noodle Soup',
  };
  const value = e.value || e.range.getDisplayValue();
  Object.keys(notes).some(key => {
    if (value.includes(key)) {
      e.range.setNote(notes[key]);
      return true;
    }
  });
}

Upvotes: 2

Related Questions