2013-04-07 67 views
4

我正在研究Google Apps腳本電子表格應用程序,我希望程序具有的功能之一是根據來自2個不同列的數據自動排序一系列表單響應。所以,我希望通過數據列16進行排序,然後按列排序1.我可以實現使用該方法在手動此功能: https://drive.googleblog.com/2010/06/tips-tricks-advanced-sorting-rules-in.htmlGoogle Apps腳本其他排序規則

目前我運行Spreadsheet.sort(column, ascending)功能與第一列,但我無法進行排序,因此它將接受第二列作爲附加的排序規則。 Google Apps腳本中是否有方法可用來模擬此功能?

回答

11

見文件: https://developers.google.com/apps-script/reference/spreadsheet/range#sort(Object)

function sortFormResponses() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 

    // change name of sheet to your sheet name 
    var s = ss.getSheetByName("Form Responses"); 
    var lastCol = s.getLastColumn(); 
    var lastRow = s.getLastRow(); 

    // assumes headers in row 1 
    var r = s.getRange(2, 1, lastRow - 1, lastCol); 

    // Note the use of an array 
    r.sort([{ column: 1, ascending: true }, { column: 16, ascending: true}]); 

} 
+0

這是一流的解決方案!我不知道那種方法。感謝您的信息:-) – 2013-04-07 13:50:02

+0

非常感謝,這正是我一直在尋找的。 – 2013-04-07 14:34:52

+0

新文檔包含豐富的新信息和代碼示例,值得一看。感謝Google員工。 – ScampMichael 2013-04-07 14:55:36

0

您可以在數組級別進行排序,只需從工作表中獲取數據到矩陣中,然後選擇要排序的列進行多次排序。

是這樣的:

function test(){ 
sortSheetOnColumn(2,3) 
} 

function sortSheetOnColumn(col1,col2){ 
    var sh = SpreadsheetApp.getActiveSheet(); 
    var data = sh.getDataRange().getValues();// get all data 
    var header = data.shift(); 
    data.sort(function(x,y){ // Note: sort method changes the original array 
// var xp = Number(x[col2-1]);// use these to sort on numeric values 
// var yp = Number(y[col2-1]); 
    var xp = x[col2-1].toLowerCase();// use these for non-numeric values 
    var yp = y[col2-1].toLowerCase(); // I used toLowerCase() for my use case but you can remove it or change it to whatever you need 
    Logger.log(xp+' '+yp); // just to check the sort is OK 
    return xp == yp ? 0 : xp < yp ? -1 : 1;// sort on column col ascending 
    }); 
    data.sort(function(x,y){ // Note: sort method changes the original array 
// var xp = Number(x[col1-1]);// use these to sort on numeric values 
// var yp = Number(y[col1-1]); 
    var xp = x[col1-1].toLowerCase();// use these for non-numeric values 
    var yp = y[col1-1].toLowerCase();// 
    Logger.log(xp+' '+yp); // just to check the sort is OK 
    return xp == yp ? 0 : xp < yp ? -1 : 1;// sort on column col ascending 
    }); 
// and at the end take back the headers 
    data.unshift(header); 
    sh.getDataRange().setValues(data); 
} 

或更好,下面亞當的評論:

function sortSheetOnColumn2(col1, col2) { 
    var sh = SpreadsheetApp.getActiveSheet(); 
    var data = sh.getDataRange().getValues();// get all data 
    var header = data.shift(), x1, y1, x2, y2; 
    col1--; 
    col2--; 
    data.sort(function(x, y) { 
    x1 = x[col1].toLowerCase(); 
    y1 = y[col1].toLowerCase(); 
    x2 = x[col2].toLowerCase(); 
    y2 = y[col2].toLowerCase(); 
    return x1 == y1 ? (x2 == y2 ? 0 : x2 < y2 ? -1 : 1) : x1 < y1 ? -1 : 1; 
    }); 
    data.unshift(header); 
    sh.getDataRange().setValues(data); 
} 

邁克爾的,如果更聰明在Range.sort方法使用建立一個我不知道的答案(至少是其擴展的可能性)。

+0

我不知道這是否會是任何更有效,但我想你也可以用單一功能的數組進行排序:'data.sort(函數(X ,y){return x [0] == y [0]?(x [1] == y [1]?0:x [1] AdamL 2013-04-07 08:18:54

+0

嗨,亞當,有趣!但我這樣做是爲了更清楚地處理排序順序,即首先是哪一列......得到一個邏輯結果似乎相當棘手......(對我的pov來說是「邏輯」的;-) - 在你的方法中訂單是什麼? – 2013-04-07 08:32:44

+0

我的邏輯可能有瑕疵嗶嘰,但我試圖達到與你的相同的順序;即「col1」優先於「col2」。因此,只有當相應的「col1」值相等時,才需要比較「col2」值。再次,我的邏輯中可能會出現漏洞... – AdamL 2013-04-07 08:43:04