简体   繁体   中英

How do I calculate duration in google apps script from sheets?

Thanks for opening my question. What I'm doing, to me, should be very simple. I am a beginner for programming so I am not aware of what I need to get this done. I need help.

The problem: I have to have 4 columns for times. (Travelto, Arrive, Depart, Travelfrom) I don't always use all of them so my script has to recognize that I want certain values based on which cells in a row are blank or which have content. I have tried using isblank() on the spreadsheet to determine a binary number which I then convert to a decimal. I'd like my script to do that so I don't have to add another column to my google sheets. I think I would use an array and then check if each element is blank in the array then multiply each element in that array by 1 so it's now a number instead of a boolean. Then I want to take the elements of the array and convert them into a single binary number and convert that to a decimal number to feed to my switch case, which will contain the correct way to calculate the hours and return the hours in decimal so it should be formated such as 1.75 for 1 hr 45 mins. The value it returns must be able to be summed so the function can't return a string. also I prefer 2 decimal places for the output.

I have attempted to figure out how to calculate the time in google's apps Script. I have had limited success. The output of my script is unintelligible as to how it got the answer it did. This is probably because I can't figure out how to tell what the script sees the times as. does it see 13:00:00, 0.5416667, or something completely different? I can't seem to figure it out.

I want to pass two values from a google sheets spreadsheet, which are visually formatted as time, then take those two times subtract one from the other and get the amount of time between them, the duration so that I know how many hours have been worked.

function worked(time1,time2)       //pass 2 time values to function
{                                  //Start of the function
var time1;                         //declare time1 variable
var time2;                         //Declare time 2 variable
var outnumber = time1-time2;       //Declare outnumber and subtract time1 from time2
return outnumber                 //return the difference of time1 and time2
}

here's the link to my sheet and code included in the editor. anyone with the link can comment https://docs.google.com/spreadsheet/ccc?key=0Ar4A89ZoxmJCdHBFR0VCblVtWUVvR3hFbTdlcjdKNUE&usp=sharing

Please tell me what I'm doing wrong or not doing at all to make this work. Thanks Goldenvocals369

The number you are seeing outputted is the difference in ms. You need to convert ms to the format you want.

I found a neat way to do that here: https://coderwall.com/p/wkdefg

Your code would look like this.

function worked(time1,time2)      
{                                 
var time1;
var time2;
var outnumber = time1-time2;  
  return msToTime(outnumber) 
}

function msToTime(duration) {
    var milliseconds = parseInt((duration%1000)/100)
        , seconds = parseInt((duration/1000)%60)
        , minutes = parseInt((duration/(1000*60))%60)
        , hours = parseInt((duration/(1000*60*60))%24);

    hours = (hours < 10) ? "0" + hours : hours;
    minutes = (minutes < 10) ? "0" + minutes : minutes;
    seconds = (seconds < 10) ? "0" + seconds : seconds;

    return hours + ":" + minutes + ":" + seconds;
}

You can use this function. The function's documentation is:

Returns the displayed value of the top-left cell in the range. The value will be of type String. The displayed value takes into account date, time and currency formatting formatting, including formats applied automatically by the spreadsheet's locale setting. Empty cells will return an empty string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM