Tuesday, 4 June 2024

Correct date formats in a Google Sheet

The following Google Apps Script is just one way you could search through data within a Google Sheet, pinpoint all date-values (no matter where they are), check their format (e.g. dd/mm/yyyy) and update it to a format of your choosing if necessary.

This function has been useful when American/British date formats have managed to make their way into a Google Sheet.

Screenshot showing difference between American/British date formats
Screenshot showing difference between American/British date formats


The Code

There are a few bits of code to tease out and explore, starting with us getting all of the Sheets within the file so that we can eventually loop through each of them in-turn:
var allSheets = ss.getSheets();
var allSheetsLength = allSheets.length;

There are 3 For Loops to juggle in this function: one to loop through each Sheet tab, one to loop through each row of data and one to loop through each column of data. We will also be using the 'getNumberFormats()' method in order to determine dates from other format-types (e.g. numbers, formulas).

We need to be able to test the format of each cell of data we loop through in order to ensure we only target date values. The following will return a value of true if a date is found:
var dateCheck = Object.prototype.toString.call(singleValue) === '[object Date]';

We can then compare this with our desired format to know if we need to take any action:
if (cellFormat != "dd/mm/yyyy") {


Set our desired format:

sheet.getRange(row, column).setNumberFormat("dd/mm/yyyy");



Download

Correct date formats in a Google Sheet download (please use 'File' > 'Make a copy' for your own version).


2 comments:

  1. Cool utility! And nice looking blog! I wanted to ask why you didn't use an array function instead of a for loop? I'm still getting comfortable with them myself but from what I understand they are far superior in terms of performance and with the new arrow functions much smaller code to write?
    here's a stack overflow solution thats offered that is pretty cool ( it's the one suggested as an alternative to the accepted one ) https://stackoverflow.com/questions/13605213/iterate-over-range-append-string-to-each

    ReplyDelete
    Replies
    1. Thank you.

      Yes there are always multiple ways of achieving a task with Apps Script, thank you for sharing the link with an alternative I will definitely bear this in mind for the future.

      In this instance a For Loop is more universally understood I find, when helping people learn Apps Script coding. I also find working with dates a real pain so I wanted that to be the focus of this post and keep everything else as straightforward as possible.

      You are correct about the performance aspect, but only when you scale up to thousands of pieces of data and large Google Sheets do you really notice any difference, which wasn't quite applicable to me here but I fully note your point.

      Delete