It appears the currently upvoted answers were right at one point in time, but they are both from 2014 and 2018 and it appears the API for Google Sheets has since been updated along with how you accomplish this through the UI of the spreadsheet you're working on. Listed below is how I accomplished the task that the original question was asking about. I will try to communicate the instructions on how to do this in a way that a person with little to no software engineering or programming experience can follow them.
It appears the getSheetByName()
function either no longer exists or is no longer necessary. To do what the original author of this question is trying to do (as of April 2nd, 2023), you should use the following code:
function clearRange() {
// replace '1uHKL0nc0qi6j8zW9aoG6Je67nmUu0_pqVQdwWYMuI9w' with your actual sheet ID
var sheetActive = SpreadsheetApp.openById("1uHKL0nc0qi6j8zW9aoG6Je67nmUu0_pqVQdwWYMuI9w");
sheetActive.getRange("A:Y").clearContent();
}
Replace the second instance (or both instances if you so wish) of
1uHKL0nc0qi6j8zW9aoG6Je67nmUu0_pqVQdwWYMuI9w
in the code listed above with the value that's underlined in red in the image below for the Google Sheet you're trying to run the code on. If you want to clear a range of columns different than A:Y, change the A:Y listed between the quotations in the script above to the values of the columns you'd like to clear instead. See the following examples:
-To clear all columns from F to M:
F:M
-To just clear column B:
B:B
Then, navigate to the Extensions tab and then select the Apps Script option:
Select the Editor button from the top-left menu:
Delete the current contents already in the Editor. It should look something like this:
Add the contents of the script that I provided towards the top of this post into the editor like below. Note that the Sheet ID value should be swapped out for the sheet ID of the Google Sheet you're attempting to run the script on.
Then hit the save button:
Select the Triggers button from the top-left menu:
Select the Add Trigger Button in the bottom-right corner:
You can then select the options that make the most sense for you. To do what the original question was asking (clear values every day at midnight), you'd want to select the following options and then hit the save button in the bottom-right corner:
Note that if you see an error whenever you try to hit the save button in the previous step, you likely just need to enable access to your spreadsheet/account through the pop-up menu. Firefox was blocking my pop-up menu, so if you don't see anything when saving and you're seeing an error, disable your pop-up blocker.
If this was successful, you should see something like this populated in the Triggers page:
If you see this, congrats your spreadsheet will now automatically clear the values of the rows specified in the script (A to Y in the example I provided) between 12 and 1 am CST every night!