最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google apps script - Sort a column by putting a specific name first - Stack Overflow

programmeradmin5浏览0评论

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
  • 2 Is this all your code? Sheet is undefined. lastrow is undefined. How do you know This code works? – TheMaster Commented Mar 19 at 16:55
  • 2 In addition, Please do not upload images of code/data/errors. Instead, provide a minimal, reproducible example using Tables or tools like Table to Markdown that can easily be copied and pasted. If necessary, as a last resort, Make an anonymous sample document. – Saddles Commented Mar 19 at 17:09
  • Fot to add the line that defines sheet, already added it. I ran it and it worked, but its a bit slow – Antonio Fernandez Espinosa Commented Mar 19 at 18:30
  • This 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
  • 1 function declaration is spelled wrong – Cooper Commented Mar 19 at 19:34
 |  Show 1 more comment

1 Answer 1

Reset to default 0

To 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

发布评论

评论列表(0)

  1. 暂无评论