Question

I have a spread sheet where I enter racing times, ranking is calculated automatically. Problem is, Spreadsheets cant read the Format mm:ss,xxx (x stands for milliseconds here)

And when I am using colons like -> 02:02:333 It will think its 2 hours, 2 minutes and 333 seconds (what I wanted was 2minutes, 2 seconds and 333 milliseconds), which will convert it to 02:07:33

Any Solution that would make me enter "02:02,333" and use it as a value?

any help appreciated! Tobias

Was it helpful?

Solution

Update: given the sheet he shared, the easier way would be simply add a column and insert the following function =mid(E2,4,2)*60+mid(E2,4,2)+mid(E6,7,3)*.001 where E2 is the cell containing the time in mm:ss,SSSformat. Then rank based on that columns value.

My orginal answer: here is a script that will process the highlighted cells, and convert your time format to milliseconds and place the result in the adjacent cell. Since I don't know what you want to do with the values, you could do what you want with the returned value. The conversion assumes you only care about hours/minutes/seconds and not the date part but if you look at the code you can see you could pass it any format you wanted and since you get a date object back, you could handle that any way you want. This basically copied Matt Kruse's date function from HERE just modifying it to zero out the time portion (optionally) and to add milliseconds to the tokenizer.

// ------------------------------------------------------------------
// Utility functions for parsing 
// ------------------------------------------------------------------
function _isInteger(val) {
    var digits="1234567890";
    for (var i=0; i < val.length; i++) {
        if (digits.indexOf(val.charAt(i))==-1) { return false; }
        }
    return true;
    }
function _getInt(str,i,minlength,maxlength) {
    for (var x=maxlength; x>=minlength; x--) {
        var token=str.substring(i,i+x);
        if (token.length < minlength) { return null; }
        if (_isInteger(token)) { return token; }
        }
    return null;
    }


// ------------------------------------------------------------------
// getDateFromFormat( date_string , format_string, zeroTimes )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the 
// getTime() of the date. If it does not match, it returns 0. 
// ------------------------------------------------------------------
function getDateFromFormat(val,format, zeroTimes) {
    val=val+"";
    format=format+"";
    var i_val=0;
    var i_format=0;
    var c="";
    var token="";
    var token2="";
    var x,y;
    var now=new Date();
    var year=now.getYear();
    var month=now.getMonth()+1;
    var date=now.getDate();
    var hh=now.getHours();
    var mm=now.getMinutes();
    var ss=now.getSeconds();
    var sss=now.getMilliseconds();
  if(zeroTimes){
    hh=0;
     mm=0;
     ss=0;
     sss=0;
  }
    var ampm="";
  Logger.log(" getDateFromFormat(" + val +","+format+")");
    while (i_format < format.length) {
        // Get next token from format string
        c=format.charAt(i_format);
        token="";
        while ((format.charAt(i_format)==c) && (i_format < format.length)) {
            token += format.charAt(i_format++);
            }
        // Extract contents of value based on format token
        if (token=="yyyy" || token=="yy" || token=="y") {
            if (token=="yyyy") { x=4;y=4; }
            if (token=="yy")   { x=2;y=2; }
            if (token=="y")    { x=2;y=4; }
            year=_getInt(val,i_val,x,y);
            if (year==null) { return 0; }
            i_val += year.length;
            if (year.length==2) {
                if (year > 70) { year=1900+(year-0); }
                else { year=2000+(year-0); }
                }
            }
        else if (token=="MMM"||token=="NNN"){
            month=0;
            for (var i=0; i<MONTH_NAMES.length; i++) {
                var month_name=MONTH_NAMES[i];
                if (val.substring(i_val,i_val+month_name.length).toLowerCase()==month_name.toLowerCase()) {
                    if (token=="MMM"||(token=="NNN"&&i>11)) {
                        month=i+1;
                        if (month>12) { month -= 12; }
                        i_val += month_name.length;
                        break;
                        }
                    }
                }
            if ((month < 1)||(month>12)){return 0;}
            }
        else if (token=="EE"||token=="E"){
            for (var i=0; i<DAY_NAMES.length; i++) {
                var day_name=DAY_NAMES[i];
                if (val.substring(i_val,i_val+day_name.length).toLowerCase()==day_name.toLowerCase()) {
                    i_val += day_name.length;
                    break;
                    }
                }
            }
        else if (token=="MM"||token=="M") {
            month=_getInt(val,i_val,token.length,2);
            if(month==null||(month<1)||(month>12)){return 0;}
            i_val+=month.length;}
        else if (token=="SSS"||token=="SS"||token=="S") {
            sss=_getInt(val,i_val,token.length,3);
            if(sss==null||(sss<0)){return 0;}
            i_val+=sss.length;
       Logger.log("Milli: " + sss);
      }
        else if (token=="dd"||token=="d") {
            date=_getInt(val,i_val,token.length,2);
            if(date==null||(date<1)||(date>31)){return 0;}
            i_val+=date.length;}
        else if (token=="hh"||token=="h") {
            hh=_getInt(val,i_val,token.length,2);
            if(hh==null||(hh<1)||(hh>12)){return 0;}
            i_val+=hh.length;}
        else if (token=="HH"||token=="H") {
            hh=_getInt(val,i_val,token.length,2);
            if(hh==null||(hh<0)||(hh>23)){return 0;}
            i_val+=hh.length;}
        else if (token=="KK"||token=="K") {
            hh=_getInt(val,i_val,token.length,2);
            if(hh==null||(hh<0)||(hh>11)){return 0;}
            i_val+=hh.length;}
        else if (token=="kk"||token=="k") {
            hh=_getInt(val,i_val,token.length,2);
            if(hh==null||(hh<1)||(hh>24)){return 0;}
            i_val+=hh.length;hh--;}
        else if (token=="mm"||token=="m") {
            mm=_getInt(val,i_val,token.length,2);
            if(mm==null||(mm<0)||(mm>59)){return 0;}
            i_val+=mm.length;}
        else if (token=="ss"||token=="s") {
            ss=_getInt(val,i_val,token.length,2);
            if(ss==null||(ss<0)||(ss>59)){return 0;}
            i_val+=ss.length;
          Logger.log("Seconds: " + ss);
        }

        else if (token=="a") {
            if (val.substring(i_val,i_val+2).toLowerCase()=="am") {ampm="AM";}
            else if (val.substring(i_val,i_val+2).toLowerCase()=="pm") {ampm="PM";}
            else {return 0;}
            i_val+=2;}
        else {
            if (val.substring(i_val,i_val+token.length)!=token) {return 0;}
            else {i_val+=token.length;}
            }
        }
   Logger.log("Year: "+year+" Month: " +month+ " date " +date+ " hour "  +hh+ " minutes "+mm+ " seconds " +ss+"  milli " +sss);

    // If there are any trailing characters left in the value, it doesn't match
    if (i_val != val.length) { return 0; }
    // Is date valid for month?
    if (month==2) {
        // Check for leap year
        if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year
            if (date > 29){ return 0; }
            }
        else { if (date > 28) { return 0; } }
        }
    if ((month==4)||(month==6)||(month==9)||(month==11)) {
        if (date > 30) { return 0; }
        }
    // Correct hours value
    if (hh<12 && ampm=="PM") { hh=hh-0+12; }
    else if (hh>11 && ampm=="AM") { hh-=12; }
    var newdate=new Date(year,month-1,date,hh,mm,ss);
    newdate.setMilliseconds(sss);
    return newdate;
    }

