Apps script stops working for no reason?

67 views
Skip to first unread message

Grzegorz Maciag

unread,
Jan 19, 2023, 6:27:15 AM1/19/23
to Google Apps Script Community
Hi! I created a few scripts to automate some formatting in a Google Sheet. Those are onEdit scripts that are supposed to make sure none accidentally deletes the necessary formatting of the sheet. I'm attaching one example of such a script below.

According to this guide: https://developers.google.com/apps-script/guides/sheets it should be as easy as creating the script, saving the script and then it should be just running it sheet all the time. However, my problem is that it only happens for a short time after I create the file and then after a day or two it stops running. No error messages, nothing - the script just never executes. What I have to do is to create a new file, write the script again, save and only then it begins to work (for some time). Even making a copy of the file doesn't work - it has to be a new one. That makes me wonder whether there's some limit on how many times the scripts can be executed? 

It's a bit difficult for me to describe what's happening, since I don't get any errors, but I hope it was clear enough. Does anyone have any idea what's happening?



function onEdit(e) {

var sleepTime = 5000;
var lastEditedTime = new Date().getTime();
PropertiesService.getScriptProperties().setProperty('lastEditedTime', lastEditedTime);
Utilities.sleep(sleepTime);
var newTime = new Date().getTime();
if(newTime - PropertiesService.getScriptProperties().getProperty('lastEditedTime') < sleepTime - 50) {
return;
}

var range = e.range; //range just edited
var sheet = range.getSheet();
if(sheet.getName() !== 'Orders-2023') {
return;
}
sheet.getRange('P2:P924').insertCheckboxes();
}

carlos garcia valencia

unread,
Jan 24, 2023, 12:04:20 AM1/24/23
to Google Apps Script Community
Hola, ya intetó añadiendo un disparador en donde al abrirse la hoja de cálculo se activo el código?

Alex

unread,
Jan 24, 2023, 10:33:16 AM1/24/23
to Google Apps Script Community
Hi!

@Grzegorz, don't use Utilities.sleep in event triggers.

I think you have to do something like this Reset Missed Formatting

This app ruins two triggers triggerOnEdit_triggerResetFormattion_. If someone changes a value on _MAGICBOX_.observedSheet.sheetName then it writes a mark. Every minute, another trigger checks this mark and resets the formatting if needed.

Before do anything you the owner have to run userActionInstallAllOfThis once.

1674574223424.png

Cheers!

Reply all
Reply to author
Forward
0 new messages