0

I'm not a developer but am trying to connect data entered into a Notion database with an Equals spreadsheet. I've written a preliminary connection by using help from Chat GPT 3 but something's not right. Can someone please help?

Here is what I have:

const equals = require("equals");
const axios = require("axios");

const API_KEY = equals.getSecret("notion_api_key"); // Replace with your Notion API key
const TABLE_ID = equals.getSecret("notion_table_id"); // Replace with the ID of your Notion table

const getRecords = async () => {
  const resp = await axios({
    method: "post",
    url: "https://api.notion.com/v3/query",
    data: {
      query: `{
        table(id: "${TABLE_ID}") {
          name
          columns {
            name
            type
          }
          rows {
            id
            cells {
              value {
                ... on Text {
                  text
                }
                ... on RichText {
                  text
                }
                ... on Checkbox {
                  checked
                }
                ... on MultiSelect {
                  options {
                    text
                  }
                }
                ... on Date {
                  start
                }
              }
            }
          }
        }
      }`,
    },
    headers: {
      Authorization: `Bearer ${API_KEY}`,
      "Content-Type": "application/json",
    },
  });
  return resp.data.results[0].table.rows;
};

const records = await getRecords();

// Add the table column names as headers
const headers = records[0].cells.map((cell) => cell.value.name);
equals.addHeaders(headers);

// Add the table rows as rows in the Equals sheet
for (const record of records) {
  const row = record.cells.map((cell) => {
    switch (cell.value.type) {
      case "text":
        return cell.value.text;
      case "rich_text":
        return cell.value.text;
      case "checkbox":
        return cell.value.checked;
      case "multi_select":
        return cell.value.options.map((option) => option.text).join(", ");
      case "date":
        return cell.value.start;
      default:
        return null;
    }
  });
  equals.addRow(row);
}

I also wanted to include a quick example I wrote that brings data from Airtable into Equals, just as a comparison:

const equals = require("equals");
const axios = require("axios");

const API_KEY = equals.getSecret("airtable_api_key");
const BASE_ID = equals.getSecret("airtable_base_id");
const TABLE_NAME = "Issues";
const MAX_RECORDS = 100;

const getRecords = async () => {
  const resp = await axios({
    method: "get",
    url: `https://api.airtable.com/v0/${BASE_ID}/${TABLE_NAME}`,
    params: {
      maxRecords: MAX_RECORDS,
      view: "Grid view"
    },
    headers: {
      Authorization: `Bearer ${API_KEY}`
    }
  });
  return resp.data.records;
}

const records = await getRecords();

equals.addHeaders(["number", "title", "user", "state", "closed at", "created at"]);

for(const record of records) {
  equals.addRow([
    record.fields.number,
    record.fields.title,
    record.fields.user,
    record.fields.state,
    record.fields.closed_at,
    record.fields.created_at
  ])
}

I would love to see the data from my Notion table appear in my Equals spreadsheet, so that I can make a more complex analysis off of the inputs in my Notion board.

Abbey
  • 1
  • Hello an Welcome to Stackoverflow! In order for others to help you out, you should be as precise as possible with the error you are encountering. What's the problem you are facing? – Maximilian Haindl Dec 18 '22 at 23:28

0 Answers0