MultiSelectProperty(Page): NotionRubyMapping 解説 (44)

はじめに

NotionRubyMapping 解説の第44回目です。ページ側のプロパティを解説しています。今回は MultiSelectProperty の変更です。現在はデータベース内の最初のページを使って説明していきます。

irb(main):002> page = Page.find "https://www.notion.so/hkob/619a113814134c759773175ac988b96a?pvs=4"
=> NotionRubyMapping::Page-619a113814134c759773175ac988b96a

MultiSelectProperty

まず、MultiSelectProperty を取得します。

irb(main):004> msp = page.properties["MultiSelect"]
=>
#<NotionRubyMapping::MultiSelectProperty:0x000000011e6564a8

multi_select でマルチセレクトの設定状況を確認できます。現在は何も設定されていないので空です。

irb(main):005> msp.multi_select
=> []

multi_select << でオプションを追加できます。

irb(main):006> msp.multi_select = ["macOS"]
=> ["macOS"]
irb(main):007> page.save
=> NotionRubyMapping::Page-619a113814134c759773175ac988b96a

すでにデータベースで設定済みのオプションなので、その時に設定した色がついています。

multi_select

結果として、multi_select には以下のように id や color も設定されていることがわかります。

irb(main):014> msp.multi_select
=>
[{"id"=>"f30c827f-60ed-4d50-9eb2-fc5c8c4e0b3d",
  "name"=>"macOS",
  "color"=>"purple"}]

データベースプロパティにない候補を入れてみます。

irb(main):016> msp.multi_select = ["macOS", "Windows"]
=> ["macOS", "Windows"]
irb(main):017> page.save
=> NotionRubyMapping::Page-619a113814134c759773175ac988b96a

追加した multi_select

実際に親のデータベースのオプションを調べてみると、Windows が追加されていることがわかります。

irb(main):018> page.parent.properties["MultiSelect"].multi_select
=>
{"options"=>
  [{"id"=>"f30c827f-60ed-4d50-9eb2-fc5c8c4e0b3d",
    "name"=>"macOS",
    "color"=>"purple",
    "description"=>nil},
   {"id"=>"d5359eaf-e3b9-4f42-8d4c-046ca96c1642",
    "name"=>"iOS",
    "color"=>"red",
    "description"=>nil},
   {"id"=>"0a4afa44-55c6-4bcf-9787-6d1567319b6e",
    "name"=>"iPadOS",
    "color"=>"pink",
    "description"=>nil},
   {"id"=>"06e620cd-80d3-467a-b52f-0d9380509052",
    "name"=>"Windows",
    "color"=>"default",
    "description"=>nil}]}

おわりに

今回はページの MultiSelectProperty の変更を説明しました。multi_select は文字列の配列を設定することがオプションが設定できます。データベースにないものを設定した時には、自動的にデータベースプロパティにも追加されました。

MultiSelectProperty のマニュアルはこちらです。

MultiSelectProperty

NotionRubyMapping解説