自定义排序谷歌脚本

时间:2015-06-22 02:19:53

标签: sorting google-apps-script google-sheets google-form

我想问一下,是否可以对数据表进行排序,但需要修改排序。排序应该是

  1. 清空单元格
  2. 接受
  3. 正在进行中
  4. For Follow up
  5. 这是我们需要的自定义订单。这些选项来自电子表格的下拉列表。如何在谷歌电子表格的谷歌脚本上执行此操作?提前致谢

    编辑: 以下是我的代码:

    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
    

    我需要在代码上方的顺序上对其进行排序。

1 个答案:

答案 0 :(得分:1)

通过特定值对数组进行一般排序:

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.

所有在手机上打字,所以在主

中考虑伪代码