Created
April 8, 2015 17:36
-
-
Save Scarygami/00b628ce577d2626c74e to your computer and use it in GitHub Desktop.
G+ Quick Stats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment