1

I'm trying to take information from Google Sheets and use it to send an email with the information given in sheets. I have the email part down, but when I try to editAsText to delete unwanted characters it won't allow me to. Here is what I am trying to accomplish:

var start = row[5];
returns: Fri Feb 20 2015 00:00:00 GMT+0200 (SAST)

and I want to edit that to be:

Fri Feb 20 2015
  • Which answer of Vidar did you accept? The first or the one that he added later? – Jacob Jan Tuinstra Feb 9 '15 at 14:03
  • Okay i found root of my problem there were some issues with the form that is linked to the spreadsheet, I got it working now thanks.. What was the root of the problem Daniel? We might learn from it. – Jacob Jan Tuinstra Feb 9 '15 at 17:06
0

You're basically wanting to format a date.

I would act on the JavaScript Date object, something like:

var startDate = new Date(row[5]);
var weekDay;
switch (startDate.getDay()) {
  case 0: weekDay = "Sun"; break;
  case 1: weekDay = "Mon"; break;
  case 2: weekDay = "Tue"; break;
  case 3: weekDay = "Wed"; break;
  case 4: weekDay = "Thu"; break;
  case 5: weekDay = "Fri"; break;
  case 6: weekDay = "Sat"; break;
}
var month;
switch (startDate.getMonth()) {
  case 0: month = "Jan"; break;
  case 1: month = "Feb"; break;
  case 2: month = "Mar"; break;
  case 3: month = "Apr"; break;
  case 4: month = "May"; break;
  case 5: month = "Jun"; break;
  case 6: month = "Jul"; break;
  case 7: month = "Aug"; break;
  case 8: month = "Sep"; break;
  case 9: month = "Oct"; break;
  case 10: month = "Nov"; break;
  case 11: month = "Dec"; break;
}

var formattedDate = weekDay + " " + month + " " + startDate.getDate() + " " + startDate.getFullYear();

The variable formattedDate now holds a string like Fri Feb 20 2015.

If you want to do this by manipulating strings instead, and you're not concerned with the language settings of the spreadsheet, you could use

var startDate = row[5] + "";
var formattedDate = startDate.substring(0, 15);
  • I can try that, thanks but my question still remains... can I not accomplish this in terms of string handling? – FFDP-FTW Feb 9 '15 at 10:30
  • You say you're trying to call editAsText, but as far as I can see, that method is only available in Google Documents - not Spreadsheets. – Vidar S. Ramdal Feb 9 '15 at 10:48
  • Yea i realised recently... haha. So there is no way to use string handling from the info i get from a spread sheet? – FFDP-FTW Feb 9 '15 at 12:30
  • You can make it a string, as with row[5] + "". Then you're free to extract substrings from it. See the update to my answer. – Vidar S. Ramdal Feb 9 '15 at 12:39
  • Yes I have done this already and still doesn't seem to work... but I'm starting to thing there is a logic error somewhere else so I'm going to run through the code again to check... – FFDP-FTW Feb 9 '15 at 13:16
1

Have you tried the .toDateString() method ? See what this gives you:

var start = row[5];
var dateStr = start.toDateString();
Logger.log(dateStr)
  • Haven't tried this yet ... will do so now, then use the dateStr var to edit the string... – FFDP-FTW Feb 9 '15 at 12:33
  • Just by using .toDateString() the output should be somehting like Fri Feb 20 2015. – JPV Feb 9 '15 at 13:45
0

Perhaps you can format it the way you want by using this in the first place.

Code

var start = row[5]; // "Fri Feb 20 2015 00:00:00 GMT+0200 (SAST)"    
var newStart = Utilities.formatDate(new Date(start), "SAST", "EEE MMM d yyyy");
Logger.log(newStart);

Screenshot

enter image description here

  • tried this and it threw an object error of some sort... – FFDP-FTW Feb 9 '15 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.