1

I'm looking for a way to send a tweet whenever a new row appears in a Google Spreadsheet. I thought this might be a good starting point, but I'm going to need some help with editing it.

I'd like to be able to automatically send tweets when a new row appears in a certain sheet. In addition to that, I'd like to be able to define a few Twitter parameters which would be represented by data in specific columns. For example:

  • Column A: in_reply_to_status_id
  • Column B: status

I'd be really grateful if anyone could help me with it!

Here's the original code:

function sendTweet() {
  authorizeTwitter();
  var sheet = SpreadsheetApp.getActiveSheet();
  var data  = sheet.getDataRange().getValues();
  for (var i in data) {
    tweet(data[i]);
    Utilities.sleep(1000);
  }
}

function authorizeTwitter() {

  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("CONSUMER_KEY");
  oauthConfig.setConsumerSecret("CONSUMER_SECRET");

}

function tweet(tweet) {

  var encodedTweet = encodeURIComponent(tweet);

  var requestData = {
    "method": "POST",
    "oAuthServiceName": "twitter",
    "oAuthUseToken": "always"
  };

  try {
    var result = UrlFetchApp.fetch(
          "https://api.twitter.com/1.1/statuses/update.json?status=" + encodedTweet,
          requestData);
  catch (e) {
      Logger.log(e);
  }
}
  • How is the new row being added? Manually? With a script? – Alan Wells Jan 02 '15 at 19:22
  • Throught a function (formula?) that returns empty data when it doesn't have anything to show. – Dariusz Kuśnierek Jan 02 '15 at 20:35
  • If you are adding row manually, you will have to run your script to check your spreadsheet for new rows with time driven triggers and send tweets then. If you are running with script, it is pretty straight forward to send tweets as soon as you add it thorough script. – rpm Jan 07 '15 at 23:33

0 Answers0