[英]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. 问题:我必须有4列次。 (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.
(Travelto,Arrive,Depart,Travelfrom)我并不总是使用所有这些,所以我的脚本必须认识到我想要基于连续的哪些单元格是空白的或具有内容的某些值。 I have tried using isblank() on the spreadsheet to determine a binary number which I then convert to a decimal.
我已经尝试在电子表格上使用isblank()来确定二进制数,然后我将其转换为小数。 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.
我想我会使用一个数组,然后检查数组中的每个元素是否为空,然后将该数组中的每个元素乘以1,这样它现在是一个数字而不是一个布尔值。 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.
然后我想获取数组的元素并将它们转换为单个二进制数并将其转换为十进制数以提供给我的开关案例,其中将包含计算小时数的正确方法并以小数形式返回小时数以便它应该形成如1.75的1小时45分钟。 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.
我也更喜欢输出2位小数。
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?
它看到13:00:00,0.5416667,还是完全不同的东西? 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. 我想从Google工作表电子表格中传递两个值,这些值在视觉上按时间格式化,然后将这两个值从另一个中减去一个并获得它们之间的时间长度,以便我知道已经工作了多少小时。
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
任何拥有该链接的人都可以发表评论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
谢谢Goldenvocals369
The number you are seeing outputted is the difference in ms. 您看到输出的数字是以ms为单位的差异。 You need to convert ms to the format you want.
您需要将ms转换为所需的格式。
I found a neat way to do that here: https://coderwall.com/p/wkdefg 我在这里找到了一个简洁的方法: 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.
该值的类型为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.
空单元格将返回一个空字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.