The simplest way of check if a data type of an ele

2019-08-10 22:44发布

问题:

I want to create a condition that checks if a data type of an element in an array (formed by data from a spreadsheet) is a date object (so, I can manipulate the string format of this date, because I don't want a date like this: Thu May 23 2013 00:00:00 GMT-0400 (EDT) but like this: 23/05/2013).

I get the date from the google spreadsheet using this function:

function getRowAsArray(sheet, row) {
  var dataRange = sheet.getRange(row, 1, 1, 99);
  var data = dataRange.getValues();
  var columns = [];
  for (i in data) {
    var row = data[i];
    Logger.log("Got row", row);
    for(var l=0; l<16; l++) {
        var col = row[l];
        columns.push(col);
    }
  }
  return columns;
}

Supose my data type object can be in one of the data[i] array elements. What is simplest way of doing it?

var text = data[i];

Supose my data type object can be in one of the data[i] array elements. What is simplest way of doing it? It was sugested in one awnswer to do something like this

 var data = getRowAsArray(sheet, sheet);
    for(var i=0; i<columns.length; i++) {
    var key = ":" + columns[i] + ":";

    if (data[i] instanceof Date) {
        var d = data[i]; //date from data[i]
        var m = d.getMonth() + 1; //months starts at 0
        var y = d.getFullYear();
        var day = d.getDate();
        data[i] = day + "/" + m + "/" +y;
    }          

Thanks for any help!

回答1:

Don't forget GAS has a dedicated method to format dates:

Utilities.format date(date,TZ,string representation);

You can use it to customize the way you show dates.

This approach has a few advantages over the string manipulation suggested in the other answer :

  • it takes the time zone into account
  • it can use richer formats (like month in text format)
  • it's shorter to write ;-)

Here is an example function to illustrate (read comments in code, I took the time zone parameter in the spreadsheet itself so we're sure that no error can come from this parameter)

function convertDatesToStrings(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = ss.getDataRange().getValues();
  for(n=0 ; n<data.length;++n){
    var row = data[n];
    for(var i=0; i<row.length; i++) {
      if (row[i] instanceof Date) {
        data[n][i] = "'"+Utilities.formatDate(row[i], SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "dd/MM/yy");// is a string
        // added a ' before the string to ensure the spreadsheet won't convert it back automatically
        Logger.log('Array cell '+n+','+i+' modified to '+data[n][i])
      }          
    }
  }
  ss.getRange(1,1,data.length,data[0].length).setValues(data);// write back to sheet
}


回答2:

You can use the instanceof keyword.

if (data[i] instanceof Date) {
    //it's a date
} else {
   //it's not
}

To format the date, you can do it like:

var d = new Date(), //date from data[i]
    m = d.getMonth() + 1, //months starts at 0
    y = d.getFullYear(),
    d = d.getDate();

console.log([d, m, y].join('/'));


回答3:

Here's a simple custom date validation function:

function isDate(date) {
    return (date.constructor.name == 'Date');
}