Export Google Fit Daily Steps, Weight and Distance to a Google Sheet

Updated on Friday, December 29, 2023

Google Fit Daily Step Export

Google Fit is a great way to keep track of your daily step count without needing to carry a Fitbit or other dedicated tracker. It's not easy to get that data out though, as far as I can tell the only way is Google Takeout which is not made for automation. Luckily there is an API and you can do almost anything with Google Sheets.

If you're looking to export your step count, weight and distance this post has everything you need, just follow the instructions below to get your spreadsheet up and running. This is also a good primer on using OAuth2 with Google Apps Script and should be a decent starting point for a more complex Google Fit integration. If you have any questions or feedback please leave a comment below.

To get started you need a Google Sheet, an apps script project attached to the sheet and a Google API Project that will provide access to the Fitness API. That might sound intimidating but it should only take a few minutes to get everything up and running.

In Google Drive create a new spreadsheet and call it whatever you like. Rename the first tab to 'Metrics'. Enter 'Date' in cell A1, 'Steps' in B1, 'Weight' in C1 and 'Distance' in D1. To grab history as well create another tab called 'History' with the same headers. Next select 'Script editor...' from the Tools menu which will open a new apps script project.

Give the apps script project a name and then select 'Libraries...' from the Resources menu. Next to 'Add a library' enter 1B7FSrk5Zi6L1rSxxTDgDEUsPzlukDsi4KGuTMorsTQHhGBzBkMun4iDF and click Add. This will find the Google OAuth2 library. Choose the most recent version (24 at the time of writing) and click Save. Then select 'Project properties' from the File menu and make a note of the Script ID (a long series of letters and numbers).

Open the Google API Console. Create a new project and name it something like 'Google Fit Sheet'. From the Dashboard click Enabled APIs and services and find and select the Fitness API. Then go to Credentials and create an OAuth Client ID. You'll be asked to create a consent screen, the only field you need to enter is the product name (i.e. 'My Fit App'). Then choose Web Application as the application type. You need to set the name and the authorized redirect URL. The redirect URL is https://script.google.com/macros/d/{SCRIPTID}/usercallback replacing {SCRIPTID} with the actual Script ID you made a note of above. After adding this make a note of the Client ID and Client Secret.

Go back to the apps script project and paste the code below into the Code.gs window:

Right at the top of the code there are spaces to enter the Client ID and Client Secret from the API Console. Enter these and save the project.

Switch back to your Google Sheet and reload. After reloading there will be a Google Fit menu item. First select Authorize... You'll get a screen to authorize the script and then a sidebar with a link. Click the link to authorize the script to access your Google Fit data. You can then close the sidebar and select Get Metrics for Yesterday from the Google Fit menu. You should see a new row added to the spreadsheet with yesterday's date and fitness data.

The final step is to automate pulling in the data. Go back to the apps script project and select Current project's triggers from the Edit menu. Add a trigger to run getMetrics() as a time driven day timer - I recommend between 5 and 6am. You can also click notifications to add an email alert if anything goes wrong, like your Google Fit authorization expiring (in which case you just need to come back and authorize from the Google Fit menu again.

At this point you're all set. Every day the spreadsheet will automatically update with your step count from the day before. You can add charts, moving averages, export to other systems, pull in your weight or BMI, etc. I want to add a seven day moving average step count to this blog somewhere as a semi-public motivational tool... watch this space.

Note that weight will be blank in the spreadsheet for days with no weight data. Google Fit doesn't return the last known weight, only the known value for days where an update was recorded.

If you are looking to extend this sample to other data types then this API explorer page is very helpful for finding data types that the API documentation doesn't list.

A couple of times working on this script I got my authorization in a bad state and started getting a 400 error response from the API. If this happens run your Google Fit app, click the Profile icon at the bottom and then the Settings icon at the top right. Click Manage connected apps and then disconnect the script from Google Fit. Finally run the Reset Settings option from the menu in the sheet and then authorize again.

