2013-11-23 126 views
2

我有一张电子表格,可以输入比赛时间,排名会自动计算。 问题是,电子表格不能读取的格式MM:SS,XXX(x代表在这里毫秒)将单元格格式化为:分,秒,毫秒

,当我使用冒号像 - >02:02:333它会认为它的2小时2分钟和333秒(我想要的是2分钟,2秒333毫秒),将其转换为2时07分33秒

任何解决方案,这将使我进入“02:02333”,并把它作为一个值?

任何帮助赞赏! 托拜厄斯

+0

当你说“用它作为一个值”时,你想如何使用它?您可以编写一个脚本,将脚本转换为毫秒或日期时间对象,然后在脚本中对其执行操作。或者你想在电子表格中对它做些什么,比如将它与另一列进行比较? – jkhouw1

+0

好吧,我试过这个脚本,但它仍然不会改变格式。我现在试着更加自信,这里是链接:(https://docs.google.com/spreadsheet/ccc?key=0AveLEDuTZv0vdE1xQS0zRVNuOHJnNmZIYjdhajlHelE#gid=0)我使用Rank函数。所以当我用毫秒输入时间时(如第6行),我得到一个**#Value **错误。我如何完成这项工作? – TobiasH

+0

请参阅:https://docs.google.com/spreadsheet/ccc?key = 0AryEGrzz5ZREdFVOcDdfSXhhVExXQmNXSVR4RW40c3c#gid = 0我做了一些更改以帮助您开始 - 您指定了02:02,333作为格式,但之后您使用了2:02,333将打破我的脚本,所以使用前导零。我添加了一列来保存你可以隐藏的毫秒数,然后根据这个数值排列col G而不是e。所以在col E中输入你的时间,突出显示它,然后使用时间函数以毫秒为单位进行计算。顺便说一句:如果你是一致的,你可以使用字符串函数来完成相同的事情 – jkhouw1

回答

4

更新:给出他共享的片材中,更简单的方法将被简单地添加一列并插入以下功能=mid(E2,4,2)*60+mid(E2,4,2)+mid(E6,7,3)*.001其中E2是包含在毫米时的细胞:SS,SSSformat。然后根据该列值进行排名。

我的原单回答:这里是一个脚本,将处理突出的单元格,和你的时间格式转换为毫秒,结果放在相邻的单元格。由于我不知道你想要对值做什么,你可以用返回的值做你想要的。该转换假定您只关心小时/分钟/秒而不是日期部分,但如果您查看代码,则可以看到您可以将其传递给您想要的任何格式,并且由于您获取了日期对象,因此您可以以任何方式处理你要。这基本上复制了Matt Kruse的日期函数HERE,只是修改它以将时间部分归零(可选)并向分词器添加毫秒。

// ------------------------------------------------------------------ 
// 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); 
}; 
+1

+1给他一个在电子表格中使用的菜单:D –

相关问题