From 9d6939b6bf67495e5b0d3913f5919f48eee4b534 Mon Sep 17 00:00:00 2001 From: lordneeko Date: Mon, 8 Apr 2019 11:35:10 -0400 Subject: [PATCH 1/5] Update intraday_all.gs to use HtmlService instead of UiApp --- intraday_all.gs | 100 ++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 76 deletions(-) diff --git a/intraday_all.gs b/intraday_all.gs index eb54f74..ce1dffe 100644 --- a/intraday_all.gs +++ b/intraday_all.gs @@ -33,25 +33,15 @@ var SERVICE_IDENTIFIER = 'fitbit'; var userProperties = PropertiesService.getUserProperties(); function onOpen() { - var ss = SpreadsheetApp.getActiveSpreadsheet(); - var menuEntries = [{ - name: "Setup", - functionName: "setup" - }, - { - name: "Authorize", - functionName: "showSidebar" - }, - { - name: "Reset", - functionName: "clearService" - }, - { - name: "Download data", - functionName: "refreshTimeSeries" - } - ]; - ss.addMenu("Fitbit", menuEntries); + + SpreadsheetApp.getUi() + .createMenu("Intraday Fitbit") + .addItem('Setup','setup') + .addItem('Authorize', 'showSidebar') + .addItem('Reset', 'clearService') + .addItem("Download data", 'refreshTimeSeries') + .addToUi(); + } @@ -97,15 +87,18 @@ function getConsumerSecret() { return secret; } +function getProjectKey() { + return ScriptApp.getProjectKey(); +} + // function saveSetup saves the setup params from the UI function saveSetup(e) { - setConsumerKey(e.parameter.consumerKey); - setConsumerSecret(e.parameter.consumerSecret); - setLoggables(e.parameter.loggables); - setFirstDate(e.parameter.firstDate); - var app = UiApp.getActiveApplication(); - app.close(); - return app; + + setConsumerKey(e.consumerKey); + setConsumerSecret(e.consumerSecret); + setLoggables(e.loggables); + setFirstDate(e.firstDate); + } function setFirstDate(firstDate) { @@ -124,57 +117,12 @@ function getFirstDate() { // function setup accepts and stores the Consumer Key, Consumer Secret, Project Key, firstDate, and list of Data Elements function setup() { - var doc = SpreadsheetApp.getActiveSpreadsheet(); - var app = UiApp.createApplication().setTitle("Setup Fitbit Download"); - app.setStyleAttribute("padding", "10px"); - - var consumerKeyLabel = app.createLabel("Fitbit OAuth 2.0 Client ID:*"); - var consumerKey = app.createTextBox(); - consumerKey.setName("consumerKey"); - consumerKey.setWidth("100%"); - consumerKey.setText(getConsumerKey()); - - var consumerSecretLabel = app.createLabel("Fitbit OAuth Consumer Secret:*"); - var consumerSecret = app.createTextBox(); - consumerSecret.setName("consumerSecret"); - consumerSecret.setWidth("100%"); - consumerSecret.setText(getConsumerSecret()); - - var projectKeyTitleLabel = app.createLabel("Project key: "); - var projectKeyLabel = app.createLabel(ScriptApp.getProjectKey()); - - var firstDate = app.createTextBox().setId("firstDate").setName("firstDate"); - firstDate.setName("firstDate"); - firstDate.setWidth("100%"); - firstDate.setText(getFirstDate()); - - // create the save handler and button - var saveHandler = app.createServerClickHandler("saveSetup"); - var saveButton = app.createButton("Save Setup", saveHandler); - - // put the controls in a grid - var listPanel = app.createGrid(8, 3); - listPanel.setWidget(1, 0, consumerKeyLabel); - listPanel.setWidget(1, 1, consumerKey); - listPanel.setWidget(2, 0, consumerSecretLabel); - listPanel.setWidget(2, 1, consumerSecret); - listPanel.setWidget(3, 0, app.createLabel(" * (obtain these at dev.fitbit.com, use OAuth2.0)")); - listPanel.setWidget(4, 0, projectKeyTitleLabel); - listPanel.setWidget(4, 1, projectKeyLabel); - listPanel.setWidget(5, 0, app.createLabel("Download day (yyyy-mm-dd or 'today')")); - listPanel.setWidget(5, 1, firstDate); - listPanel.setWidget(7, 0, app.createLabel("Very long intervals will not work; exceed Fitbit rate limit and/or function will timeout")); - - // Ensure that all controls in the grid are handled - saveHandler.addCallbackElement(listPanel); - // Build a FlowPanel, adding the grid and the save button - var dialogPanel = app.createFlowPanel(); - dialogPanel.add(listPanel); - dialogPanel.add(saveButton); - app.add(dialogPanel); - doc.show(app); + + var html = HtmlService.createHtmlOutputFromFile('Setup').setHeight(1200); + + SpreadsheetApp.getUi() + .showModalDialog(html, 'Setup'); } - function getFitbitService() { // Create a new service with the given name. The name will be used when // persisting the authorized token, so ensure it is unique within the From d89576190dee538bd378af8cfdb9fbd229dce642 Mon Sep 17 00:00:00 2001 From: lordneeko Date: Mon, 8 Apr 2019 11:36:53 -0400 Subject: [PATCH 2/5] Create Setup.html to use HtmlService instead of UiApp --- Setup.html | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 Setup.html diff --git a/Setup.html b/Setup.html new file mode 100644 index 0000000..28f8371 --- /dev/null +++ b/Setup.html @@ -0,0 +1,82 @@ + + + + + + + +
+
+ Fitbit OAuth 2.0 Client ID:* + +
+
+ Fitbit OAuth Consumer Secret:* + +
+
+ Project Key: + +
+
+ First Date: + +
+ +
+
+ + + From 70cf134cfa3cf256d31436f12bf17ade90e68bcb Mon Sep 17 00:00:00 2001 From: lordneeko Date: Mon, 8 Apr 2019 11:38:35 -0400 Subject: [PATCH 3/5] Update heartrate.gs to use HtmlService instead of UiApp --- heartrate.gs | 104 ++++++++++++--------------------------------------- 1 file changed, 23 insertions(+), 81 deletions(-) diff --git a/heartrate.gs b/heartrate.gs index b37c313..3ee80de 100644 --- a/heartrate.gs +++ b/heartrate.gs @@ -14,25 +14,15 @@ var GET_HEART_RATE_DATA = 1; // set this to 0 if you want to do step data instea function onOpen() { - var ss = SpreadsheetApp.getActiveSpreadsheet(); - var menuEntries = [ - { - name: "Setup", - functionName: "setup" - }, - { - name: "Authorize", - functionName: "showSidebar" - }, - { - name: "Reset", - functionName: "clearService" - }, - { - name: "Sync", - functionName: "refreshTimeSeries" - }]; - ss.addMenu("Fitbit", menuEntries); + + SpreadsheetApp.getUi() + .createMenu("Intraday Fitbit HeartRate") + .addItem('Setup','setup') + .addItem('Authorize', 'showSidebar') + .addItem('Reset', 'clearService') + .addItem("Download data", 'refreshTimeSeries') + .addToUi(); + } @@ -78,16 +68,18 @@ function getConsumerSecret() { return secret; } +function getProjectKey() { + return ScriptApp.getProjectKey(); +} + // function saveSetup saves the setup params from the UI function saveSetup(e) { - setConsumerKey(e.parameter.consumerKey); - setConsumerSecret(e.parameter.consumerSecret); - setLoggables(e.parameter.loggables); - setFirstDate(e.parameter.firstDate); - setLastDate(e.parameter.lastDate); - var app = UiApp.getActiveApplication(); - app.close(); - return app; + + setConsumerKey(e.consumerKey); + setConsumerSecret(e.consumerSecret); + setLoggables(e.loggables); + setFirstDate(e.firstDate); + } function setFirstDate(firstDate) { @@ -117,61 +109,11 @@ function getLastDate() { // function setup accepts and stores the Consumer Key, Consumer Secret, Project Key, firstDate, and list of Data Elements function setup() { - var doc = SpreadsheetApp.getActiveSpreadsheet(); - var app = UiApp.createApplication().setTitle("Setup Fitbit Download"); - app.setStyleAttribute("padding", "10px"); - - var consumerKeyLabel = app.createLabel("Fitbit OAuth 2.0 Client ID:*"); - var consumerKey = app.createTextBox(); - consumerKey.setName("consumerKey"); - consumerKey.setWidth("100%"); - consumerKey.setText(getConsumerKey()); - - var consumerSecretLabel = app.createLabel("Fitbit OAuth Consumer Secret:*"); - var consumerSecret = app.createTextBox(); - consumerSecret.setName("consumerSecret"); - consumerSecret.setWidth("100%"); - consumerSecret.setText(getConsumerSecret()); - - var projectKeyTitleLabel = app.createLabel("Project key: "); - var projectKeyLabel = app.createLabel(ScriptApp.getProjectKey()); - - var firstDate = app.createTextBox().setId("firstDate").setName("firstDate"); - firstDate.setName("firstDate"); - firstDate.setWidth("100%"); - firstDate.setText(getFirstDate()); - - var lastDate = app.createTextBox().setId("lastDate").setName("lastDate"); - lastDate.setName("lastDate"); - lastDate.setWidth("100%"); - lastDate.setText(getLastDate()); - // create the save handler and button - var saveHandler = app.createServerClickHandler("saveSetup"); - var saveButton = app.createButton("Save Setup", saveHandler); - - // put the controls in a grid - var listPanel = app.createGrid(8, 3); - listPanel.setWidget(1, 0, consumerKeyLabel); - listPanel.setWidget(1, 1, consumerKey); - listPanel.setWidget(2, 0, consumerSecretLabel); - listPanel.setWidget(2, 1, consumerSecret); - listPanel.setWidget(3, 0, app.createLabel(" * (obtain these at dev.fitbit.com, use OAuth2.0)")); - listPanel.setWidget(4, 0, projectKeyTitleLabel); - listPanel.setWidget(4, 1, projectKeyLabel); - listPanel.setWidget(5, 0, app.createLabel("Start Date for download (yyyy-mm-dd)")); - listPanel.setWidget(5, 1, firstDate); - listPanel.setWidget(6, 0, app.createLabel("End date for download (yyyy-mm-dd)")); - listPanel.setWidget(6, 1, lastDate); - listPanel.setWidget(7, 0, app.createLabel("Very long intervals will not work; exceed Fitbit rate limit and/or function will timeout")); - // Ensure that all controls in the grid are handled - saveHandler.addCallbackElement(listPanel); - // Build a FlowPanel, adding the grid and the save button - var dialogPanel = app.createFlowPanel(); - dialogPanel.add(listPanel); - dialogPanel.add(saveButton); - app.add(dialogPanel); - doc.show(app); + var html = HtmlService.createHtmlOutputFromFile('Setup').setHeight(1200); + + SpreadsheetApp.getUi() + .showModalDialog(html, 'Setup'); } function getFitbitService() { From 0dd1a333681587d0013eb29a005d8d956e21174e Mon Sep 17 00:00:00 2001 From: lordneeko Date: Mon, 8 Apr 2019 11:40:13 -0400 Subject: [PATCH 4/5] Update intraday.gs to use HtmlService instead of UiApp --- intraday.gs | 106 ++++++++++++---------------------------------------- 1 file changed, 24 insertions(+), 82 deletions(-) diff --git a/intraday.gs b/intraday.gs index 9787551..33148de 100644 --- a/intraday.gs +++ b/intraday.gs @@ -19,28 +19,17 @@ var CONSUMER_SECRET_PROPERTY_NAME = "fitbitConsumerSecret"; var SERVICE_IDENTIFIER = 'fitbit'; function onOpen() { - var ss = SpreadsheetApp.getActiveSpreadsheet(); - var menuEntries = [ - { - name: "Setup", - functionName: "setup" - }, - { - name: "Authorize", - functionName: "showSidebar" - }, - { - name: "Reset", - functionName: "clearService" - }, - { - name: "Download data", - functionName: "refreshTimeSeries" - }]; - ss.addMenu("Fitbit", menuEntries); + + SpreadsheetApp.getUi() + .createMenu("Intraday Fitbit") + .addItem('Setup','setup') + .addItem('Authorize', 'showSidebar') + .addItem('Reset', 'clearService') + .addItem("Download data", 'refreshTimeSeries') + .addToUi(); + } - function isConfigured() { return getConsumerKey() != "" && getConsumerSecret() != ""; } @@ -83,16 +72,19 @@ function getConsumerSecret() { return secret; } +function getProjectKey() { + + return ScriptApp.getProjectKey(); +} + // function saveSetup saves the setup params from the UI function saveSetup(e) { - setConsumerKey(e.parameter.consumerKey); - setConsumerSecret(e.parameter.consumerSecret); - setLoggables(e.parameter.loggables); - setFirstDate(e.parameter.firstDate); - setLastDate(e.parameter.lastDate); - var app = UiApp.getActiveApplication(); - app.close(); - return app; + + setConsumerKey(e.consumerKey); + setConsumerSecret(e.consumerSecret); + setLoggables(e.loggables); + setFirstDate(e.firstDate); + } function setFirstDate(firstDate) { @@ -122,61 +114,11 @@ function getLastDate() { // function setup accepts and stores the Consumer Key, Consumer Secret, Project Key, firstDate, and list of Data Elements function setup() { - var doc = SpreadsheetApp.getActiveSpreadsheet(); - var app = UiApp.createApplication().setTitle("Setup Fitbit Download"); - app.setStyleAttribute("padding", "10px"); - - var consumerKeyLabel = app.createLabel("Fitbit OAuth 2.0 Client ID:*"); - var consumerKey = app.createTextBox(); - consumerKey.setName("consumerKey"); - consumerKey.setWidth("100%"); - consumerKey.setText(getConsumerKey()); - - var consumerSecretLabel = app.createLabel("Fitbit OAuth Consumer Secret:*"); - var consumerSecret = app.createTextBox(); - consumerSecret.setName("consumerSecret"); - consumerSecret.setWidth("100%"); - consumerSecret.setText(getConsumerSecret()); - - var projectKeyTitleLabel = app.createLabel("Project key: "); - var projectKeyLabel = app.createLabel(ScriptApp.getProjectKey()); - - var firstDate = app.createTextBox().setId("firstDate").setName("firstDate"); - firstDate.setName("firstDate"); - firstDate.setWidth("100%"); - firstDate.setText(getFirstDate()); - - var lastDate = app.createTextBox().setId("lastDate").setName("lastDate"); - lastDate.setName("lastDate"); - lastDate.setWidth("100%"); - lastDate.setText(getLastDate()); - // create the save handler and button - var saveHandler = app.createServerClickHandler("saveSetup"); - var saveButton = app.createButton("Save Setup", saveHandler); - - // put the controls in a grid - var listPanel = app.createGrid(8, 3); - listPanel.setWidget(1, 0, consumerKeyLabel); - listPanel.setWidget(1, 1, consumerKey); - listPanel.setWidget(2, 0, consumerSecretLabel); - listPanel.setWidget(2, 1, consumerSecret); - listPanel.setWidget(3, 0, app.createLabel(" * (obtain these at dev.fitbit.com, use OAuth2.0)")); - listPanel.setWidget(4, 0, projectKeyTitleLabel); - listPanel.setWidget(4, 1, projectKeyLabel); - listPanel.setWidget(5, 0, app.createLabel("Start Date for download (yyyy-mm-dd)")); - listPanel.setWidget(5, 1, firstDate); - listPanel.setWidget(6, 0, app.createLabel("End date for download (yyyy-mm-dd)")); - listPanel.setWidget(6, 1, lastDate); - listPanel.setWidget(7, 0, app.createLabel("Very long intervals will not work; exceed Fitbit rate limit and/or function will timeout")); - // Ensure that all controls in the grid are handled - saveHandler.addCallbackElement(listPanel); - // Build a FlowPanel, adding the grid and the save button - var dialogPanel = app.createFlowPanel(); - dialogPanel.add(listPanel); - dialogPanel.add(saveButton); - app.add(dialogPanel); - doc.show(app); + var html = HtmlService.createHtmlOutputFromFile('Setup').setHeight(1200); + + SpreadsheetApp.getUi() + .showModalDialog(html, 'Setup'); } function getFitbitService() { From 901ee0b97972679deafb6ca21708c1a6566ae382 Mon Sep 17 00:00:00 2001 From: lordneeko Date: Mon, 8 Apr 2019 11:42:18 -0400 Subject: [PATCH 5/5] Update README to include needed Setup.html file --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 31f886d..5423470 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ And "_Developer information: invalidrequest - Invalid redirecturi parameter valu That means you did not follow the instructions correctly. +For all intraday scripts you must also get the Setup.html file. This file should be loaded to your google scripts project as well. The HtmlService will use this when calling the Setup function. + ## interday.gs Download step data, one row per day, from a start day to the present. Make sure not to set the start day too far in the past.