I updated this post on Jan 21, 2019 to extend the sample to handle weight and distance as well as steps. I also improved the history function to handle many days in one API call rather than a quick hack I added earlier that pulled a day at a time. I'd recommend using the code above rather than anything included in comments below (at least comments before this update).

Add your comment...

More Google Apps Script Projects

(All Code Posts)

(Published to the Fediverse as: Export Google Fit Daily Steps, Weight and Distance to a Google Sheet #code #software #fit #appsscript #google #sheets #drive Detailed instructions for setting up an automatic daily export of your step count from Google Fit to a Google Sheet. Uses Google Sheets, Apps Script and the Google Fitness API. )

Comments

Radu J

This script is great and works wonderful. Is there any possibility also to fetch blood_glucose, blood pressure, heart rate

Any suggestion how I cn change the script?

kaciyn

Re: my last comment running the script does require a little bit of babysitting as it occasionally times out and needs to be restarted (I would just update the numbers to start where it left off each time), but it's not too bad as it only took me a few minutes to get about 8 years' worth of measurements

kaciyn

Thanks so much for this!

I was wanting to get history much further back than the 90 day range limit that Google API lets you get at a time so I added a little while loop into the getHistory() function (tried to link a gist but it won't let me RIP)

function getHistory() {

//up to how many days ago you want to get data, default set at ~10 years

const earliestDate=3650;

var fromDaysAgo=0;

var toDaysAgo=0;

//grabs entries 50 at a time to circumvent the limit of 90 and also for nice round numbers

while (fromDaysAgo<=earliestDate) {

toDaysAgo=toDaysAgo+50;

getMetricsForDays(fromDaysAgo, (toDaysAgo-1), 'Measurements');

fromDaysAgo=toDaysAgo;

}

}

I also wanted to get body fat percentage data, the dataSourceId of which proved unreasonably hard to find, so I'm adding it below in case anyone else is similarly having trouble:

{

"dataTypeName": "com.google.body.fat.percentage.summary",

"dataSourceId": "derived:com.google.body.fat.percentage:com.google.android.gms:merged"

},

yubin

I set permissions yesterday, but the next day the permissions were removed, so there is a problem where the data is not updated automatically. How to solve this?

Diedrich

Does anyone know the full JSON file that stores the Google Fit data? I've been able to modify the code to also pull steps, weight, distance, heart minutes, and move minutes, but there's more data within the file and I don't know what the other buckets contain. For example, there's a data field that is after the move minutes, but I have no idea what kind of data it contains -- according to the data type name, it's called "com.google.activity.segment", but I don't know what that really means. Any help on the data structure of the file would be very helpful. Thanks!, DS

yubin

Step count data comes in fine, but sometimes weight data doesn’t come in well. I use an Android phone and a scale called PiCooc. Both Google Fit and Samsung Health are linked to the PiCooc scale, but why is data sometimes coming in and sometimes not?

Kjetil

Simon, could you please show me how your complete code looks like, with the sleep-data included? I have the steps, calroies etc up and runing, but really struggling with sleep data. Thank you so much :)

Robert Ellison

Hi Ois, I don't have a device with sleep data so I've never looked at getting this working. Check this comment and see if it helps.

ois

Hi Robert, first off, brilliant blog post, you've managed to make a complicated setup very straightforward.

I'm guessing reading sleep data will not be so straightforward. Do you have any recommendations on tackling it, by the looks of it i might need a second script to read each sleep event for the day i.e. light, REM etc into a different page.

have you ever given it a try?

Darren Wall

Possibly my problem was not script related but "new phone" related. I've just recently moved to iPhone and since a recent patch update I had not had the Google Fit app left open. Having left the app open over night the script appears to have logged my step count and distance as normal this morning. The update from my last post still stands. Companies like to keep us on our toes by changing menus and terminology!

Add Comment

All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.

Newsletter

Related

Average Server Response Time in Azure Metrics