I need a function in Appscript that sorts a column with names by putting a specific name first. The only function I could think of is kind of long,it basically replaces the name in question with the number 1, then sorts the column ascendingly and finally replaces 1 with the name in question. This is the column i wish to sort
Sheet = SpreadsheetApp.getActiveSheet();
var lastRow = Sheet.getLastRow();
function SortbyName()
{
for (var i = 2; i <= lastRow; i++)
{
if (Sheet.getRange(i,1).getValue() == "Name in question")
{Sheet.getRange(i,1).setValue("1");}
}
Sheet.sort(1)
for (var i = 2; i <= lastRow; i++)
{
if (Sheet.getRange(i,1).getValue() == 1)
{Sheet.getRange(i,1).setValue("Name in question");}
}
}
This code works, but is very slow and convoluted so any suggestions would be welcomed.
I need a function in Appscript that sorts a column with names by putting a specific name first. The only function I could think of is kind of long,it basically replaces the name in question with the number 1, then sorts the column ascendingly and finally replaces 1 with the name in question. This is the column i wish to sort
Sheet = SpreadsheetApp.getActiveSheet();
var lastRow = Sheet.getLastRow();
function SortbyName()
{
for (var i = 2; i <= lastRow; i++)
{
if (Sheet.getRange(i,1).getValue() == "Name in question")
{Sheet.getRange(i,1).setValue("1");}
}
Sheet.sort(1)
for (var i = 2; i <= lastRow; i++)
{
if (Sheet.getRange(i,1).getValue() == 1)
{Sheet.getRange(i,1).setValue("Name in question");}
}
}
This code works, but is very slow and convoluted so any suggestions would be welcomed.
Share Improve this question edited Mar 20 at 22:45 Antonio Fernandez Espinosa asked Mar 19 at 16:55 Antonio Fernandez EspinosaAntonio Fernandez Espinosa 11 bronze badge 6 | Show 1 more comment1 Answer
Reset to default 0To prioritize a specific name at the top while sorting the remaining data alphabetically, you can try this code:
function sortByName() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getDataRange();
var values = range.getValues();
var specificNames = "Fritz" // Name that you want to prioritize
// Sorting the values and prioritizing the specified name
values.sort((a, b) => {
if (a[0] === specificNames && b[0] !== specificNames) {
return -1; // If 'a' is the prioritized name and 'b' is not, 'a' comes first
} else if (a[0] !== specificNames && b[0] === specificNames) {
return 1; // If 'b' is the prioritized name and 'a' is not, 'b' comes first
} else {
return a[0].localeCompare(b[0]); // Sorting all values alphabetically
}
});
// Set the sorted values back to the sheet
range.setValues(values);
}
Sample Data:
Input | Output |
---|---|
Steve | Fritz |
Carl | Fritz |
John | Bob |
Fritz | Carl |
Hanz | Hanz |
John | John |
Fritz | John |
Bob | Steve |
References:
sort()
localeCompare()
JavaScript Comparison and Logical Operators
Sheet
is undefined.lastrow
is undefined. How do you knowThis code works
? – TheMaster Commented Mar 19 at 16:55This code works, but is very slow and convoluted
- When you say slow, how slow is it? Could you provide more details regarding the performance issues? – Jats PPG Commented Mar 19 at 18:39