Changing sheet ordering in google sheets based on last change order

Viewed 18

I am trying to create an event calendar in google sheets for an organization spanning many cities. I have created a google form for different cities to enter their events, and those events are automatically brought to google sheets. I have a sheet for future events and sheets for each city.

The future events sheet will shows events in the future without filtering for the city.

The city sheets will show all events that took place or will take place in that city.

I was able to write the formulas for filtering the data for every sheet.

I would love to change the ordering of sheets based on the last time those sheets are changed. For example, I would like Istanbul to take place of New York like the change

from Figure 1

enter image description here

to Figure 2,

enter image description here

when Istanbul enters a new event.

Is there any way I can do that?

1 Answers

Class Spreadsheet has a moveActiveSheet(position) method

If your talking about user edits then perhaps this will work

function onMyEdit(e) {
  const sh = e.range.getSheet();
  const shts = ["Sheet0","Sheet1","Sheet2","Sheet3","Sheet4"];//These are the sheet names involved in the sorting whichever one gets edited moves to the position of the first one.
  const name = sh.getName();
  const idx = shts.indexOf(name);
  if(~idx) {
    let nA = e.source.getSheets().map(sh => sh.getName());
    let f = nA.find(n => n.startsWith("Sheet"));
    let pos = nA.indexOf(f);
    e.source.moveActiveSheet(pos);
  }
}

Demo:

enter image description here

Related