8

I am pretty new to Google Apps Script, so please bear with me.

I am collecting daily interest rates from a bank on Google Sheets, and I am using the following code to append new rows for the rates contained in A5:F5, with column A containing dates.

function recordHistory() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Interest Rates");
  var source = sheet.getRange("A5:F5");
  var values = source.getValues();
  values[0][0] = Utilities.formatDate(new Date(), "GMT+10:00", "yyyy-MM-dd");
  sheet.appendRow(values[0]);

};

My issue is this - although I have specified the date format to be "yyyy-MM-dd" the dates in column A in my new rows are created in this format "M/dd/yyyy".

I have tried pre-formatting entire column A with "yyyy-MM-dd" using the drop down Format menu in Google Sheets, but if I run the code above my new row is still in "M/dd/yyyy".

It's as if my code ignores Utilities.formatDate completely. FYI I got the above code from here.

1
  • Removed that part, cheers Commented Jun 2, 2015 at 23:34

2 Answers 2

15

The Utilities.formatDate() utility formats a javascript String. However, when written out to the spreadsheet, the new row of data is interpreted by Google Sheets to determine data types and appropriate formatting, just as if you'd typed it in through the user interface.

Since you've got a recognizable date string, Google Sheets decides it's a Date, stores it as such, and applies the default date format.

You've got two options for making the date in the spreadsheet meet your formatting needs.

  1. Force it to be interpreted as a String, even if it does look like a date.

    cell.setValue(Utilities.formatDate(new Date(), "GMT+10:00", "''yyyy-MM-dd"));
    //                                                           ^^ force string literal
    
  2. Set the date format for the cell. This will leave the value of the cell as a date, which is useful for calculations.

    Date formats follow the SimpleDateFormat specification.

    // Cell A1 contains a date
    var cell = SpreadsheetApp.getActiveSheet().getRange("A1");
    cell.setNumberFormat('yyyy-mm-dd');
    
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you and apologies for the delay in getting back to the forum. I would prefer the second solution but unfortunately it doesn't work, I am still getting a new row with the date in the form of 'code "M/dd/yyyy"'. I have edited the OP with the updated code.
You didn't change your code according to the explanation and advice. When you call appendRow, the new ddTa gets interpreted as a date. You need to adjust the format AFTER the row is added.
Yea I didn't understand when to insert the code - but all good now, I inserted the code after appenRow with getRange("A9:A") so that it formats column A starting from cell A9, and it seems to do the trick. Thank you
0

Solution :

values[0][0] = Utilities.formatDate(new Date(), 
"GMT+10:00", "yyyy-MM-dd").setNumberFormat('yyyy-mm-dd');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.