简体   繁体   English

Google-Apps-Script - 将 HH:MM 参数转换为一天的一小部分

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

I have '12:30' in a cell in my Google sheet.我的 Google 表格的单元格中有“12:30”。 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.它作为参数传入我的函数(Google Apps 脚本),我希望能够提取值 12 和值 30。然后我希望能够返回 0.5+0.0208333 = 0.5208333 来表示12:30 代表的一天。 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.当我在 Google 工作表上将格式更改为“数字”时,它会作为浮点数 0.520833 正确传递,但这违背了允许用户在 Google 工作表用户界面上输入“HH:MM”格式值的目的。 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.有人可以告诉我如何,特别是在 Google Apps Script 中,我可以提取作为“HH:MM”参数传入的 HH 和 MM。

  • Select the column(say A:A) in Google sheets and format it as HH:MM .在 Google 表格中选择列(比如 A:A)并将其格式化为HH:MM This will enable users to enter in HH:MM format.这将使用户能够以HH:MM格式输入。
  • Use apps-script to change the number format onEdit使用 apps-script 更改数字格式 onEdit

Sample Script:示例脚本:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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