我有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);
};
請在問題標題中刪除/替換爲「Javascript」「Script Java」,以避免讓人困惑。 – Pyromonk
首先,您需要檢索單元格的值:將'.getValues()'添加到'sheet.getRange(「Ax:Zy」)'調用中,這比在迭代過程中抓取值更快範圍。所以,如果你這樣做'var data = sheet。getRange('AC9:AE53');''你可以擺脫'dateArry'。在開始排序之前,您應該先查找空單元格。一旦你填充了單元格,那麼你可以使用'Date.parse()'來獲取一個日期序列並對它們進行排序(不要忘記給這些日期字符串添加年份)。你能否澄清你的問題,以便我們可以給出更好的答案? –
嗨Dean,我添加了.getValues()。我沒有在這段代碼中使用實際的日期,因爲它最終需要用純文本格式來創建.csv文件。所以日期實際上是字符串。年份也不會被使用,也不需要,因爲所有排序的日期將在一週或兩週內進行。 – Clayten