Links

Generate Google Sheets rows for new items in Notion database

Tags: #notion #operations #automation #googlesheets
Author: Pooja Srivastava
Description: This notebook allows users to automatically generate Google Sheets rows for new items added to a database using Notion.

Input

Import librairies

from naas_drivers import notion, gsheet
import pandas as pd

Setup Notion

# Enter Token API
NOTION_TOKEN = "*****"
# Enter Database URL
DATABASE_URL = "https://www.notion.so/********"
# Unique column name for notion
col_unique_notion = "Name"

Setup Google Sheets

# Spreadsheet URL
SPREADSHEET_URL = "------"
# Sheet name
SHEET_NAME = "Sheet1"
# Unique column# for gsheets
col_unique_gsheet = "Name"

Setup Naas

# Schedule your notebook every hours
naas.scheduler.add(cron="0 * * * *")
# -> Uncomment the line below to remove your scheduler
# naas.scheduler.delete()

Model

Get dataframe from Notion database

db_notion = notion.connect(NOTION_TOKEN).database.get(DATABASE_URL)
df_notion = db_notion.df()

Get dataframe Google Sheets spreasheet

df_gsheet = gsheet.connect(SPREADSHEET_URL).get(sheet_name=SHEET_NAME)
df_gsheet

Compare Notion database with Google Sheets

# Iterate through all rows in Notion database and find match in Google Sheets
# If no match is found then add data to df_difference dataframe
df_difference = pd.DataFrame()
for index, row in df_notion.iterrows():
x = row[col_unique_notion]
if not (x == df_gsheet[col_unique_gsheet]).any():
df_difference = df_difference.append(df_notion.loc[index])

Output

Add new rows in Google Sheets

# Send data to Google Sheets
gsheet.connect(SPREADSHEET_URL).send(
sheet_name=SHEET_NAME,
data=df_difference,
append=True,
)