简体   繁体   中英

Google Sheets: Script order only rows where date > today

Some help will be appreciated. I have the following code:

function sort() {

  //Variable for column to sort first
  var sortFirst = 1;
  var sortFirstAsc = true;
  //Variables for column to sort second
  var sortSecond = 2;
  var sortSecondAsc = true;
  //Variables for column to sort third
  var sortThird = 4;
  var sortThirdAsc = true;

  //Number of header rows
  var headerRows = 1; 

  /** Define variables */

  /** Sorting function **/

  var activeSheet = SpreadsheetApp.getActiveSheet();
  var sheetName = activeSheet.getSheetName(); //name of sheet to be sorted
  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
  var range = sheet.getRange(headerRows+1, 1, sheet.getMaxRows()-headerRows-1, sheet.getLastColumn());
  range.sort([{column: sortFirst, ascending: sortFirstAsc}, {column: sortSecond, ascending: sortSecondAsc}, {column: sortThird, ascending: sortThirdAsc}]);
}

It works fine, but now i want to order only when date from column A is TODAY or greater.

Image of sheet I want to sort below row 3 only.

Any idea how can achieve that?

Thanks in advance;)

Solution

A possible workaround to solve your isse is to sort only in the range of dates that are today or later. Follow these steps to achieve this:

  1. Sort the range in ascending order according to the date column (1).
  2. Search for the first item that is equal or higher than today's date.
  3. Select a new range that starts from the row of the item we found on the previous step.
  4. Execute the sorting function on this new range.

Here is the piece of code to complete this task explained with comments:

 function sort() { //Variable for column to sort first var sortFirst = 1; var sortFirstAsc = true; //Variables for column to sort second var sortSecond = 2; var sortSecondAsc = true; //Variables for column to sort third var sortThird = 4; var sortThirdAsc = true; //Number of header rows var headerRows = 1; /** Define variables */ /** Sorting function **/ var activeSheet = SpreadsheetApp.getActiveSheet(); var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1'); var range = sheet.getRange(headerRows+1, 1, sheet.getMaxRows()-headerRows-1, sheet.getLastColumn()); // Sort the whole range by date in ascending order range.sort([{column: 1, ascending: true}]); // Get the date values of the first column to then compare them to today's date. Flat is used to create a 1D array as getValues() will return a 2D array and to make things simpler var getDateValues = sheet.getRange(headerRows+1, 1,sheet.getLastRow()-1,1).getValues().flat(); // Initialise today's date. As we are only interested on comparing the date, we set the starting time at 00am. We also start a variable to store the index of the row where dates are equal or higher than today. var today = new Date().setHours(0,0,0,0); var indexStartRange; // Loop through all dates values for(i=0;i<getDateValues.length;i++){ // Create date object for each value var date = new Date(getDateValues[i]); // if this date is equal or higher than today, then we store the value of its index and break the loop. if(date.valueOf() >= today.valueOf() ){ indexStartRange = i + 2; break; } } // create a final range starting from the row where dates start being equal or higher than today and just run as you did in your example before. var finalRange = sheet.getRange(indexStartRange, 1, sheet.getLastRow(), sheet.getLastColumn()); finalRange.sort([{column: sortFirst, ascending: sortFirstAsc}, {column: sortSecond, ascending: sortSecondAsc}, {column: sortThird, ascending: sortThirdAsc}]); }

I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM