简体   繁体   中英

Google-Apps-Script - convert HH:MM argument to fraction of a day

I have '12:30' in a cell in my Google sheet. It is passed in as an argument into my function (Google Apps Script) and I want to be able to extract the value 12 and the value 30. I want to then be able to return 0.5+0.0208333 = 0.5208333 to represent the fraction of the day that 12:30 represents. I have searched and not been able to find a solution to this simple problem. When I change the format to 'number' on the Google sheet it is passed correctly as a float 0.520833 but that defeats the purpose of being able to allow the user to enter a 'HH:MM' format value on the Google sheet user interface. Can someone tell me how, specifically within Google Apps Script I can extract the HH and the MM passed in as an 'HH:MM' argument.

  • Select the column(say A:A) in Google sheets and format it as HH:MM . This will enable users to enter in HH:MM format.
  • Use apps-script to change the number format onEdit

Sample Script:

function onEdit(e){
 if (e.range.columnStart != 1) return; //A:A
 e.range.setNumberFormat('#.######');
}

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