【问题标题】:How to sort date strings in accending order in Google Apps Script Javascript如何在 Google Apps Script Javascript 中按升序对日期字符串进行排序
【发布时间】:2017-06-02 04:36:03
【问题描述】:

我需要在电子表格中对 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:

这是我第三次运行该程序。它工作得很好,除了它在排序时将空/空单元格放在首位。我需要任何 null 到最后,同时保持其余部分按升序排列。谢谢!!!

例子:

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

正确输出 = ["5/3","6/2",""]

incorrectOutput = [" ", "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);


  };

【问题讨论】:

  • 请在问题标题中删除/替换为“Javascript”“Script Java”以避免混淆。
  • 首先,您需要检索单元格的值:将.getValues() 添加到您的sheet.getRange("Ax:Zy") 调用中,这比在遍历范围时获取值更快。所以,如果你对var data = sheet.getRange('AC9:AE53'); 这样做,你可以摆脱dateArry。然后,您应该在开始排序之前查找空单元格。填充单元格后,您可以使用Date.parse() 获取日期序列并对它们进行排序(不要忘记将年份添加到这些日期字符串中)。您能否澄清您的问题,以便我们提供更好的答案?
  • 嗨,Dean,我添加了 .getValues()。我没有使用此代码中的实际日期,因为它最终需要采用纯文本格式才能创建 .csv 文件。所以日期实际上是字符串。年份也没有使用,也不需要,因为所有排序的日期都在一两个星期内。
  • 你应该使用真实的日期对象,一切都会更简单......并在最后将它们转换为字符串。当日期在 12 月或 1 月时,不使用年份值可能是个问题!
  • 所以现在最困难的事情是对每行的 3 个日期进行排序/排序。每行 3 个日期需要单独比较。我添加了一些新代码,但还没有到达任何地方。

标签: javascript arrays datetime google-apps-script google-sheets


【解决方案1】:

你也可以使用sort(sortSpecObj)

对给定范围内的单元格进行排序。按指定的列和顺序对给定范围内的单元格进行排序。

这是文档中的一个 sn-p:

 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() 会更容易排序。

希望这会有所帮助。

【讨论】:

  • 我在帖子中说过我已经知道该怎么做。需要帮助对日期进行水平排序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 2014-09-08
  • 2021-10-03
  • 2021-12-18
  • 2018-07-07
相关资源
最近更新 更多