Google Sheets onEdit(e) dynamic sort function - google-apps-script

Thank you in advance for reading my post and offering suggestions.
Fun disclosure, I found the snippet of code on the internet and tried to adopt it to my needs, I don't intend to plagiarize anyone's work.
I invoke the following from my onEdit function but is does not do anything at all. I want to sort the table in ascending chronologically order based on column 12 which is a date field, as this field is updated in real time.
function onEdit(e) { SortApptDate(e); }
function SortApptDate(e) {
var sheet = 'Sheet1';
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort({ column: 12, ascending: true });
}
Thank you again.
Jim

Modification points:
In your script, sheet is not the sheet object. By this, the script is not run. An error occurs. In your situation, the event object can be used.
When above points are reflected to your script, it becomes as follows.
Modified script:
function onEdit(e) { SortApptDate(e); }
function SortApptDate(e) {
var sheet = e.source.getActiveSheet()
if (sheet.getSheetName() == "Sheet1") {
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());
range.sort({ column: 12, ascending: true });
}
}
In this modified script, when "Sheet1" is edited, the script for sorting is run.
When you edited cell in "Sheet1", range is sorted with the column "L" as the ascending sort.
References:
Simple Triggers
Event Objects

Related

get my script right Google Sheets onEdit script

So I tried to write this code in the script editor,
but it seems that is not working.
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var range = sheet.getRange("A2:D");
range.sort(\[{column: 1, ascending: true}, {column: 4, ascending: true}\]);
}
I'm trying to auto-sort by two columns in the range A2:D every time a new line adds to the sheets. Can someone help, please?
I'm trying to auto-sort by two columns in the range A2:D every time a new line adds to the sheets. Can someone help, please?
This works for me:
function onEdit(e) {
//e.source.toast("Entry");//just for debugging
var sheet = e.range.getSheet();
var range = sheet.getRange("A2:D"+sheet.getLastRow());
range.sort([{ column: 1, ascending: true }]);
}

Google Script for Sheet Auto Sort not triggering

I have the following script that works during testing but it does not trigger on edit. Basically in Column 5 I have a dropdown and want them to sort when changed.
// Define the order by which you want to sort
SORT_ORDER = [
{column: 5, ascending: true}, // index 1 for category column
{column: 17, ascending: false} // Index 3 for Revenu column
];
// On sheet edit, apply the function multiSortColumns()
function onEdit(e){
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/19elFArXzf32sSkeyxJ39cSYOVG4PH51px42-ocW3HYI/edit#gid=1695682723");
var sheet = ss.getSheetByName("Deals"); // Get the working sheet name
var range = sheet.getRange("A2:T"); // Get the range of data
range.sort(SORT_ORDER);
}
I tried changing the form onEdit to onChange and no luck.
If it's the same worsheet you're working on, try changing the definition of ss to:
var ss = e.source
That should overcome the permissions issue for onEdit. Let me know!

Google Sheets – Auto-Sort 2 Columns within a Specific Range on Multiple Worksheets

I have been looking for a way to script an auto-sort on a Google Sheet that does the following:
Only applies to a specific range (A35:I911), but on all of the worksheets within the main Google Sheet.
Sorts the A column (Date of Post) first and the B column (Time of Post) second.
Occurs on edit of any cell within that range (A35:I911).
I tried the following code, but it will not work properly:
function onEdit(event) {
var postDate = 1;
var postTime = 2;
var sheet = event.source.getActiveSheet();
var tableRange = "A35:I911";
var range = sheet.getRange(tableRange);
range.sort([
{column: postDate, ascending: true},
{column: postTime, ascending: true}
]);
}
Thank you in advance!
Here is a link to a redacted version of the Google Sheet: https://docs.google.com/spreadsheets/d/1NUy6aY-lEA66UFXg4Yx4mXsTgFvj5g2Dhv4jToYsQnw/edit?usp=sharing)
Modification points:
Although from your question, unfortunately, I cannot understand about but it will not work properly, when I saw your sample Spreadsheet, it seems that the max row is 910 at the sheet "March 2021". I thought that this might be the reason of your issue of it.
When you want to run the script when the cells "A35:I911" are edited, in your situation, I thought that when the cells "A35:B911" are edited, running the script might be suitable. Because even when the cells "C35:I911" are edited, the sort is not run.
And, from your sample script, I think that you might not want to run the script when the sheet "Overview" is edited.
When above points are reflected to your script, it becomes as follows.
Modified script:
function onEdit(event) {
var range = event.range;
var sheet = event.source.getActiveSheet();
if (sheet.getSheetName() == "Overview" || range.rowStart < 35 || range.rowStart >= 911 || range.columnStart > 2) return;
var postDate = 1;
var postTime = 2;
var tableRange = sheet.getMaxRows() < 911 ? "A35:I" + sheet.getLastRow() : "A35:I911";
var range = sheet.getRange(tableRange);
range.sort([
{column: postDate, ascending: true},
{column: postTime, ascending: true}
]);
}
When you want to run the script, please edit the cells "A35:I911" in the sheet except for "Overview".
When you want to run the script when the cells "A35:I911", please modify range.columnStart > 9.
References:
Simple Triggers
getMaxRows()

