1

I am running a script that automatically organizes priority in a to-do list. It works great on the current tab, but when moved to a sheet within the worksheet the same OnEdit function will not run. Here is the code below:

function myFunction() {

}

function onEdit(B){
  var sh0 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
  var editedCell = sh0.getActiveRange().getColumnIndex();

  if(editedCell == 1) { 
    var range = sh0.getRange("A4:G");
    range.sort({column: 3, ascending: true});
  }
}

I also have a script to delete rows when marked "completed" and it works fantastically throughout the entire sheet.

function myFunction() {

}
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
  var lastRow = sheet.getLastRow();
  for( i=1 ;i<=lastRow ; i++) { // i <= lastRow
       var status = sheet.getRange("F"+i).getValue();
    if (status == "Completed") {
          sheet.deleteRows(i);
    }
  }
}
3
  • It's not clear what you mean by the content you added on the last edit ( rev. 4 ). By the other hand, post questions should be specific and should contain only one question. Also it's worth to note that you could delete the myFunction function as it's does nothing and apparently you are not using it. Commented Jun 19, 2019 at 21:21
  • I guess I'm confused at how to get the triggering function to work. I can't seem to figure it out after reading the post probably about 30 times :( Commented Jun 19, 2019 at 22:19
  • Maybe we should start from 0. Create a new spreadsheet, add a new script project to it, delete the default code and add one an only one onEdit function. To test it edit your spreadsheet, if it works, fine you are done, right? if it doesn't work look at the execution code, then edit your question to add the details about what you did and the execution transcript. Commented Jun 20, 2019 at 4:11

1 Answer 1

0

The problem with your script are the following code lines

var sh0 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];

The above code line assigns the first sheet to sh0

var editedCell = sh0.getActiveRange().getColumnIndex();

The above code line gets a range from the first sheet

Try this function

function onEdit(e){
  if(e.range.columnStart === 1) { 
    var range = e.range.getSheet().getRange("A4:G");
    range.sort({column: 3, ascending: true});
  }
}

Reference

Related questions

8
  • Thank you so much for your response. I am getting an error that says "TypeError: Cannot read property "columnStart" from undefined. (line 6, file "Code")". Any idea? Commented Jun 19, 2019 at 19:52
  • @AlejandroStory I'm sorry. It should be e.range.columnStart instead of e.columnStart. I corrected my answer. If you don't see the change please refresh your web browser tab. Commented Jun 19, 2019 at 20:11
  • ok so when I do this I get "TypeError: Cannot read property "range" from undefined. (line 6, file "Code")". Thank you for helping me figure this out, I can tell you know more than I do about this stuff, I am a newbie :) Commented Jun 19, 2019 at 20:16
  • @AlejandroStory That is because you are running the onEdit function directly from the code editor and the event object isn't assigned. Commented Jun 19, 2019 at 20:21
  • how and where do I assign the event object? Commented Jun 19, 2019 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.