How to Connect Gmail and Google Sheets to Notion Workspace using Apps Script?

August 6, 2021 Twinkle Kapoor How To

In this article, learn how to use the Notion API with Apps Script to connect Gmail, Google Sheets, Google Forms to your Notion workspace.

Notion is a globally used, all-time favorite tool for storing everything from web pages to code snippets to recipes, has gotten better. They have published a public API, so it will be much easier for developers to read and write to their Notion workspace from external applications.

For example, you can create a document in Google Docs and export it to Notion while it remains within Docs. Google Sheets users can pull pages from the Notion database into their spreadsheet. Any new submission to Google Forms can be saved directly to Notion and so on!

Quick Tip to Save Gmail Messages in Notion

I have created a Gmail plug-in that makes it easy for you to save emails, or any other text content, from Gmail to your Notion workspace with one click. Here’s how the application works.

Step 1: Connect Gmail to Notion

Step 2: Allow access to Notion pages – in case of multiple databases in your Notion workspace, you can choose to grant access to the selected databases and the remaining ones will be inaccessible to the external app.

Step 3: Choose Email – you’ll have an option to edit the email subject content of any email message in Gmail before sending it to the Notion Page

Note: The app only supports plain text format at this time.

Step 4: Open Notion – The content of the selected email message will be added to your Notion database once you hit the ‘Send to Notion’ button. To view the recently added page, you can click the All Updates link in the Notion sidebar.

Using Notion with Google Apps Script

If you want to integrate your own Google plugin with the Notion API, here is a summary of the necessary steps.

Step 1: Click the ‘Create New Integration’ button once you are on notion.so webpage. You’ll be given a Client ID and Client Secret that you’ll need in a later step.

Step 2: It is important to incorporate the OAuth2 library in your Apps Script project and invoke the ‘getRedirectUri’ method to get the OAuth2 redirect URL for the previous step.

const getNotionService = () => {
return OAuth2.createService(“Notion”)

.setAuthorizationBaseUrl(“https://api.notion.com/v1/oauth/authorize”)
.setTokenUrl(“https://api.notion.com/v1/oauth/token”)
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
.setCallbackFunction(“authCallback”)
.setPropertyStore(PropertiesService.getUserProperties())
.setCache(CacheService.getUserCache())
.setTokenHeaders({
Authorization: Basic ${Utilities.base64Encode(
${CLIENT_ID}:${CLIENT_SECRET}`

)}`,
});
};
const authCallback = (request) => {
const isAuthorized = getNotionService().handleCallback(request);
return HtmlService.createHtmlOutput(
isAuthorized ? “Success!” : “Access Denied!”
);
};
const getRedirectUri = () => {
console.log(OAuth2.getRedirectUri());
};

Step 3: Now, to connect to Notion API make a ‘Get’ HTTP request to the /vi/databases to summon all databases that the user has explicitly shared with the authorized app.

function getDatabasesList() {
var service = getNotionService();
if (service.hasAccess()) {
const url = “https://api.notion.com/v1/databases”;
const response = UrlFetchApp.fetch(url, {
headers: {
Authorization: Bearer ${service.getAccessToken()},
“Notion-Version”: “2021-05-13”,
},
});
const { results = [] } = JSON.parse(response.getContentText());
const databases = results
.filter(({ object }) => object === “database”)
.map(({ id, title: [{ plain_text: title }] }) => ({ id, title }));
console.log({ databases });
} else {
console.log(“Please authorize access to Notion”);
console.log(service.getAuthorizationUrl());
}
}

Author

Leave a Reply

Your email address will not be published. Required fields are marked *