I am slowly teaching myself how to use Google Apps Script. I wrote a script that sorts Google Form results in reverse chronological order (so that the most recent submission is always at top). I successfully got it to run the script and sort on every form submission, but it creates a new trigger every time, so that after 25 form submits the triggers have piled up and I get a “exception: this script has too many triggers” error message.
Here is what I have written, if someone can give me some advice about what I am doing wrong, I appreciate it:
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("myFunction")
.forSpreadsheet(sheet)
.onFormSubmit()
.create();
function myFunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getActiveSheet()
var lr= s.getLastRow()
var lc=s.getLastColumn()
var data=s.getRange(1,1,lr,lc)
data.sort( { column : 1, ascending: false } )
}
I also tried writing into the script to delete the trigger afterwards, using:
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++)
ScriptApp.deleteTrigger(triggers[i]);
so it successfully deletes the triggers but it also stops sorting the submissions too.
StevenBalke is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.