Home > Net >  How should I optimise my function with Google Apps Script when using simple triggers?
How should I optimise my function with Google Apps Script when using simple triggers?

Time:07-22

I am using Scripts to create a multi- select dropdown using onEdit as the function. The issue I have is that it was initially working perfectly & executing in 0.15s but has now started timing out after 30 seconds. I'm not sure how to adapt this as I don't know what the change is. Any help would be appreciated!

function MultiSelect(e) {
  var oldValue;
  var newValue;
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeCell = ss.getActiveCell();
  if (
    activeCell.getColumn() == 2 &&
    ss.getActiveSheet().getName() == 'Waitlist'
  ) {
    newValue = e.value;
    oldValue = e.oldValue;
    if (!e.value) {
      activeCell.setValue('');
    } else {
      if (!e.oldValue) {
        activeCell.setValue(newValue);
      } else {
        activeCell.setValue(oldValue   ', '   newValue);
      }
    }
  }
}

Script image

CodePudding user response:

Try it this way:

function MultiSelect(e) {
  if (e.range.columnStart == 2 && e.range.getSheet().getName() == 'Waitlist') {
    if (!e.value) {
      e.range.setValue('');
    } else if (!e.oldValue) {
      e.range.setValue(e.value);
    } else {
      e.range.setValue(e.oldValue   ', '   e.value);
    }
  }
}
  • Related