Log cell value on specific time intervals - google-sheets

The function =GOOGLEFINANCE("NASDAQ:GOOG","PRICE") fetches the realtime price in Google Sheets. So if it is required to log the value of the above function every 15 minutes and reset the log at midnight, is there any way to do so?
Mainly I have to log value of a derived field which uses value of this function.
Required format
| TIME (in 24h format) | Price |

Here is a solution. Put a trigger (15mn) on 'periodic'
function activate(x) {
return x
}
function periodic(){
var sh=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var der=sh.getLastRow()+1
sh.getRange('A'+der).setValue(new Date())
sh.getRange('B'+der).setValue(sh.getRange('A2').getValue())
sh.getRange('B1').setValue(!sh.getRange('B1').getValue())
}
https://docs.google.com/spreadsheets/d/1QM7uI1xsJaCDkyEZ1UvE3knc00gFlgt_BItJQRb9M70/copy

Related

How to format a number as HH:MM in google sheets

I have Data set in google sheets that shows time spent log for a user in SECONDS.
For example,
Name (text) | Time Spent (number)
user A | 86400
How can format the time spent field as 24:00:00
Followed so many format related articles but all I am getting is 2073600:00:00 or similar weird numbers
You need to first divide by 86400, then set Format > Number > Duration. This is because the duration is evaluated in days (a value of 1 is formatted as 24 hours, 0 minutes, 0 seconds).
Example: if cell A1 contains the value of "3600" (seconds), you can display it as a duration in A2 by setting the value of A2 to "=A1/86400", then changing the format of A2 to Duration. A2 will display a duration of 1 hour in the default format.
See also How to format a duration as HH:mm in the new Google sheets for custom formatting of the duration.
try:
=INDEX(TEXT(A2:A5/86400, "[h]:mm:ss"))

Time differance "in mins" using current date and time stamp

How do I work out the duration from the current date and time to cell A1
A B Results
+-----------------+-------------------------------------------------------+--------------+
1 |20-01-07 07:27:27|=TIME(HOUR(NOW()),MINUTE(NOW()),SECOND(NOW()))-C2 | 5:31:57 |
+-----------------+-------------------------------------------------------+--------------+
2 |20-01-07 07:27:27|=TIME(HOUR(NOW()),MINUTE(NOW()),SECOND(NOW()))-C2*1440 | -56307326.9 |
+-----------------+-------------------------------------------------------+--------------+
I have tried this method above but it seems to not use the DATE.
Is there a way I can get it to include the date and also get it to display in minutes
https://docs.google.com/spreadsheets/d/15EqEkdzcPntTV1K0EfOW_BPcq7zFDNqOMNwEFdQuDIE/edit#gid=0
use:
=ARRAYFORMULA(IF(A2:A, TEXT(NOW()-A2:A, "[m]")*1, ))
=now() - A1 will give you the number of days between the date in A1 and now.
The decimal part of the number if a fraction of a day.
So if you need minutes, try the formula :
=(now()-A1)*(24*60)
The output will most likely have decimal places which is the number of seconds as fractional minutes.

How to change Time Zone in Alpha Vantage API

Can I change the time zone in the result from Alpha Vantage API? Here is an example of the output. It's currently in EST. I'd like to get it in IST.
'Meta Data': {
'1. Information': 'Intraday (1min) open, high, low, close prices and volume',
'2. Symbol': 'BSE:------',
'3. Last Refreshed': '2019-11-01',
'4. Output Size': 'Compact',
'5. Time Zone': 'US/Eastern
}
'Time Series (1min)': {
'2019-11-01 00:08:59': {
'1. open': '70.7500',
'2. high': '70.7500',
'3. low': '70.7500',
'4. close': '70.7500',
'5. volume': '0'
},
Welcome to StackOverflow!
Right now the time zone that is returned is the only time zone you will receive from the API. However, if you're pulling the data in with a python script. You can always convert it to your time zone of choice.
from alpha_vantage.timeseries import TimeSeries
from datetime import datetime
import pytz
ts = TimeSeries()
data, meta_data = ts.get_daily('TSLA')
format = '%Y-%m-%d %H:%M:%S'
datetime = datetime.strptime(meta_data['3. Last Refreshed'], format)
old_timezone = pytz.timezone(meta_data['5. Time Zone'])
new_timezone = pytz.timezone("Asia/Calcutta")
# returns datetime in the new timezone
my_timestamp_in_new_timezone = old_timezone.localize(datetime).astimezone(new_timezone)
print(my_timestamp_in_new_timezone.strftime(format))
You could run a method that just converts all the times to whatever timezone you want when you pull the data
ts.get_daily('TSLA') should be changed to ts.get_intraday('TSLA').

How to show 24h time in Google Sheets?

I am creating a google sheet that feeds from a form. I want to capture the timestamp in a separate cell when another cell is changed. I can get it to display the time now but I need it to be in military time.
var timezone = "GMT-4"
var timestamp_format = "hh:mm:ss"; // Timestamp Format.
I expect the output to say 13:15:55, but the actual is 1:15:55.
Google's App Script uses the standard JavaScript Date object when returning forms. Here is how to get what you want from a Date object.
var resultFromForm = ...
var time = resultFromForm.toTimeString().substring(0, 8);
you can just change internal formatting like:

Will multiple Date() value(timestamp) be equal while initializing in same method and same thread?

I'm curious to know that initializing Date() objects in same method and same thread will be equal to orderedSame. I tried it with unit Test Case but It given various results as attached screenshots.
Test case failed at Assert#2:
Test case failed at Assert#4:
Test case Success:
I think, the time components of Date() will be changing based on the code execution time from date1 to date5.
So can we say that the Date() object created at line #2 will be different from the one created in line #1?
You cannot assume anything. Date()
Creates a date value initialized to the current date and time.
Two consecutive Date() calls can return the same value because
the internal clock has a limited resolution,
the date is stored as a TimeInterval aka Double, which has a limited
precision.
Usually, the values would be non-decreasing, but even that need not be the case
because the user can change the time settings of the device.
Each time you call Date() the system fetches the time from it's realtime clock. If any measurable time has passed between that call and the previous call, the dates will be different. Date objects use a Double count of seconds internally, which has a precision of ≈15 decimal digits, so they are able to measure tiny, tiny spans of time (less than a picosecond.) I don't know the precision of the realtime clock on iOS, but it's will likely be in nanoseconds.
Try running this code in release mode on your target device:
let arraySize = 1000
var array = [Date]()
var differences = 0
array.reserveCapacity(arraySize)
for _ in 1...arraySize {
array.append(Date())
}
for index in 0 ..< arraySize-1 {
if array[index] != array[index+1] {
differences += 1
let difference = array[index+1].timeIntervalSinceReferenceDate -
array[index].timeIntervalSinceReferenceDate
print("Dates at index \(index) are different! by \(difference)")
}
}
if differences == 0 {
print("All dates are equal")
} else {
print("\(differences) dates were different out of \(arraySize)")
}
I ran it as a command-line tool on my Mac and found that I always got at least a couple of dates that were different. (Macs run a lot faster than iOS devices)
Modern iOS devices are multi-core, interrupt driven devices, so the results will vary from run to run.
Absolutely each object is different from another object because every individual object has its different address.
When You create date by Date() system requested new timestamp. Depending on whole system load You can get small delay. Date objects are sensetive to miliseconds, so You have no guarantee that objects will be created in same milisecond. To check this You can check timeIntervalSince1970 of each date (this method will return Unix timestamp.

Resources