get my script right Google Sheets onEdit script - google-apps-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 }]);
}

Related

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 Sheets onEdit(e) dynamic sort function

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

Sort Range on Spreadsheet with button linked to app script

I have the following code that I'm trying to link to a button to sort a range on the page by the column H (Date). I got the button linked but the code is not working
function sortRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FBY Team');
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A6:M100");
range.sort({column: 2, ascending: true});
}
I have added a link to my test spreadsheet.
https://docs.google.com/spreadsheets/d/1iWQ40boplJcJmdFg9HNIyOAOrHOjlCRu362LWRdV5y0/edit?usp=sharing
The problems
'FBY Team' is not one of your sheet.
you usually use ss variable for spreadsheets here you are trying to grab a sheet.
when line 2 is corrected you can delete line 3.
The header starts at row 7 not 6 so range is incorrect
Solution
what I suggest is you change your code to:
function sortRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Your Sheet 1');
var range = sheet.getRange("A7:M100");
range.sort({column: 2, ascending: true});
}
this should work now.

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