Skip to main content
Push to Custom Widgets from a Google Sheet

Steps to use a Google Sheets script that pulls data from a sheet, before pushing the data to a Custom Widget.

Updated over 2 months ago

We encourage you to try Datasets for the most simple, powerful way to access your most important data with Geckoboard.

In this article, we'll demonstrate a Google Sheets script that pulls data from a sheet before pushing it to the configured Geckoboard Custom Widget.

Note: This script pulls from just the active/single sheet and supports the Funnel, Pie, Map, RAG, Monitoring, Text, and List widgets. You can also send an Image to a Text widget.

Adding the script

  1. Create a new Google Sheet. Click on Tools > Script editor.

    Script editor on Google Sheets

Remove what's there by default and paste in the following code:

function main() {
  var data = getRows();
  pusher(data.key, data.widgets);function pusher(key, widgets) {
  widgets.forEach(function(widget) {
    var widgetURL = "https://push.geckoboard.com/v1/send/" + widget.key;
    var payload = {
      "api_key": key,
      "data": widget.data
    }
    var options = {
      "method" : "post",
      "contentType": "application/json",
      "payload" : payload
    };
    options.payload = JSON.stringify(options.payload);
    Logger.log(options);
    UrlFetchApp.fetch(widgetURL, options);
  });
}function getRows() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rows = sheet.getDataRange().getValues();
  var key = rows[0][1];
  var data = rows.slice(1);
  var widgetRows = findWidgets(data);
  var widgets = splitWidgetData(data, widgetRows);
  var payloads = makePayloads(widgets);
  return {
    key: key,
    widgets: payloads
  }
}function makePayloads(widgets) {
  return widgets.map(function(widget) {
    var key = widget[0][1];
    var type = widget[1][1];
    var data = widgetType(type, widget.slice(2));
    return {
      key: key,
      data: data
    }
  });
}function splitWidgetData(data, widgetRows) {
  var widgets = [];
  widgetRows.forEach(function(_, i) {
    if (widgetRows[i + 1]) {
      widgets.push(
        data.slice(widgetRows[i], widgetRows[i + 1])
      );
    } else {
      widgets.push(
        data.slice(widgetRows[i])
      );
    }
  });
  return widgets;
}
               
function findWidgets(rows) {
  var widgetIndexii = [];
  var column = rows.map(function(row) {
    return row[0];
  });
  column.forEach(function(row, i) {
    if (row === 'Widget') {
      widgetIndexii.push(i);
    }
  });
  return widgetIndexii;
}function widgetType(type, data) {
  var types = {
    RAG: function(items) {
      return { 
        item: items.map(function(item) {
          return { 
            value: item[0],
            text: item[1]
          }
        })
      }
    },
    LIST: function(items) {
      return items.map(function(item) {
        var bit = { title: { text: item[0] } };
        if (typeof item[1] !== undefined) {
          bit.label = {};
          bit.label.name = item[1];
        }
        if (typeof item[2] !== undefined) {
          bit.label.color = item[2];
        }
        if (typeof item[3] !== undefined) {
          bit.description = item[3];
        }
        return bit;
      });
    },
    FUNNEL: function(items) {
      var itemList = items.map(function(item) {
        return {
          value: item[0],
          label: item[1]
        } 
      })
      var obj = { item: itemList };
      if (items[0][2] == 'percentage' && items[0][3] == 'hide') {
        obj.percentage = 'hide';
      }
      return obj;
    },
    PIE: function(items) {
      var slices = {
        item: items.map(function(slice) {
          return {
            value: slice[0],
            label: slice[1],
            color: slice[2],
          }
        })
      }
      return slices;
    },
    TEXT: function(items) {
      return { 
        item: items.map(function(item) {
          return { 
            text: item[0],
            type: item[1] || 0,
          }
        })
      }
    },
TEXTIMG: function(items) {
return {
item: items.map(function(item) {
return {
text: '<img src="' + item[0] + '" alt="Image">',
type: item[1] || 0,
}
})
}
}, MONITORING: function(items) { var item = items[0]; return { status: item[0], downTime: item[1] || '', responseTime: item[2] || 0, } }, MAP: function(items) { return { points: { point: items.map(function(item) { var latitude = item[0]; var longitude = item[1]; var size = item[2]; var color = item[3]; var point = { latitude: latitude, longitude: longitude, } if (size) { point.size = size; } if (color) { point.color = color; } return point; }) } } }, }; return types[type](data); }}

Google Sheets Script Editor

Setting up your spreadsheet

Setting up the Custom Widgets spreadsheet
  1. In cell A1 put the text 'API Key', then in cell B1 add your Geckoboard API Key. Follow these instructions to find your API Key.

  2. In cell A2 put 'Widget', then in cell B2 put the Push widget’s key. Follow these steps to find your widget key:

    1. In Geckoboard, click Add widget.

    2. Find Custom Widgets API from the list of integrations.

    3. Choose and configure your widget type.

    4. Set the method to Push and click on Add widget.

    5. Click Copy widget key.

  3. In cell A3 put 'Type', then in B3 put the widget type (check the script to see available types).

  4. In the next rows put the parameters of the widget. For a RAG widget, the custom widget expects a Value and Text. You would add Value in Column A and the Text in Column B.

  5. The script will pull data row by row and split the rows into groups where it finds the ‘Widget’ and ‘Type’ declarations.

  6. Click the Play > button from the script editor to run the code. It will ask you to authorize the application, so you’ll need to click Allow.

    Run the code by pressing the play button on the script editor

  7. Check your dashboard. If all goes well, you should see the data there.

RAG custom widget

Setting up your spreadsheet for List widgets

List widgets require your spreadsheet to be formatted in the following way:

It requires item title in column A, the label in column B, the label color in column C and the description in column D.

Setting up your Google Sheet for a list widget

The List widget

Setting up your spreadsheet for Map widgets

Map widgets require your spreadsheet to be formatted in the following way:

The columns are latitude, longitude, size, and color.

Setting up your Google Sheet for a map widget
The List widget

Note

As Color requires a hex code, you'll need to change the cell formatting of that column to Plain Text. This will prevent Google Sheets automatically changing 008850 to just 8850.

Setting your color column to plain text

Setting up your spreadsheet for displaying an image using the Text widget

If you want to send an image to your custom widget, your spreadsheet needs to be formatted in the following way:

Type has to be set to TEXTIMG, column A needs to contain the image URL, and column B needs to be set to 0.

Using triggers to automatically update your data

To update your data automatically, follow these steps:

  1. Click on Triggers in the script editor.

  2. Click on Add Trigger and set up an automation to run at the frequency you’d like.

Did this answer your question?