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!
Related
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 }]);
}
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()
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)
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.
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