簡體   English   中英

如何在 Google Apps Script Javascript 中按升序對日期字符串進行排序

[英]How to sort date strings in accending order in Google Apps Script Javascript

我有 3 列需要在電子表格中排序的日期。

首先我需要水平排序。 日期采用字符串格式。

例如“5/3”、“5/20”、“6/3”或有時是空白。

如果某些單元格為空白,我需要將所有內容移到最左側的列中。

其次,我需要按日期對行進行排序。 range.sort 很好。

這是我到目前為止所擁有的。

function sortDate() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Loads");
 var range = sheet.getRange("C9:BA53"); 


  //Sorts Horizontally

    //getValues gives a 2D array. 
    var data = sheet.getRange('AC9:AE53');



    //This for loop with loop through each row
    for(var i=0; i<data.length; i++) 
    {

    var dateArry = [data[i][0],data[i][1],data[i][2]];
          //This for loop with loop through each column
         //for (var j = 0; j < data[i].length ; j ++){
          //This assumes Column AC has the dates you are comparing aganist

         //dateElem = date.split('/');

         //dateElem[1] = Number(dateElem[1]) + 1;

         //newDate = dateElem.join('/');

         var sortDates = dateArry.sort();

         sheet.getRange("AC"+i+"AE"+i).setValues(sortDates);




         };
    };

更新代碼:

這是我所做的更新。 最難的部分是對每一行日期進行排序。 我添加了第二個 for 循環來分隔每組日期。

function sortDate() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Loads");
 //var range = sheet.getRange("C9:BA53"); 


  //Sorts Horizontally

    //getValues gives a 2D array. 
    var data = sheet.getRange('AC1:AE53').getValues();



    //This for loop with loop through each row
    for(var i=0; i<data.length; i++) {
      for (var j = 8; j < data[i].length ; j ++){

    var dateArry = [data[i][0],data[i][1],data[i][2]];

????????? 如何對每行的 3 個日期進行排序?????????????

         var sortDates = dateArry.sort();

         //sheet.getRange("AC"+i+":AE"+i).setValues(sortDates);
         sheet.getRange("AC"+i).setValue(sortDates[0]);
         sheet.getRange("AD"+i).setValue(sortDates[1]);
         sheet.getRange("AE"+i).setValue(sortDates[2]);

         };
    };

    };

更新代碼 2:

這是我第三次運行該程序。 除了在排序時將空/空單元格放在首位之外,它效果很好。 我需要在最后保留任何空值,同時保持其余部分按升序排列。 謝謝!!!

例子:

inputArray = ["5/3", " ","6/2"]

正確輸出 = ["5/3","6/2",""]

不正確的輸出 = [" ", "5/3","6/2"] 這就是它現在正在做的事情。

第三組代碼:

 function sortDate2() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheetByName("Loads");
 //var range = sheet.getRange("C9:BA53"); 
 //Trying to fix improper sorting. works great for alphabetic sorting


  //Sorts Horizontally

    //getValues gives a 2D array. 
    var data = sheet.getRange('AC9:AE53').getValues();



    //This for loop with loop through each row
    for(var i=0; i<data.length; i++) {


    for (var j = 0; j < data[i].length ; j ++){ 

    var dateArry = [data[i][0],data[i][1],data[i][2]];





         //var sortDates = dateArry.sort(function(a, b){return a-b});
         var sortedDates = dateArry.sort(function (a, b) {
    // '01/03/2014'.split('/')
    // gives ["01", "03", "2014"]
    a = a.split('/');
    b = b.split('/');
    return a[1] - b[1] || a[0] - b[0] || (a===null)-(b===null) ;
});






         data[i][0] = sortedDates[0];
         data[i][1] = sortedDates[1];
         data[i][2] = sortedDates[2];


        };

         };

         sheet.getRange('AC9:AE53').setValues(data);


  };

您還可以使用sort(sortSpecObj)

對給定范圍內的單元格進行排序。 按指定的列和順序對給定范圍內的單元格進行排序。

這是文檔中的一個片段:

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 var range = sheet.getRange("A1:C7");

 // Sorts by the values in the first column (A)
 range.sort(1);

 // Sorts by the values in the second column (B)
 range.sort(2);

 // Sorts descending by column B
 range.sort({column: 2, ascending: false});

 // Sorts descending by column B, then ascending by column A
 // Note the use of an array
 range.sort([{column: 2, ascending: false}, {column: 1, ascending: true}]);

 // For rows that are sorted in ascending order, the "ascending" parameter is
 // optional, and just an integer with the column can be used instead. Note that
 // in general, keeping the sort specification consistent results in more readable
 // code. We could have expressed the earlier sort as:
 range.sort([{column: 2, ascending: false}, 1]);

 // Alternatively, if we wanted all columns to be in ascending order, we would use
 // the following (this would make column 2 ascending)
 range.sort([2, 1]);
 // ... which is equivalent to
 range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}]);

我也同意@Dean,使用Date.parse()會使排序更容易。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM