Skip to content Skip to sidebar Skip to footer

Custom Google Sheets Function To Add An Item To A Comma Delimited List

I have comma separated lists in a Google Sheet, like 'apple,banana'. I need a function to add elements to this list, like so =SETADD('apple,banana','carrot'), which should return t

Solution 1:

Unfortunately, in the current stage, Array.includes() cannot be used at Google Apps Script, yet. So as a workaround, how about this modification?

From:

if (!array.includes(item)) {

To:

if (array.indexOf(item) === -1) {

References:

Solution 2:

Your problem is that you are running your code in an interpreter which doesn't support include. Try to use es5 code or switch to a node version which supports include (according to node.green its v6.4+).

I would suggest to change your code:

functionSETADD(list, item) {
  vararray = list.split(',');
  if (list.indexOf(','+item+',') > -1 || list.startsWith(item+',') || list.endsWith(','+item)) {
    returnlist;
  }
  else {
    returnlist + ',' + item;
  }
}

Post a Comment for "Custom Google Sheets Function To Add An Item To A Comma Delimited List"