Привет. Думай как программист по шагам. У меня есть код:
```javascript
function sortByTime() {
  CleanSh(); 

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = sheet.getRange("A2:B" + sheet.getLastRow()).getValues();

  var outputD = [];
  var outputE = [];
  var outputF = [];
  var outputH = [];
  var timeSlots = [[], [], []]; 

  data.forEach(function(row) {
    var name = row[0];
    var timeRange = row[1];

    if (!timeRange || timeRange.toString().startsWith('#')) return; 

    var timeRanges = timeRange.split('\n');
    

    timeRanges.forEach(function(singleTimeRange) {
      console.log("singleTimeRange", singleTimeRange)
      var singleTimeRange = singleTimeRange.slice(0,13)
      var timeMatch = singleTimeRange.match(/(\d{2}:\d{2})\s*-\s*(\d{2}:\d{2})/);
      console.log(timeMatch, "timeMatch")

      if (timeMatch) {
        var startTimeStr = timeMatch[1];
        var endTimeStr = timeMatch[2];
        
        var startTime = new Date("1970-01-01T" + startTimeStr + ":00");
        var endTime = new Date("1970-01-01T" + endTimeStr + ":00");

        var placed = false;

        var preferredColumn = getPreferredColumn(name, timeSlots, startTime, endTime);

        
        if (preferredColumn !== null) {
          if (preferredColumn === 0) {
            outputD.push(name + " " + singleTimeRange);
            //console.log("singleTimeRange", singleTimeRange)
          } else if (preferredColumn === 1) {
            outputE.push(name + " " + singleTimeRange);
          } else {
            outputF.push(name + " " + singleTimeRange);
          }
          timeSlots[preferredColumn].push(singleTimeRange);
          placed = true;
        }

        if (!placed) {
          var availableColumns = [];
          for (var i = 0; i < 3; i++) {
            var isAvailable = isTimeSlotAvailable(timeSlots[i], startTime, endTime);

            if (isAvailable) {
              availableColumns.push(i);
            }
          }

          if (availableColumns.length > 0) {
            var randomColumn = availableColumns[Math.floor(Math.random() * availableColumns.length)];
            if (randomColumn === 0) {
              outputD.push(name + " " + singleTimeRange);
            } else if (randomColumn === 1) {
              outputE.push(name + " " + singleTimeRange);
            } else {
              outputF.push(name + " " + singleTimeRange);
            }
            timeSlots[randomColumn].push(singleTimeRange);
            placed = true;
          }
        }

        if (!placed) {
          outputH.push(name + " " + singleTimeRange);
        }
      } else {
        outputH.push(name + " " + singleTimeRange + " #Неверный формат времени");
      }
    });
  });

  if (outputD.length > 0) {
    var outputRangeD = sheet.getRange(2, 4, outputD.length, 1);
    outputRangeD.setValues(outputD.map(function(item) { return [item]; }));
  }

  if (outputE.length > 0) {
    var outputRangeE = sheet.getRange(2, 5, outputE.length, 1);
    outputRangeE.setValues(outputE.map(function(item) { return [item]; }));
  }

  if (outputF.length > 0) {
    var outputRangeF = sheet.getRange(2, 6, outputF.length, 1);
    outputRangeF.setValues(outputF.map(function(item) { return [item]; }));
  }

  if (outputH.length > 0) {
    var outputRangeH = sheet.getRange(2, 8, outputH.length, 1);
    outputRangeH.setValues(outputH.map(function(item) { return [item]; }));

    for (var k = 0; k < outputH.length; k++) {
      sheet.getRange(2 + k, 8).setBackground("red");
    }
  }
}
```
Этот код не правильно выполняет функцию сортировки. Нужно Сначало отсортировать все данные а потом уже размещаться в таблицу