How to Import Multiple Rows from Google Sheets to a Notion Database?

This topic was automatically generated from Slack. You can find the original thread here.

hi I need help importing multiple rows from Google Sheets to a Notion Database. The Google Sheet has columns for Data, Task and Notes and has 7 rows total. The Notion database contains the same fields.

To import multiple rows from Google Sheets to a Notion database, you can create a Pipedream workflow that triggers on a schedule or via HTTP request, reads the data from Google Sheets, and then creates or updates entries in the Notion database accordingly. Below is an example of how you might structure this workflow using Pipedream component code. This example assumes you have already set up OAuth for Google and Notion in Pipedream. First, ensure you have the necessary columns (Data, Task, and Notes) in both your Google Sheets and Notion database with matching field names. ``` import { axios } from “@pipedream/platform”; export default defineComponent({ props: { google: { type: “app”, app: “google”, }, notion: { type: “app”, app: “notion”, }, }, async run({ steps, $ }) { // Fetch data from Google Sheets const sheetData = await axios($, { url: https://sheets.googleapis.com/v4/spreadsheets/YOUR_SPREADSHEET_ID/values/YourRange, headers: { Authorization: Bearer ${this.google.$auth.oauth_access_token}, }, }); // Assuming the first row contains headers and subsequent rows contain data const [headers, …rows] = sheetData; for (const row of rows) { // Map Google Sheets data to Notion database fields const payload = { parent: { database_id: “YOUR_NOTION_DATABASE_ID” }, properties: { “Data”: { title: [ { text: { content: row[0], // Assuming ‘Data’ is in the first column }, }, ], }, “Task”: { rich_text: [ { text: { content: row[1], // Assuming ‘Task’ is in the second column }, }, ], }, “Notes”: { rich_text: [ { text: { content: row[2], // Assuming ‘Notes’ is in the third column }, }, ], }, }, }; // Create or update entries in Notion database await axios($, { method: “POST”, url: “https://api.notion.com/v1/pages”, headers: { Authorization: `

I built a workflow using the Google Sheets Get Values in Range action and Notion Create Page from Database action. I can get one row into notion but I need to send multiple rows to Notion. How do I create a code step that will loop through each row in the Google Sheet and add it to the Notion database?

To loop through each row in a Google Sheet and add each row to a Notion database, you can use a Pipedream code step in your workflow. This code step will retrieve the rows from Google Sheets, then iterate over each row, creating a new page in your Notion database for each one. Here’s an example of how you might write this code step in Node.js: import { axios } from "@pipedream/platform" export default defineComponent({ props: { google: { type: "app", app: "google", }, notion: { type: "app", app: "notion", }, }, async run({ steps, $ }) { // Replace with your Google Sheets spreadsheet ID and range const spreadsheetId = "YOUR_SPREADSHEET_ID"; const range = "YOUR_RANGE"; // e.g., "Sheet1!A2:E" // Fetch rows from Google Sheets const sheetData = await axios($, { method: "GET", url: `https://sheets.googleapis.com/v4/spreadsheets/${spreadsheetId}/values/${range}`, headers: { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, }, }); // Iterate over each row and add it to Notion for (const row of sheetData.values) { // Construct your Notion page properties based on the row data // This is an example, adjust the properties to match your Notion database schema const properties = { Name: { title: [ { text: { content: row[0], // Assuming the first column of your sheet contains the name }, }, ], }, // Add more properties as needed }; // Create a page in Notion await axios($, { method: "POST", url: `https://api.notion.com/v1/pages`, headers: { Authorization: `Bearer ${this.notion.$auth.oauth_access_token}`, "Notion-Version": "2021-08-16", "Content-Type": "application/json", }, data: { parent: { database_id: "YOUR_NOTION_DATABASE_ID" }, properties, }, }); } }, }) Please replace "YOUR_SPREADSHEET_ID", `"

does a date need to be formatted before it can be exported from Google Sheets to a Notion database

To export a date from Google Sheets to a Notion database, you’ll need to ensure that the date is formatted correctly for Notion to interpret it properly. Notion expects date formats to comply with ISO 8601 (e.g., YYYY-MM-DD for just a date, or YYYY-MM-DDTHH:MM:SSZ for a date with time). If the date in Google Sheets is not in this format, you should format it before exporting it to Notion. Here’s a basic example of how you might format a date in JavaScript, which you can adapt for use in a Pipedream workflow: ```
// Assuming dateFromGoogleSheets is the date string you got from Google Sheets const dateFromGoogleSheets = “MM/DD/YYYY”; // Example format // Convert the date to a JavaScript Date object const dateObject = new Date(dateFromGoogleSheets); // Format the date to ISO 8601 const formattedDate = dateObject.toISOString(); // Now formattedDate is ready to be used in your Notion database