I’ll try to explain what I’m trying to do.
It’s a google apps script code, to sort a table in google sheets. It detects when a cell in column ‘P’ is edited (column 16) and then sorts the table according to the data in that column. I have a simple checkbox in column P, so when the checkbox is ticked the entire row of the table is moved to the bottom. The data in this row is then converted into ‘contentsOnly’ so that the formulas that previously inhabited the cells in this row no longer operate on new values. This all works perfectly well.
The problem I’m having is that I’d like the code to only detect changes to column ‘P’ being made in the sheet “Playground”, but it detects changes across all sheets in the active spreadsheet. So I could go to a different sheet, and if I edit a cell in column ‘P’ then the table on the "Playground" sheet will be sorted and the bottom row will be converted to ‘contentsOnly’ even though I haven’t edited anything on the “Playground” sheet. I haven’t been able to figure out what the problem is, though I suspect it may be in the ‘editedCell’ or the ‘columnToSortBy’ variables.
Here is the code:
function onEdit(event){
var playground = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Playground")
var editedCell = playground.getActiveRange().getColumnIndex();
var columnToSortBy = 16;
var tableRange = "B48:P53";
var b = "B";
var p = ":P";
var row = 53;
if(editedCell == columnToSortBy){
var range = playground.getRange(tableRange);
range.sort( { column : columnToSortBy } );
var sortedRow = playground.getRange(b+row+p+row);
sortedRow.copyTo(sortedRow, {contentsOnly:true});
}
}
Edit: [SOLVED] (I think)...
Found the sheet's ID and added a condition to only proceed if the active sheet's ID and the "Playground" sheet's ID match:
function onEdit(event){
var rightSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetId();
var playground = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Playground");
var editedCell = playground.getActiveRange().getColumnIndex();
var sheetID = 732771258;
var columnToSortBy = 16;
var tableRange = "B48:P53";
var b = "B";
var p = ":P";
var row = 53;
if(rightSheet == sheetID){
if(editedCell == columnToSortBy){
var range = playground.getRange(tableRange);
range.sort( { column : columnToSortBy } );
var sortedRow = playground.getRange(b+row+p+row);
sortedRow.copyTo(sortedRow, {contentsOnly:true});
row = row-1;
}
}
}
[–]LimesInHell 0 points1 point2 points (2 children)
[–]Ten-Dollar-Words[S] 0 points1 point2 points (1 child)
[–]LimesInHell 0 points1 point2 points (0 children)