Search code examples
google-apps-scriptgoogle-sheetsuniquegoogle-sheets-formulagoogle-sheets-query

Update cells values after UNIQUE formula lists another value


I have a Google Spreadsheet to count my financial expenses. There are several categories e.g. Food. Each time I add a new category, UNIQUE value adds the value. The sheet is for several people so I have several UNIQUE formulas, one for each person.

I have created a script which copies all values from UNIQUE formulas to one column and then make it unique to make sure all people have the same categories.

I wanted to make it automatic, so each time I add a new category, one of the unique lists is updated and the script should be launched. However, there is my problem. onEdit script does not detect changes if it is from UNIQUE formula.

onEdit script does not work.

function onEdit(e) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = e.range;
  var editRange = { // B4:J6
    top : 3,
    bottom : 20,
    left : 22,
    right : 23
  };

  // Exit if we're out of range
  var thisRow = e.range.getRow();
  if (thisRow < editRange.top || thisRow > editRange.bottom) return;

  var thisCol = e.range.getColumn();
  if (thisCol < editRange.left || thisCol > editRange.right) return;

  sheet.getRange('X3:X1000').clearContent();

  var base = 3;
  var counter = 3;
  var counterResult = 3;

  while(sheet.getRange(counter, 22).getValue().length != 0){
    var getValue = sheet.getRange(counter++, 22).getValue();
    sheet.getRange(counterResult++, 24).setValue(getValue);
  }

  counter = base;

  while(sheet.getRange(counter, 23).getValue().length != 0){
    var getValue = sheet.getRange(counter++, 23).getValue();
    sheet.getRange(counterResult++, 24).setValue(getValue);
  }
}

The function which detects changes in UNIQUE formula.


Solution

  • Wow! I did not know I can add 2 parameters with {}. This solves my problem! Thank you!

    =UNIQUE({A:A;B:B})
    

    and to kill empty unique:

    =QUERY(UNIQUE({A:A;B:B}), "where Col1 is not null", 0)