天天看點

無需打開應用,如何用 api 實作在 Notion 的表格中寫入資料

作者:又開始折騰了

上一篇 用 Notion 搭建專屬于自己的圖書館 隻簡單介紹了一下大體思路,這裡再講一下技術細節。本篇适合有一定程式設計基礎的朋友

準備工作

  1. 在 Notion 中建立一個表格,确定好每列的标題和類型
  2. 建立一個 Notion api
  3. 将 Notion api 與建立的表格連接配接

具體步驟可以參考這篇文章:Notion – The all-in-one workspace for your notes, tasks, wikis, and databases.

構造請求體

請求體的主要部分就兩塊:parent 和 properties,格式如下

{
"parent": {
    "type": "database_id",
    "database_id": "Notion 表格 ID"
    },
"properties":{
	字段 1,
	字段 2,
	字段 3
	}
}           

其中 Notion 表格 ID 要換成自己的表格 ID,而我們要寫入的内容則是在 字段 1,字段 2,字段 3……這個位置

字段格式

那字段該怎麼寫呢,下面是 Notion 常用的字段類型格式

Title

"标題":{
	"title":[
		{
		"type":"text",
		"text":{"content":"甲"}
		}
	]
}           

Text

"文本":{
	"rich_text":[
		{
		"type":"text",
		"text":{"content":"甲"}
		}
	]
}           

Select

"單選":{
	"select":{
		"name":"甲"
	}
}           

Multi select

"多選":{
	"multi_select":[
		{"name": "甲"},
		{"name": "乙"},
		{"name": "丙"}
	]
}           

Number

"數字":{
	"number":123
}           

File

"檔案":{
	"files":[
		{
		"name":"甲",
		"tpye":"external",
		"external":{"url":"檔案連結"}
		}
	]
}           

漢字和阿拉伯數字要替換成自己的内容

例如你的表格中,有一列名稱叫書名,類型是 title,書名名字叫《藝概》,就應該找第一個格式模闆,将其改成這樣:

"書名":{
	"title":[
		{
		"type":"text",
		"text":{"content":"《藝概》"}
		}
	]
}           

還有一列叫價格,類型是 number,價格是 30 元,就該找到 number 模闆,将其改成這樣:

"價格":{
	"number":30
}           

然後找到标題二下面的代碼,将這字段 1,字段 2 替換成兩塊代碼,結果如下:

{
	"parent": {
		"type": "database_id",
		"database_id": "Notion 表格 ID"
	},
	"properties": {
		"書名": {
			"title": [
				{
					"type": "text",
					"text": {
						"content": "《藝概》"
					}
				}
			]
		},
		"數字": {
			"number": 30
		}
	}
}           

注意字段最後的英文逗号,最後一個字段不需要加逗号

最終效果

無需打開應用,如何用 api 實作在 Notion 的表格中寫入資料
"parent": {
    "type": "database_id",
    "database_id": "你的 notion 表格 ID"
  },
  "properties": {
    "中圖分類":{
      "rich_text": [
        {
          "type": "text",
          "text": {
            "content": "I206.2"
          }
        }
      ]
    },
    "書名": {
      "title": [
        {
          "type": "text",
          "text": {
            "content": "藝概"
          }
        }
      ]
    },
    "作者": {
      "rich_text": [
        {
          "type": "text",
          "text": {
            "content": "[清]劉熙載 著&葉子卿 校注"
          }
        }
      ]
    },
    "譯者": {
      "rich_text": [
        {
          "type": "text",
          "text": {
            "content": ""
          }
        }
      ]
    },
    "出版社": {
      "select": {
        "name": "浙江人民美術出版社"
      }
    },
    "出版日期": {
      "number": 2017
    },
    "ISBN": {
      "rich_text": [
        {
          "type": "text",
          "text": {
            "content": "9787534055850"
          }
        }
      ]
    },
    "叢書": {
      "rich_text": [
        {
          "type": "text",
          "text": {
            "content": "藝文叢刊"
          }
        }
      ]
    },
    "豆瓣評分": {
      "number": 9.1
    },
    "封面": {
      "files": [
        {
          "name": "testname",
          "type": "external",
          "external": {
            "url": "https://img2.doubanio.com/view/subject/s/public/s33648212.jpg"
          }
        }
      ]
    },
    "閱讀狀态": {
      "select": {
        "name": "已讀"
      }
    },
    "藏書情況": {
      "multi_select": [ { "name": "紙質書"} ]
    }
  }
}           

繼續閱讀