Export Tweets from Twitter with Google Apps Script

The Twitter Archiver app will archive tweets for any hashtag to a Google Spreadsheet using the Twitter API and Google Apps Script.

You can set a time-based trigger to run downloadTweets() every 5 minutes or even 1 minute for #hashtags that are extremely popular and generate thousands of tweets. The code has been updated to using the OAuth1 library instead of the OAuthConfig service which has since been deprecated.

function downloadTweets(searchTerm) {
  var twitterService = getTwitterService_();

  var props = PropertiesService.getUserProperties();
  var sinceID = props.getProperty('SINCEID') || '';

  var api = 'https://api.twitter.com/1.1/search/tweets.json?count=100&include_entities=false';
  api += '&result_type=recent&q=' + encodeString_(searchTerm) + '&since_id=' + sinceID;

  var result = twitterService.fetch(api);

  if (result.getResponseCode() == 200) {
    var json = JSON.parse(result.getContentText());

    var tweets = json.statuses;

    // SINCEID will store the ID of the last processed tweet

    for (var i = tweets.length - 1; i >= 0; i--) {
      if (i == 0) {
        props.setProperty('SINCEID', tweets[0].id_str);

/* Add the tweet details into the sheet */
function logTweet_(tweet) {
  var log = [];

  log.push(new Date(tweet.created_at));
    '=HYPERLINK("https://twitter.com/' +
      tweet.user.screen_name +
      '/status/' +
      tweet.id_str +
      '","' +
      tweet.user.name +
  log.push(tweet.text.replace(/\n|\r/g, ' '));


function getTwitterService_() {
  var props = PropertiesService.getUserProperties();

  return OAuth1.createService('twitter')

/* Properly encode the Twitter search query */
function encodeString_(q) {
  var str = encodeURIComponent(q);
  str = str.replace(/!/g, '%21');
  str = str.replace(/\*/g, '%2A');
  str = str.replace(/\(/g, '%28');
  str = str.replace(/\)/g, '%29');
  str = str.replace(/'/g, '%27');
  return str;
Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory


Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.