Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use Twitter directly in your Google Doc, so no one will ever blame you for being social
var CONSUMER_KEY = "<< YOUR KEY HERE >>";
var CONSUMER_SECRET = "<< YOUR SECRET HERE >>";
function getConsumerKey() {
return CONSUMER_KEY;
}
function getConsumerSecret() {
return CONSUMER_SECRET;
}
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.addMenu("Tweetsheet", [
{ name : "Tweet", functionName : "renderTweetDialog" },
{ name: "Refresh timeline", functionName: "refreshTimeline" }
]);
};
function refreshTimeline() {
authorize();
var requestData = {
"method": "GET",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
try {
var result = UrlFetchApp.fetch(
"https://api.twitter.com/1/statuses/home_timeline.json?include_rts=1",
requestData);
var tweets = Utilities.jsonParse(result.getContentText());
var sheet = SpreadsheetApp.getActiveSpreadsheet();
for (var i = 0; i < tweets.length; i++) {
sheet.getRange("A" + (i+3)).setValue("@" + tweets[i].user.screen_name);
sheet.getRange("B" + (i+3)).setValue(tweets[i].text).clearFormat();
sheet.getRange("C" + (i+3)).clearContent();
sheet.getRange("D" + (i+3)).setFontColor("white").setValue(tweets[i].id);
if (tweets[i].favorited) {
sheet.getRange("B" + (i+3)).setBackgroundColor("yellow");
}
if (tweets[i].current_user_retweet) {
sheet.getRange("B" + (i+3)).setFontWeight("bold");
}
}
}
catch(e) {
Logger.log(e);
}
}
function onEdit() {
try {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
if (sheet.getActiveCell().getColumn() != 3) {
return;
}
var rowId = sheet.getActiveCell().getRow();
var command = sheet.getRange("C" + rowId).getValue();
var tweetId = sheet.getRange("D" + rowId).getValue();
if (command == "") {
return;
}
var validCommands = {
"<3": "fave",
"rt": "retweet",
"reply": "reply"
};
var app = UiApp.createApplication().setTitle('What do you want to tweet today?');
var handler;
if (command in validCommands) {
switch(validCommands[command]) {
case "fave":
handler = app.createServerHandler("fave");
break;
case "retweet":
handler = app.createServerHandler("retweet");
break;
case "reply":
handler = app.createServerHandler("sendTweet");
break;
}
}
var tweet = app.createTextBox().setName("tweetId").setWidth("100%").setValue(tweetId);
tweet.setVisible(false);
handler.addCallbackElement(tweet);
app.setHeight(100);
var sendButton = app.createButton("Yes", handler);
var dialogPanel = app.createFlowPanel();
dialogPanel.add(tweet);
if (command == "reply") {
var tweetText = app.createTextBox().setName("tweet").setWidth("100%").setValue(sheet.getRange("A" + rowId).getValue());
dialogPanel.add(tweetText);
handler.addCallbackElement(tweetText);
}
dialogPanel.add(sendButton);
app.add(dialogPanel);
sheet.show(app);
} catch (e) {
Logger.log(err);
}
}
function fave(e) {
var requestData = {
"method": "POST",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
try {
authorize();
var result = UrlFetchApp.fetch(
"https://api.twitter.com/1/favorites/create/" + e.parameter.tweetId + ".json",
requestData);
} catch (err) {
Logger.log(err);
}
var app = UiApp.getActiveApplication();
app.close();
return app;
}
function retweet(e) {
authorize();
var requestData = {
"method": "POST",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
try {
var result = UrlFetchApp.fetch(
"https://api.twitter.com/1/statuses/retweet/" + e.parameter.tweetId + ".json",
requestData);
} catch (err) {
Logger.log(Utilities.jsonStringify(err));
}
var app = UiApp.getActiveApplication();
app.close();
return app;
}
function renderTweetDialog() {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var app = UiApp.createApplication().setTitle("Send Tweet");
app.setHeight(100);
var helpLabel = app.createLabel("Enter your tweet below:");
helpLabel.setStyleAttribute("text-align", "justify");
var tweet = app.createTextBox().setName("tweet").setWidth("100%");
var sendHandler = app.createServerClickHandler("sendTweet");
sendHandler.addCallbackElement(tweet);
var sendButton = app.createButton("Send Tweet", sendHandler);
var dialogPanel = app.createFlowPanel();
dialogPanel.add(helpLabel);
dialogPanel.add(tweet);
dialogPanel.add(sendButton);
app.add(dialogPanel);
doc.show(app);
}
function authorize() {
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
oauthConfig.setAccessTokenUrl(
"https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl(
"https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl(
"https://api.twitter.com/oauth/authorize");
oauthConfig.setConsumerKey(getConsumerKey());
oauthConfig.setConsumerSecret(getConsumerSecret());
var requestData = {
"method": "GET",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
try {
var result = UrlFetchApp.fetch(
"https://api.twitter.com/1/statuses/mentions.json",
requestData);
} catch(e) {
Logger.log(e);
}
}
function sendTweet(e) {
var tweet = e.parameter.tweet;
var tweetId = e.parameter.tweetId;
authorize();
// Tweet must be URI encoded in order to make it to Twitter safely
var encodedTweet = encodeURIComponent(tweet);
var requestData = {
"method": "POST",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
if (tweetId) {
requestData.payload = { "in_reply_to_status_id": tweetId };
}
try {
var result = UrlFetchApp.fetch(
"https://api.twitter.com/1/statuses/update.json?status=" + encodedTweet,
requestData);
} catch (e) {
Logger.log(e);
}
var app = UiApp.getActiveApplication();
app.close();
return app;
}

Cool script. How do I implement this? I want to use this to get follower number count from a twitter username.

Thanks,

Timothy

Timothy, Have you found any solutions for this that work. I am trying to do the same thing.

LukeXF commented Sep 13, 2014

Same here - Still an issue 👍

This is great, I think it could do with a minor modification of allowing peoples to schedule tweets from a sheet based on a defined time period...

Error encountered: Script is using OAuthConfig which has been shut down. Learn more at http://goo.gl/IwCSaV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment