Skip to content

Commit e9837ea

Browse files
author
Eric Koleda
committed
Removed number workaround code
Previously time values in New Sheets were being returned as number values representing the the fraction of a day (24 hours). This has now been fixed, so the workaround is being removed.
1 parent dd3b276 commit e9837ea

File tree

1 file changed

+2
-30
lines changed

1 file changed

+2
-30
lines changed

date_add_and_subtract/Code.gs

+2-30
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ var moment = Moment.load();
2929
var VALID_UNITS = ['year', 'month', 'week', 'day', 'hour', 'minute', 'second',
3030
'millisecond'];
3131

32-
/**
33-
* The epoch date used by Google Sheets.
34-
*/
35-
var SHEETS_EPOCH = '12/30/1899 0:00:00';
36-
37-
/**
38-
* The number of seconds in a day.
39-
*/
40-
var SECONDS_PER_DAY = 86400;
41-
4232
/**
4333
* Runs when the add-on is installed.
4434
*/
@@ -65,7 +55,7 @@ function use() {
6555
var title = 'Date Custom Functions';
6656
var message = 'The functions DATEADD and DATESUBTRACT are now available in ' +
6757
'this spreadsheet. More information is available in the function help ' +
68-
'box that appears when you start using them in a forumula.';
58+
'box that appears when you start using them in a formula.';
6959
var ui = SpreadsheetApp.getUi();
7060
ui.alert(title, message, ui.ButtonSet.OK);
7161
}
@@ -82,7 +72,6 @@ function use() {
8272
* @customFunction
8373
*/
8474
function DATEADD(date, unit, amount) {
85-
date = normalizeDate(date);
8675
validateParameters(date, unit, amount);
8776
return moment(date).add(unit, amount).toDate();
8877
}
@@ -99,30 +88,13 @@ function DATEADD(date, unit, amount) {
9988
* @customFunction
10089
*/
10190
function DATESUBTRACT(date, unit, amount) {
102-
date = normalizeDate(date);
10391
validateParameters(date, unit, amount);
10492
return moment(date).subtract(unit, amount).toDate();
10593
}
10694

107-
/**
108-
* Normalizes a date value from Google Sheets, as they can sometimes be passed
109-
* as number values.
110-
* @param {?} date The date value.
111-
* @return {Date} The normalized date value.
112-
*/
113-
function normalizeDate(date) {
114-
if (typeof date == 'number') {
115-
var days = Math.floor(date);
116-
var seconds = (date - days) * SECONDS_PER_DAY;
117-
date = moment(SHEETS_EPOCH).add(days, 'days')
118-
.add(seconds, 'seconds').toDate();
119-
}
120-
return date;
121-
}
122-
12395
/**
12496
* Validates that the date, unit, and amount supplied are compatible with
125-
* Momnent, throwing an exception if any of the parameters are invalid.
97+
* Moment, throwing an exception if any of the parameters are invalid.
12698
* @param {Date} date The date to add to or subtract from.
12799
* @param {string} unit The unit of time to add/subtract.
128100
* @param {number} amount The amount of the specified unit to add/subtract.

0 commit comments

Comments
 (0)