Short version: I want to output text in cell B12 if a time-driven trigger has been set for the sheet. I am using a free Google account (not G Suite). If you don't want to read my long version, you can skip to (2) at the end of long version and then to code.
Long version: I created some digital paperwork for our employees. We need to backup these sheets as we cannot rely solely on the Google Drive storage. I was dumbfounded when I realized that the .gsheet files that are pulled down when using Google Drive Backup and Sync are not actual backup files - they are just a file containing a URL which points to the cloud location. So, I figured out how to write a script [function backup()] to make a copy of the sheet as an XLSX file and put it in another folder in GDrive. That is done and works great when I combined it with a time-driven trigger that is installed programmatically.
Next comes the problem. There is a sheet that is named TEMPLATE. Every morning, the supervisors make a copy of the TEMPLATE sheet. They then use the copy to record their daily work. Every job needs a separate sheet. Herein lies the problem - time-driven triggers are not copied when you copy a sheet. So every new sheet that is made (by copying the TEMPLATE) does not have the time-driven trigger installed.
My solution that does not work: To programmatically install time-driven triggers on a newly copied sheet, authorization is required first. To solve this, I created another script that creates a custom menu. In that custom menu the user can click a button that runs the another script I created onOpen(). When selected, onOpen() creates the time-driven trigger programmatically but first asks for authorization. This means on every single job (new sheet created by copying TEMPLATE), someone has to click through the authorization steps to allow the script to run. We run multiple jobs per day. This is a hassle for my employees. I am trying as hard as I can to work around this issue.
(1) I don't think there is any way around the authorization, but if anyone knows any way, PLEASE let me know!
(2) I do not want to require the users to remember to click the custom menu and then the function:
I want B12 to designate if the user needs to access the custom menu to run the function.
So B12 would have the following text written to it "Must choose at top: Custom Menu/Initialize Backend" **IF** a trigger has not been created.
My limited understanding is that the code below works when run straight from the script editor because it bypasses the limitations of the built in function onEdit(e).
When I edit the sheet within the defined range that the code lists, nothing happens because onEdit(e) will not call ScriptApp.getProjectTriggers().
I have spent days researching other solutions, and now I am hoping some kind soul will lend their knowledge. Maybe I can take an entirely different approach? I sure hope this is possible.
For clarity, I hope we can find an answer to what I laid out in (2). But, below is what I want to do stated simply from start to finish. If you can think of a different way to approach this altogether, please let me know:
- End user makes copy of TEMPLATE Google Sheet to use for the day.
- Scripts in the TEMPLATE get copied too, so end user does not have to do anything else.
- Later that day, scripts make a copy of the Google Sheet and saves it as an XLSX file in another Google Drive folder at specified time.
- I use Google Drive Backup and Sync to download all the XLSX files to my comptuer, and then I use CMD Robocopy to transfer to server on a schedule.
function onEdit(e)
{
var editRange = { // D2:H8
top : 2,
bottom : 8,
left : 4,
right : 8
};
// Exit if we're out of range
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
var thisCol = e.range.getColumn();
if (thisCol < editRange.left || thisCol > editRange.right) return;
// We're in range
var ss = e.range.getSheet();
// onEdit won't call ScriptApp.getProjectTriggers() CAN'T MAKE THIS WORK
if (ScriptApp.getProjectTriggers() < 1) {
ss.getRange("B12").setValue('Must choose at top: Custom Menu/Initialize Backend');
}
else
{
ss.getRange("B12").setValue('');
}
}