'Enable / Disable a Google Sheets script based on a sheet named 'settings' where cell A1 = true or false (checked/un-unchecked)
I have a script that is currently backing up my spreadsheet twice a day. I use a trigger to do this and it works great! I would like to have the script check the sheet (tab) named 'settings' to see if the checkmark in Cell A1 is checked (true). If it is true, it will continue to run the backup twice a day. I am essentially looking to make the cell A1 enable or disable backups. If cell A1 = True (checked), the backups will continue to run. If the cell A1 is false (unchecked), backups will not run.
Here is my current backup code that works fine. I just want an 'on and off switch':
function Backup() {
// generates the timestamp and stores in variable formattedDate as year-month-date hour-minute-second
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
// gets the name of the original file and appends the word "backup" followed by the timestamp stored in formattedDate
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Backup " + formattedDate;
// gets the destination folder by their ID. REPLACE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx with your folder's ID that you can get by opening the folder in Google Drive and checking the URL in the browser's address bar
var destination = DriveApp.getFolderById("the_folder_id_goes_here");
// gets the current Google Sheet file
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())
// makes copy of "file" with "name" at the "destination"
file.makeCopy(name, destination);
}
Solution 1:[1]
try this:
function Backup() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("settings");
if(sh.getRange("A1").getValue() == "FALSE")return;
var dts = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Backup " + dts;
var destination = DriveApp.getFolderById("the_folder_id_goes_here");
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())
file.makeCopy(name, destination);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cooper |
