<jupyter_start><jupyter_text>Notion - Scheduled Updates from Gsheets **Tags:** notion gsheet productivity naas_drivers **Author:** [<NAME>](https://www.linkedin.com/in/pooja-srivastava-bb037649/) This notebook does the following tasks: - Schedule the notebook to run every 15 minutes- Connect with Gsheet and get all rows using the gsheet driver's get method- Connect with NotionDB and get all pages using the Notion Databases driver's get method- Compare the list of pages from notion db with the rows returned from gsheet and add non matching rows to a list- Create new pages in Notion db for each row in the non matching list using the Notion Pages driver's create method- For each created page, add the properties and values from gsheet and update in notion using the Notion Pages driver's update methods Pre-requisite: 1. Gsheets : Please share your Google Sheet with our service account   For the driver to fetch the contents of your google sheet, you need to share it with the service account linked with Naas. 🔗 <EMAIL> 2. Notion : Create a notion integration and use the secret key to share the database.  Refer the documentation here  Input  Import library<jupyter_code>import pandas as pd
import naas
from naas_drivers import notion, gsheet<jupyter_output><empty_output><jupyter_text>Scheduler<jupyter_code>#Schedule the notebook to run every 15 minutes
naas.scheduler.add(cron="*/15 * * * *")<jupyter_output><empty_output><jupyter_text>Setup Notion<jupyter_code># Enter Notion Token API and Database URL
notion_token = "****"
notion_database_url = "https://www.notion.so/YOURDB"

#Unique column name for notion
col_unique_notion = 'Name'<jupyter_output><empty_output><jupyter_text>Setup Gsheet<jupyter_code># Enter spreadsheet_id and sheet name
spreadsheet_id = "****"
sheet_name = "YOUR_SHEET"

#Unique column# for gsheets
col_unique_gsheet = 'Name'<jupyter_output><empty_output><jupyter_text>Model  Get Gsheet and Notion data<jupyter_code>#Connect with Gsheet and get all data in a dataframe
gsheet.connect(spreadsheet_id)
df_gsheet = gsheet.get(sheet_name=sheet_name)
#Connect with Notion db and get all pages in a dataframe
database = notion.connect(notion_token).database.get(notion_database_url)
df_notion = database.df()<jupyter_output><empty_output><jupyter_text>Compare Gsheet and Notion data<jupyter_code>#Iterate through all rows in Gsheet and find match in Notion db
#If no match is found then add data to df_difference dataframe

df_difference = pd.DataFrame()
for index,row in df_gsheet.iterrows():
    x = row[col_unique_gsheet]
    if not (x == df_notion[col_unique_notion]).any():
        df_difference = df_difference.append(df_gsheet.loc[index])<jupyter_output><empty_output><jupyter_text>Output  Create a new page in Notion if it does not match with Gsheet<jupyter_code>#Create a new page in notion db for each row in df_difference dataframe
if(df_difference.empty == False):    
    for index, row in df_difference.iterrows():
        page = notion.connect(notion_token).page.create(database_id=notion_database_url, title=row[col_unique_gsheet])
        #Add all properties here and map with respective column in row
        page.select('Type', row['Type'])
        page.select('Status', row['Status'])
        page.rich_text('Summary', row['Summary'])
        page.update()
    print("The gsheets rows synced successfuly to Notion DB")
else:     
    print("No new rows in Gsheet to sync to Notion DB")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08136180034622043, 'fraction_numerical': 0.004039238315060588, 'mean_word_length': 5.408502772643253, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '56823', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1084, 'n_tokens_neox': 1046, 'n_words': 373, 'path': 'Notion/Notion_Scheduled_Updates_from_Gsheets.ipynb', 'repo_name': 'srini047/awesome-notebooks', 'size': '3516'}