1

I have an array which has a date in the 3rd column, and I wrote the following script to calculate the Friday on or after that day. The script works (after a little trouble) but unfortunately while it adds the new column, it also overwrites column 3 with the Friday date.

Any advise would be much appreciated.

  Logger.log("Started Week Ending");
  newData[0].push("Week Ending");

  for(i = 1; i < newData.length; i++) {
    l = newData[i][2] ;
    for(;l.getDay() != 5;) {
      l.setDate(l.getDate()+1);
    } 
    newData[i].push(l);
  }

Basically I want the variable "lowercase L" to change value but not the source of its value newData[i][2]. Any idea what I am doing wrong?

I first posted this in Google Docs Forum and was advised I might find more help here. Since posting, I tried a variation of this part of the code instead directly filling the array and manipulating the new column value instead of an intermediate variable. The code for this is below:

  Logger.log("Started Week Ending");
  newData[0].push("Week Ending");

  for(i = 1; i < newData.length; i++) {
    newData[i].push(newData[i][2]) ;
    for(;newData[i][20].getDay() != 5;) {
      newData[i][20].setDate(newData[i][20].getDate()+1);
    } 
  }

Thank you for looking! Eric

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • 1
    You don't show all your code... I bet you have a `setValues()` just after the snippet you've shared. The date in column 3 is changing because of the way you are manipulating it with `setDate()`. The statement `l = newData[i][2] ` doesn't create a new Date object in `l`, rather it lets `l` access (and modify) `newData[i][2] `, in place. I suspect the second approach has a similar problem - you don't say how it behaves. – Mogsdad Jun 04 '15 at 04:05
  • I think this maight be helpful: http://stackoverflow.com/questions/18359093/how-to-copy-javascript-object-to-new-variable-not-by-reference – bksi Jun 04 '15 at 04:12

0 Answers0