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
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 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!
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 sheet where when I change a specific cell to "YES", I need a template sheet to be copied to a new version and named as per the value of a cell on the current row.
I'm having trouble working out how to get the value of the first cell in the row selected. This is what I have so far (I know this is wrong):
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var currentCell = sheet.getCurrentCell();
if (currentCell = "YES")
{
SpreadsheetApp.getActiveSpreadsheet().toast("New change control sheet added to workbook.","Change Control",15);
var sourceRow = ss.getActiveRange().getRowIndex();
var tabName = ss.getRange(cell,1).getValues();
ss.getSheetByName("CCTemplate").showSheet()
.activate();
ss.setActiveSheet(ss.getSheetByName('CCTemplate'), true);
ss.duplicateActiveSheet();
ss.setActiveSheet(ss.getSheetByName('CCTemplate'), true);
ss.getActiveSheet().hideSheet();
ss.setActiveSheet(ss.getSheetByName('Copy of CCTemplate'), true);
ss.getActiveSheet().setName("CC" & tabName);
}
}
Any ideas?
function onEdit(e) {
var sh=e.range.getSheet();
if(sh.getName()=='Your Sheet Name' && e.value=="YES") {
e.source.toast="New change control sheet added to workbook.","Change Control",15);
var tabName=sh.getRange(e.range.rowStart,1).getValue();
var tsh=e.source.getSheetByName('CCTemplate');
var csh=tsh.copyTo(e.source);
csh.setName('CC'+tabName);
}
}
You should avoid using activate in your scripts especially in simple triggers where you have to finish in 30 seconds. I think this code does the same thing that you intended for your code. One significant difference is that I use the information that comes in the event object that comes with the trigger. You should add the code Logger.log(JSON.stringify(e)) and then look at the logs you will see that there is a lot of information available to you which removes the need to run extra functions to get things like a spreadsheet.
Use event objects
onEdit offers among others the event objects range and value which are helpful to retrieve the range that has been edited and its value.
Also
When you want to a cell and compare it against a value, like in if (currentCell = "YES") - you need to retrive its value (either currentCell.getValue() or just event.value) and you need to use == instead of = for comparison.
Be careful with getValues() vs getValue(). The former gives you a 2D array and is not necessary if you want to retrieve the value of a single cell.
There is no need to set your sheet to active in order to change its name.
You can rewrite your code as following:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var currentCell = event.range;
var value = event.value;
if (value == "YES")
{
...
var sourceRow = range.getRowIndex();
var tabName = ss.getRange(sourceRow, 1).getValue();
...
ss.getSheetByName('Copy of CCTemplate').setName("CC" + tabName);
}
}
Good morning everyone :),
I'm struggling withe a problem for some time and I was wondering that anyone can help me here.
I wrote some script that is copying and pasting rows from one sheet to another when Q column checkbox is TRUE. Please see code below :
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
if(ss.getActiveCell().getColumn() == 16 && ss.getActiveCell().getRow() > 1 && ss.getActiveCell().isChecked()) {
var source = ss.getRange("TRACKER!B2:N2");
var destSheet = ss.getSheetByName("ARCHIVE");
var destRange = destSheet.getRange(destSheet.getLastRow()+1,2);
source.copyTo(destRange,{contentsOnly: true});
source.clear();
}
}
My QQ is how to change last script input :
source.clear();
That instead of clearing it just delete whole row, and also how to change
var source = ss.getRange("TRACKER!B2:N2");
That B2:N2 will be change into B?:N? where "?" depending which row in column Q was selected.
Thank You in advance
Have a good day
Mariusz
When the onEdit() trigger fires,
Apps Script passes the function an event object as an argument,
typically called e. The event object contains information about the
context that caused the trigger to fire.
So you could change your script to:
function onEdit(e) {
var activeSheet = e.source.getActiveSheet(); // Assumes the active sheet is called 'TRACKER'
var activeCell = e.source.getActiveCell();
if ( activeCell.getColumn() == 17 && activeCell.getRow() > 1 && activeCell.isChecked() ) {
var source = activeSheet.getRange(activeCell.getRow(), 2, 1, 13); // Columns B through N
var destSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ARCHIVE");
var destRange = destSheet.getRange(destSheet.getLastRow() + 1, 2);
source.copyTo(destRange,{contentsOnly: true});
source.clearContent(); // To clear just the contents and not delete the row
}
}
Column Q = column 17.
This script assumes that the active sheet with the checkboxes is called 'TRACKER'
Ref docs for onEdit()
Ref docs for getting cells, ranges in spreadsheets and copying and clearing them.
Hello ADW and thank you for quick reply,
I have update the code like this :
function onEdit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
if(ss.getActiveCell().getColumn() == 16 && ss.getActiveCell().getRow() > 1 && ss.getActiveCell().isChecked()) {
var source = ss.getRange("TRACKER!B" + ss.getActiveCell().getRow()+ ":N"+ss.getActiveCell().getRow());
var destSheet = ss.getSheetByName("ARCHIVE");
var destRange = destSheet.getRange(destSheet.getLastRow()+1,2);
source.copyTo(destRange,{contentsOnly: true});
ss.deleteRow(ss.getActiveCell().getRow());
}
}
and it seems to work fine now but nevertheless once again thank you for the code it surly help me to solve this query.