2017-06-02 30 views
0

我有3列日期需要在電子表格中排序。如何按照Google Apps腳本中的附加順序對日期字符串進行排序Javascript

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

例如「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); 




     }; 
    }; 

更新的代碼:

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

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:

這裏是我的方案的第3道。除了在排序時將空/空單元放在第一位之外,它的功能很好最後我需要任何空的去,而其餘的按照升序排列。謝謝!!!

實施例:

inputArray = [ 「5/3」, 「」, 「6/2」]

correctOutput = [ 「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); 


    }; 
+0

請在問題標題中刪除/替換爲「Javascript」「Script Java」,以避免讓人困惑。 – Pyromonk

+1

首先,您需要檢索單元格的值:將'.getValues()'添加到'sheet.getRange(「Ax:Zy」)'調用中,這比在迭代過程中抓取值更快範圍。所以,如果你這樣做'var data = sheet。getRange('AC9:AE53');''你可以擺脫'dateArry'。在開始排序之前,您應該先查找空單元格。一旦你填充了單元格,那麼你可以使用'Date.parse()'來獲取一個日期序列並對它們進行排序(不要忘記給這些日期字符串添加年份)。你能否澄清你的問題,以便我們可以給出更好的答案? –

+0

嗨Dean,我添加了.getValues()。我沒有在這段代碼中使用實際的日期,因爲它最終需要用純文本格式來創建.csv文件。所以日期實際上是字符串。年份也不會被使用,也不需要,因爲所有排序的日期將在一週或兩週內進行。 – Clayten

回答

0

你也可以使用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()將使它更容易進行排序。

希望這會有所幫助。

+0

我在我的帖子中說過,我知道如何做到這一點。需要幫助水平排序日期。 – Clayten

相關問題