Google Apps Script to sort empty cells in a particular column to the top

I have a lost property register in Google Sheets which has columns A:I. As items are found, users add the items to the sheet so over time the sheet will have a significant number of rows. This is a sample of my sheet:
I have several buttons in row 1 which have GAS behind each so that a user can sort a particular column.
Column H is populated with a conclusion date and I want a button with GAS to first sort column G, the "Expiry Date" ascending which will always be populated and secondly column H, the "Conclusion Date" so that the empty cells are at the top i.e. all items that have not yet been concluded at the top of the sheet. I have the following GAS but this sends all of the empty cells to the bottom.
//Sort Lost Property Register By Column H
function SortLostPropertyRegisterByColumnH() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Lost Property Register');
var range = sheet.getRange("A3:I");
range.sort({column: 7, ascending: true});
range.sort({column: 8, ascending: false});
}
Is there any way to do this?
I believe your goal as follows.
You want to put the rows that the column "H" is blank to the top of the range A3:I after the sort was finished.
For this, how about this answer?
In this answer, I would like to propose the following flow.
Sort the Spreadsheet.
In this case, your script is used.
Retrieve the values from the range A3:I.
The rows with the empty cell at the column "H" are put to the top of values.
Put the processed values to the Spreadsheet.
Sample script:
When your script is modified, it becomes as follows.
function SortLostPropertyRegisterByColumnH() {
// 1. Sort the Spreadsheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Lost Property Register');
var range = sheet.getRange("A3:I" + sheet.getLastRow()); // Modified
range.sort({column: 7, ascending: true});
range.sort({column: 8, ascending: false});
// I added below script.
// 2. Retrieve the values from the range `A3:I`.
// 3. The rows with the empty cell at the column "H" are put to the top of values.
SpreadsheetApp.flush();
const values = range.getValues().reduce((o, e, i, a) => {
o[e[7].toString() == "" ? "blanks": "values"].push(e);
if (i == a.length - 1) o.result = o.blanks.concat(o.values);
return o;
}, {blanks: [], values: []});
// 4. Put the processed values to the Spreadsheet.
range.setValues(values.result);
}
References:
reduce()
setValues(values)

Script that it is not effective when inserting a row

In a script in Google Spreadsheet, I manage to make a script to sort automatically my spreadsheet:
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var editedCell = sheet.getActiveCell();
var tableRange = "A2:G100";
if(editedCell.getColumn() == 1,2){
var range = sheet.getRange(tableRange);
range.sort( [1,2] );
}
}
My issue is that I need the script to NOT function when I insert a new row. I want people to be able to insert a row and that the sheet sort only when we edit the column 1 or 2.
Is that possible?
If not, maybe there is something to activate the event every 2 or 3 minutes. It's really not the best, but, if there is really nothing else, I might live with that.
Thanks in advance!
I would rewrite this:
I wasn't sure about the following code so I tested it and it doesn't work for me.
if(editedCell.getColumn()==1,2)
{
var range = sheet.getRange(tableRange);
range.sort( [1,2] );
}
Like this:
var ecol=editedCell.getColumn();
if(ecol==1 || ecol==2)
{
var range = sheet.getRange(tableRange);
range.sort( [{column: 2, ascending: true}, {column: 1, ascending: true}]);
}
You might wish to try the installable onChange event because you can use the e.changeType parameter if(e.changeType=='EDIT') to specify which changes you wish to allow. Reference