samedi 27 décembre 2014

Is there a bug in Google Spreadsheet object wrapper sent to Google Apps Script?


Attached is a Test Sheet which shows results and expected results. The Test sheet has all of the functions needed to run the sheet.


The main function is supposed to return a range of dates generated from a starting date. Each date should be three months ahead of its preceding date.


The test uses two versions of the function, one using UTC date methods on a date object, the other using normal date methods on a date object.


The sheet named quarter in the "workbook" has the results. Sheet processing tests relies on these Named Ranges: StartDateQ, StartDateWeird .


Anyone who seeks to solve this need only look at the sheet named quarter, the scripts page holding the functions and have working knowledge of Google Apps Script and the Google Spreadsheet API.


The Problem


The array of returned dates are not as expected. In some cases, the dates look right but some of the dates have unwanted time data that can be seen only by selecting a cell. In some cases, the dates are not right, being off by a day.


Why is this a Problem?


In an actual spreadsheet, formulas that rely on date matching fail since some of the visual dates have associated actual time data. Since the time data doesn't match, even though visually, the dates look the same, matching fails. Matching failures break the sheet.


The Algorithm



  1. If there is at least a start date to process, get the number of quarters to generate dates using countAge().

  2. Initialize date object with start date sent from spreadsheet.

  3. Make an empty array to hold dates to return to the spreadsheet.

  4. For the tally of quarters, loop through one count at a time.

  5. Insert the latest date to the array.

  6. Move the date along to the next quarter until there are no more quarters for which to generate a date.

  7. Return the results to the spreadsheet


Functions in Use (internal API)


is(obj, type) //compare if object is the right type


countAge(dateString,dateString2) //count years between dates, aka age


TEST VERSIONS


quartersUTC(startDate,endDate) // custom function to get quarter dates from two dates — UTC Version


quartersPlain(startDate,endDate) // custom function to get quarter dates from two dates — Plain Version


COMMENTARY


Since neither test function relies on "date math", but instead relies on date setters, e.g.,


not this:


d = new Date(); d.setMonth(d.getMonth()) + 3;


but this: if (latestDate.getMonth() == 0) { latestDate.setMonth(3,1) }


it does not seem obvious why the programming logic fails to produce the expected results.





Aucun commentaire:

Enregistrer un commentaire