Custom sorting google scripts

2.2k Views Asked by At

I would like to ask if it is possible to sort the data sheet with one row but from a modified sort. The sorting should be

  1. Empty cells
  2. Accepted
  3. In Progress
  4. For Follow up

This is the custom order we need. These options are from a dropdown on the spreadsheet. How can I do this on google scripts for google spreadsheet? Thanks in advance

EDIT: Here is my code below:

var s = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var currentRow = e.range.getRow();
var archiverow = e.source.getSheetByName("Archive").getLastRow();
Logger.log(archiverow)
Logger.log(currentRow)
var new_id = archiverow + currentRow
var id = "AR-"+new_id;
Logger.log(id);

var name = e.namedValues["Your Name"];
SendAdminSlack(e, name);
var duedate = e.namedValues["Due Date"].toString();
s.getRange(currentRow, 13).setValue(duedate);
duedate = Utilities.formatDate(new Date(duedate), "GMT+8", "MMMMM dd, yyyy");

var date_submitted = s.getRange(currentRow, 2).getValue().toString().split(' ');
date_submitted = date_submitted[1] + " " +  date_submitted[2] + " " + date_submitted[3];
date_submitted = Utilities.formatDate(new Date(date_submitted), "GMT+8", "MMMMM dd, yyyy");

var sum = s.getRange(currentRow, 3).getValue();
var description = e.namedValues["Request Description"];

var ovcalendar = CalendarApp.getCalendarById(calendarId);

var title = '[ADMIN REQUEST] ' + name;

var caldesc = "Summary: " + sum + "\n";
caldesc += "Due Date: " + duedate + "\n\n";
caldesc += "Description: " + description;
Logger.log(caldesc);

var event = ovcalendar.createAllDayEvent(title, new Date(duedate), {description: caldesc});
Logger.log(date_submitted.toString())
s.getRange(currentRow, 14).setValue(event.getId().toString())
s.getRange(currentRow, 1).setValue(id);

var subj = "Admin Request for "+name;
var message = "Request Id: "+id+"<br>";
message += "Due Date: "+duedate+"<br>";
message += "Request Summary: "+sum+"<br><br>";
message += "Request Description: "+description;

MailApp.sendEmail({
  to: kath,
  subject: subj,
  htmlBody: message,
  noReply: true
});

s.sort(9); // I NEED TO CUSTOMIZE THIS SORT OR MAKE A NEW ONE

I need to sort it specifically on the order above the code.

1

There are 1 best solutions below

3
On

generically sorting an array by specific value:

var cond = function(term) {
    var score, conditions = ["For Follow Up", "In Progress", "Accepted"];
    if (term == "") {
        score = conditions.length;
    } else {
        score = conditions.indexOf(term);
    }

    return score;
}


// perhaps it's this simple
var values = s.getValues();

values = values.sort(function(a, b){

var column = 0; // or wherever your value is

    return (cond(a[column]) < cond(b[column])) ? -1 : (cond(a[column]) > cond(b[column])) ? 1 : 0;

});

s.setValues(values); // maybe, your need maybe more complicated.

All typed on a phone so consider it pseudocode in the main