Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9632

Format dates in Google spreadsheets script?

$
0
0

First of all,please excuse me for my bad English.

I am splitting multi-line cell into different rows

These are the scripts I use

// Main of this script.
function result(range) {
  var output = [];
  for (var i in range) {
    var celLen = 1;
    var c1 = range[i].map(function(e, i){
      var cell = e.toString().split("\n"); // Modified
      var len = cell.length;
      if (len == 1) {
        return cell[0];
      } else if (len > 1) {
        celLen = celLen > len ? celLen : len;
        var t2 = [];
        for (var k=0; k<cell.length; k++) {
          t2.push(cell[k]);
        }
        return t2;
      }
    });
    var c2 = c1.map(function(e, i){
      var r = [];
      if (!Array.isArray(e)) {
        for (var k=0; k<celLen; k++) {
          r.push(e);
        }
      } else {
        for (var k in e) {
          r.push(e[k]);
        }
        if (e.length < celLen) {
          for (var m=0; m<celLen - e.length; m++) {
            r.push("");
          }
        }
      }
      return r;
    });
    var c3 = c2[0].map(function(e, i){return c2.map(function(f, j){return c2[j][i]})});
    Array.prototype.push.apply(output, c3);
  }
  return output;
}

// For testing this script.
function main() {
  var ss = SpreadsheetApp.getActiveSheet();
  var data = ss.getDataRange().getValues();
  var r = result(data);
  ss.getRange(ss.getLastRow() + 1, 1, r.length, r[0].length).setValues(r);
}
//This can be seen at c1 in the script.
//Create each cell using parsed cells.
//This can be seen at c2 in the script.
//Create output cells.
//This can be seen at c3 in the script.

The original date looks like this:

2020/1/20 PM 6:51:12

But the end result of the date format is like this:

Mon Jan 20 2020 18:51:12 GMT+0800 (HKT)

I tried many methods, but I still can't keep the date format unchanged. Is there a more elegant way to solve the problem?

https://docs.google.com/spreadsheets/d/189VXy-OYy-Ncog6fnjtJ9vNR7zK-q0A9PNZioLRp9k8/edit?usp=sharing


Viewing all articles
Browse latest Browse all 9632

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>