Google Sheets Apps Script - Decrease or Increase Date

TAPS_MikeDion

Well-known Member
Joined
Aug 14, 2009
Messages
629
Office Version
  1. 2011
Platform
  1. MacOS
Happy New Year's Eve to everyone!

I'm sure this will be an easy one for those of you who know the Google Sheets Apps Script language.
All I was hoping to do is have the date in cell A2, which is selected by using data validation as a drop-down calendar (this is already set up), be reduced by 14 days when the ◀️ image is clicked or increased by 14 days when the ▶️ image is clicked. I realize this will require 1 script for each function.

For example:
1. The date is set via the drop-down calendar in A2 to 12/28/24
2. Clicking the ◀️ changes (reduces) the date in cell A2 to 12/14/24
- OR-
3. Clicking the ▶️ changes (increases) the date in cell A2 to 1/11/24

I'm all set with knowing how to assign the scripts to the images, it's just the script code for the date changes that I'm not getting right.

Thank you to anyone offering to assist!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Disregard. Google AI was able to assist me.

For anyone down the road who may need this, here is the Google Sheets Apps Script it gave me that worked.

VBA Code:
function addDaysToCellA2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var date = sheet.getRange("A2").getValue(); // Get the date from cell A2
  var newDate = new Date(date.getTime() + 14 * 24 * 60 * 60 * 1000); // Add 14 days
  sheet.getRange("A2").setValue(newDate); // Set the new date in cell A2
}
function reduceDaysToCellA2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var date = sheet.getRange("A2").getValue(); // Get the date from cell A2
  var newDate = new Date(date.getTime() - 14 * 24 * 60 * 60 * 1000); // Reduce 14 days
  sheet.getRange("A2").setValue(newDate); // Set the new date in cell A2
}
 
Upvote 0
Solution

Forum statistics

Threads
1,225,187
Messages
6,183,425
Members
453,160
Latest member
DaveM_26

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top