/**
 */
function formatSelection() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  var dayMillis = 24*3600*1000;  //miliseconds in a day
  var range = SpreadsheetApp.getActiveSheet().getActiveRange();
  var numRows = range.getNumRows();
  var numCols = range.getNumColumns();
  for (var i = 1; i <= numRows; i++) {
    for (var j = 1; j <= numCols; j++) {
      var currentValue = range.getCell(i,j).getValue();
      Logger.log("Current Value: "+ currentValue);
      var newDate=new Date();
      newDate=getDateFromFormat(currentValue,"mm:ss,SSS",true);
      var convertedNewDate=parseInt(newDate/dayMillis);
      Logger.log(" New Date: " + newDate + " Converted Value: " + convertedNewDate);
      var newTime=newDate.getHours()*1000*60*60+newDate.getMinutes()*1000*60+newDate.getSeconds()*1000+newDate.getMilliseconds();
      SpreadsheetApp.getActiveSheet().getRange(range.getCell(i,j).getRow(),range.getCell(i,j).getColumn()+1).setValue("Milliseconds: "+ newTime);
      //range.getCell(i,j).setValue(convertedNewDate);
      //.setNumberFormat("mm:ss,SSS");

    }
  }


};

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "Format as Value",
    functionName : "formatSelection"
  }];
  sheet.addMenu("Time Functions", entries);
};

OTHER TIPS

=if(B4>=60,trunc(divide(B4,60),0)&":"&trunc(mod(B4,60),0)&":"&round(mod(B4,1)*100,0),trunc(mod(B4,60),0)&":"&round(mod(B4,1)*100,0))

Cell B4 = 137.35 (137.35 seconds)

Displays as 2:17:35 (2 minutes, 17 seconds, 35 hundreds-seconds)

Cell B4 = 27.73 (27.73 seconds)

Displays as 27:73 (27 seconds, 73 hundreds-seconds)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top