Skip to content

Instantly share code, notes, and snippets.

@Scarygami
Created April 8, 2015 17:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scarygami/00b628ce577d2626c74e to your computer and use it in GitHub Desktop.
Save Scarygami/00b628ce577d2626c74e to your computer and use it in GitHub Desktop.
G+ Quick Stats
/**
* Creates a menu entry in the Google Sheets UI when the document is opened.
*/
function onOpen(e) {
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Start', 'showSidebar')
.addToUi();
}
/**
* Runs when the add-on is installed.
*/
function onInstall(e) {
onOpen(e);
}
/**
* Displays the sidebar for the add-on
*/
function showSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('Sidebar').setTitle('QuickStats');
SpreadsheetApp.getUi().showSidebar(ui);
}
/**
* Fetches and displays data from the Google+ API Service
*/
function fetchData(userId) {
var person, data, tmpDate, sums, sum_row;
userId = userId || 'me';
// Check if the User ID is valid
try {
person = Plus.People.get(userId);
} catch(e) {
SpreadsheetApp.getUi().alert("Invalid User ID");
return;
}
PropertiesService.getUserProperties().setProperty('userId', userId)
sum_row = 2;
// Fetch 100 activities from the API
data = Plus.Activities.list(userId, 'public', {maxResults: 100});
if (data && data.items && data.items.length > 0) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var w = ss.insertSheet(
person.displayName + ' ' + Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), "yyyy-MM-dd HH:mm:ss"),
{template: ss.getSheetByName('template')});
var w1 = ss.insertSheet(
person.displayName + ' ' + Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), "yyyy-MM-dd HH:mm:ss") + ' Chart',
{template: ss.getSheetByName('template2')});
tmpDate = new Date(data.items[0].published);
sums = [new Date(tmpDate.getFullYear(), tmpDate.getMonth(), tmpDate.getDate()), 0, 0, 0];
for (i = 0; i < data.items.length; i++) {
tmpDate = new Date(data.items[i].published);
tmpDate = new Date(tmpDate.getFullYear(), tmpDate.getMonth(), tmpDate.getDate());
if (tmpDate.getTime() != sums[0].getTime()) {
w1.getRange(sum_row, 1, 1, 4).setValues([sums]);
sums = [tmpDate, 0, 0, 0];
sum_row++;
}
sums[1] += data.items[i].object.plusoners ? data.items[i].object.plusoners.totalItems : 0;
sums[2] += data.items[i].object.resharers ? data.items[i].object.resharers.totalItems : 0;
sums[3] += data.items[i].object.replies ? data.items[i].object.replies.totalItems : 0;
w.getRange(i + 2, 1, 1, 6).setValues([[
tmpDate,
data.items[i].title,
data.items[i].url,
data.items[i].object.plusoners ? data.items[i].object.plusoners.totalItems : 0,
data.items[i].object.resharers ? data.items[i].object.resharers.totalItems : 0,
data.items[i].object.replies ? data.items[i].object.replies.totalItems : 0
]]);
}
w1.getRange(sum_row, 1, 1, 4).setValues([sums]);
for (i = 1; i <= 6; i++) {
w.autoResizeColumn(i);
}
}
w1.activate();
}
/**
* Returns the last used UserId
*/
function getUserId() {
var userProperties = PropertiesService.getUserProperties();
var userId = userProperties.getProperty('userId');
if (!userId) {
userId = 'me';
userProperties.setProperty('userId', userId)
}
return userId;
}
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<style>
td {
padding-left: 2px
}
</style>
<div class="sidebar branding-below">
<form>
<table>
<tr>
<td><label for="user_id">User ID</label></td>
<td><input id="user_id"></td>
</tr>
</table>
<button class="blue" id="fetch_data">Fetch Data</button>
</form>
</div>
<script>
document.getElementById("fetch_data").onclick = function () {
var user_id = document.getElementById("user_id").value;
document.getElementById("fetch_data").disabled = true;
google.script.run.withSuccessHandler(
function () {
document.getElementById("fetch_data").disabled = false;
}
).fetchData(user_id);
}
google.script.run.withSuccessHandler(
function (userId) {
document.getElementById("user_id").value = userId;
}
).getUserId()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment