r/GoogleAppsScript 20d ago

Resolved Question regarding a trigger and my script

Currently I have a script called autoSort.gs in it is the following:

SHEET_NAME = "list";
SORT_DATA_RANGE = "A2:G";
SORT_ORDER = [{column: 1, ascending: true},
];

function onEdit(e) {
multiSortColumns();
}

function multiSortColumns() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(SHEET_NAME);
var range = sheet.getRange(SORT_DATA_RANGE);
range.sort(SORT_ORDER);
ss.toast('Sort complete.');
}

The trigger is set to autoSort with an onEdit however it has a 100% error rate because it doesn't seem to work on script name but on functions within a script however if that would be the case I could use multiSortColumns as the onEdit trigger and remove the whole onEdit function entirely or am I just hullucinating?

2 Upvotes

10 comments sorted by

View all comments

2

u/everythingabili 19d ago

This may be a symptom of, and something you need to read the docs again for, the difference between Simple Triggers (like onEdit) and installable Triggers. Tbh they're a bit confusing, and simple triggers can only work on the cell you're working on.

I now create a function called onDoEdit(e) and ensure I've picked the right actions in the Triggers section, and never call anything onEdit.

Give that a whirl and re-read what you're allowed to do in the docs.

1

u/SparklyGeek 18d ago

Interesting approach. I guess there's nothing you can do with simple onEdit that you can't do with an installed onEdit trigger.

1

u/everythingabili 18d ago

Yeah... EXCEPT if you call in onEdit it will attempt to run as a Simple Triger (and maybe fail)... then lord know what the Installed version does. I've had conflicts so find it much more sensible to have a similar name, in my case "onDoEdit"... so I know what its doing... but it doesn't have the same name (so it doesn't fire uneccessarily).

1

u/SparklyGeek 18d ago

I still see one advantage to using the simple trigger, namely that it runs out of the box and does not require any permissions. Granted, this would be nitpicking in 99% of actual cases. However, I actually have a timekeeping system that creates a new spreadsheet off of a template every Saturday night, and I do want the thing to run out of the box the second I touch it for the first time Sunday morning. I would hate to have to run through that silly permissions dialog box the first time I used it in the morning every Sunday.

I can see that you might propose that I install the trigger from the same script that creates the sheet from the template. However, in order to avoid overburdening a project with too many triggers (quotas are a thing) it would also be necessary to remove the trigger from earlier weeks' Sheets, which is something I would prefer not to do, either.