Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:14:48+00:00 2026-06-17T13:14:48+00:00

I have a spreadsheet that I want to conditionally format according to dates. If

  • 0

I have a spreadsheet that I want to conditionally format according to dates. If a date is 7 days away, I want the cell to turn red, if a date is 14 days away, I want it to turn green, etc.

The first step to doing this, I figured, would be to create a simple conditional format script, where if a date is today, it would be highlighted. But my code doesn’t work 🙁

function formatting() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); // get the sheet
  var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1); // get all the rows
  var fValues = columnF.getValues(); // get the values
  var today = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy"); // get today's date
  for (var i = 0; i < fValues.length; i++) { // repeat loop
    if (fValues[i][0] == today) { // if the date in the cell is today's date...
      sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('green'); // ...turn that cell green
    }
  }
}

What am I doing wrong?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:14:50+00:00Added an answer on June 17, 2026 at 1:14 pm

    You should not convert your date to a string, you can parse the date in days, month and years like in Srik’s answer, or you can also compare dates using their native values like below, it becomes easier to calculate other dates… I created a few variables to simplify the math :

    day = the number of milliseconds in a day

    today = today’s date at 00:00 (in days)

    function formatting() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'); // get the sheet
      var columnF = sheet.getRange(2, 6, sheet.getLastRow()-1, 1).setBackgroundColor('white'); // get all the rows and clear colors
      var fValues = columnF.getValues(); // get the values
      var day = 24*3600*1000
      var today = parseInt((new Date().setHours(0,0,0,0))/day); // get date today
      for (var i = 0; i < fValues.length; i++) { // repeat loop
        var dataday = parseInt(fValues[i][0].getTime()/day)
        Logger.log(dataday+' = '+today)
        if (dataday == today-7) {sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('red')}
        else if (dataday == today-14){sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('green')}
        else if (dataday == today-21){sheet.getRange(i + 2, 6, 1, 1).setBackgroundColor('blue')}
      }
    }
    

    Test sheet available here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large spreadsheet in Excel 2007 OpenXML format that I want to
I have a spreadsheet in google docs that I'd want to integrate in a
I have a spreadsheet with action items that have dates and priorities attached to
I have some data in CSV format that I want to pull into an
I have data from an Excel spreadsheet that I want to import into a
So I have an excel spreadsheet that has a specific column I want to
I have a google spreadsheet that looks like this, for budget keeping. I want
I want to update a cell in a spreadsheet that is used by a
I have a big json object containing cell data from a sample spreadsheet that
I have a list of Google Apps users in a spreadsheet that I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.