# Send Tweets from Google Sheets using a Google Apps Script

Source: Nidhinkumar

### Overview

In this post, you will learn how to send automated tweets from Google Sheets to Twitter using Google Apps Script.

### Objectives

* What is Apps Script
    
* Create a Twitter developer account
    
* Create a Google Sheet and enable Apps Script
    
* Integrate Twitterlib in Apps Script
    
* Write the logic for the tweets
    
* Tweet it
    

### Prerequisites

* G Suite account
    

### 1\. What is Apps Script

[Google Apps Script](https://developers.google.com/apps-script/overview) is a rapid application development platform that makes it fast and easy to create business applications that integrate with G Suite. You can write code in modern JavaScript and have access to built-in libraries such as Gmail, Calendar, Drive, etc.,

### 2\. Create a Twitter developer account

To send tweets we some API keys and tokens which we can get only if we have a Twitter developer account (if you have a Twitter developer account you can move to Step 3 :)

Create a Twitter developer account from this [link](https://twitter.com/login?redirect_after_login=https%3A%2F%2Fdeveloper.twitter.com%2Fen%2Fapps). Click the Create button like below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056438709/4fb7a61d-2554-4aa1-aecf-d63fff037aaa.jpeg align="left")

Create an App

Fill the form with the app title, description, website link, and the purpose and click save (typically it would take some time).

> Make sure you give the Read and Write permission for the app

once the app is approved you could see a page like below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056439963/4b08dfa1-f08b-4f0b-9f5e-4dd63ad5671f.jpeg align="left")

Keys and Tokens

In the keys and tokens section, you will have the API, API secret, Access token, and Access token secret keys (copy those keys in your local machine).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056442061/7f49c3e8-463c-4e8e-9186-95c54e7fd07a.jpeg align="left")

Permissions

### 3\. Create a Google Sheet and enable Apps Script

We will create some motivational quotes and try to send those quotes as daily tweets.

Open Google Sheets and add some quotes like below with a date field.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056443371/88b50e4c-9ab7-4ad6-996a-8099e9f09e6e.jpeg align="left")

Quotes

Once the quotes are added click **Tools** -&gt; **Script Editor** in Google Sheets.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056444882/7092442a-fc5f-414a-bbd8-7a91a7b823b0.png align="left")

Script Editor

Once you click the Script Editor you will see a window like below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056446617/3a40da1d-833e-4e02-8712-633e3ecb12cf.jpeg align="left")

Script Editor

Here we will write the logic to take the data from Google Sheet and compare the current date and the date in the sheet. If it matches we will send the tweet.

### 4\. Integrate Twitterlib in Apps Script

To send Tweets from Apps Script we are in need of the [Twitterlib](https://github.com/airhadoken/twitter-lib) to do that click **Resources -&gt; Libraries** and then add the below the number in the input box

MKvHYYdYA4G5JJHj7hxIcoh8V4oX7X1M\_

and then click **Add**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056447722/7c06fbd2-d501-431c-ba6d-3fce8b77a03e.jpeg align="left")

Adding Twitter Lib

Select the latest version and click **Save**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056449040/4643d651-ca13-4989-8697-1aadfc038e92.jpeg align="left")

Twitter Lib latest version

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056450459/468950d4-faf1-4795-8326-dc74730a565a.jpeg align="left")

Now the Twitterlib has been added

### 5\. Write the logic for the tweets

Now we will write the logic to send tweets by taking the data from Google Sheet and compare the current date with the date from Google sheet and if both the dates match, we will send the tweet.

First, we will get the active sheet and the rows

```javascript
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var startRowNumber = 1;
var endRowNumber = sheet.getLastRow();
```

Now we will copy the keys which we have done in Step 2 like below

```javascript
var twitterKeys = {
TWITTER_CONSUMER_KEY: "//add your API key",
TWITTER_CONSUMER_SECRET: "//add your API secret key",
TWITTER_ACCESS_TOKEN: "//add your access token key",
TWITTER_ACCESS_SECRET: "//add your access token secret key",
}
```

Now we will initialize Twitterlib for authentication

```javascript
var props = PropertiesService.getScriptProperties();
props.setProperties(twitterKeys);
var params = new Array(0);
var service = new Twitterlib.OAuth(props);

var quote;
var identifier;
```

Now we will have the final logic to get the data from the Google sheet and compare the dates, as well as whether the service has access to send tweets to Twitter

```javascript
for (var currentRowNumber = startRowNumber; currentRowNumber <= endRowNumber; currentRowNumber++) {
var row = sheet.getRange(currentRowNumber + ":" + currentRowNumber).getValues();
// check that the second column (Date) is equal to today
if (isToday(row[0][1])) {
quote = row[0][0];
identifier = currentRowNumber - 1;
if (!service.hasAccess()) {
console.log("Authentication Failed");
} else {
console.log("Authentication Successful");
var status = quote + "\n\n" + "#Quotes #Motivation";
try {
var response = service.sendTweet(status, params);
console.log(response);
} catch (e) { console.log(e) }
}
break;
}
}
```

Compare Dates logic

```javascript
function isToday(date) {
var today = new Date();
var dateFromRow = new Date(date);
return dateFromRow.getDate() == today.getDate() &&
dateFromRow.getMonth() == today.getMonth() &&
dateFromRow.getFullYear() == today.getFullYear()
}
```

Now the final code would be like below

### 6\. Tweet it

Now if the run the function sendTweets() you will be asked for authorization like below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056451621/9247cd4c-6b48-4b89-a57d-ef689861f0cf.jpeg align="left")

App script Login

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056452862/a40ba788-c86e-4566-b641-3a38dc277e39.jpeg align="left")

App script permission

Once the function is executed you could see the output like below

<iframe src="https://www.youtube.com/embed/_chECsoQjhI?feature=oembed" width="700" height="393"></iframe>

Source: Nidhinkumar

Instead of manually triggering the function you can trigger it automatically like below

Click **Edit -&gt; Current Project’s Trigger** which will open a new tab click **Add Trigger** which will open a pop-up modal like below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056454216/cbda1093-fe54-42b8-8520-f00dadf465d9.jpeg align="left")

Trigger

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056455633/7f978d6d-5f02-4ebd-85c3-deb93b61488d.jpeg align="left")

Trigger

Click **Save** now it will automatically send tweets daily

### Reference Links

* TwitterLib — [Click here to view](https://github.com/airhadoken/twitter-lib)
    
* App Script Code — [Click here to view](https://gist.github.com/nidhinkumar06/5e4ddc3dad09a1fc0221065176e3cc5c)
    

### Congratulations!

You have learned how to send automated tweets from Google Sheet using Apps Script

Happy Learning :)
