Change Header Color Automatically
Learn how to automatically change the header color in your Google Sheets based on the time of day using Apps Script. This tutorial covers creating functions for sunrise, sunny, sunset, and night colors.
This is something fun we can create in our spreadsheet . We're going to change the header color based on what time it is, based on the time of day. We're going to change the header color either at night to a deep, dark purple, at sunrise a nice little orange yellow, and then during the day we're gonna keep it as a sunny, bright yellow, and during sunset at 4pm Change it to this sort of sunset y orange. So how are we going to do this? We're going to use Apps Script , and it's fairly easy. Just go up to Extensions, Apps Script . Here we have a function called My Function . We're going to need to create four different functions. The first one we will create, let's look at our timing. We have, the first one during the day is sunrise. Sunrise. So we'll call this Function sunrise. We're going to get spreadsheet app, which is just a general function inside Tips to Navigating Thousands of Lines of Code In Apps Script How To Create a Stop Watch in Google Sheets Introducing SheetFools Alternate Row Color Automatically
of App Script that we can use here. Get active spreadsheet . We're gonna choose which is the entire workbook. We're going to get sheet by name, and the sheet is gonna be called Jobs. In this particular case, we're going to get the range , which is in quotes. 1 colon 1 is the header row , the first row. We can make this a little easier to read. There we go. And now we're going to set background. The background we're going to set, in this case, let's go grab our sunrise hex code we've already picked in quotes. And so this function is called sunrise. We will now copy and paste this and call this one night. And we'll use this hex code. We'll copy and paste this as well. And do sunny, we'll call this function sunny. Getting Started Coding in Apps Script Getting Started Coding in Apps Script Explore Readable Hex Codes VIBE CODE GOOGLE SHEETS
We're going to format it a little bit just so we can see all of them and then we'll need sunset. Sunset, and let's change that hex code. So they're all four of these are the same function . Let's test it, let's test sunrise. I'm going to hit run up here. Go up to our jobs and it is changed the header . So how do we do this based on the time? Let's go back to our colors and look at our timing. So the first one we, in the day time, is sunrise. So we can move these like this, so that we can see sunrise, sunny, sunset, then night. So at 6am, we need to trigger sunrise. Let's go over to the left side, create trigger . On the bottom right, add trigger . Choose which function to run, we're going to choose sunrise. Instead of event source from spreadsheet , we're going to use time driven. We're going to choose A day timer. And here we can select the time of day. I'm going to choose 6am. So this will run between the hours of 6am to 7am. Now Do It Every Damn Day - Learn to Code in Google Sheets Part 5 Learn to Code in Google Sheets, For Programmers | For Advanced Google Sheet Users Automate Two Sheets
Click save. And now we're going to do the 10am for sunny. So create a trigger . Add trigger on the bottom right. Choose which function to run. Sunny. Time driven. Day timer. And 10 a. m. So this is only triggering it once. We just need to trigger it once and each one will override the last one. So now we need to do sunset at 4 p. m. Add trigger . Sunset. Time driven. Day timer. 4 p. m. Save. And our last one is going to be the night. Time driven. Day timer. We'll choose 7 p. m. Click save. If for any reason you want to edit any of these, the pencil icon on the far right will Allow you to edit the trigger if we want to delete these and we're like hey We're tired of these changing all the time We can hit the three dots on the right and click delete trigger to delete any of these triggers We go delete forever and we can always add them back if we want to But now you have your Create an Automated Task List Learn to Code in Google Sheets, For Programmers | For Advanced Google Sheet Users How to Prevent Delete Tab
header changing to very specific hex codes Explore Readable Hex Codes