MyNote365

Office365 , OneNote , SharePoint , PowerApps , MicrosoftFlow 関連の備忘録

【Microsoft Lists】アイテム更新をする書式設定のサンプル(随時更新)

f:id:tecchan365:20211120084723p:plain

書式設定にアップデートがありました!!!

customRowAction に新たに setValue アクションが追加され、このアクションを利用することによって、アイテム更新ができるようになりました!!

設定方法については、次の Microsoft Docs を参照ください。

docs.microsoft.com

以下、作成してみた書式設定のサンプルを掲載します。
※作成次第、随時追加していきます。

サンプルについての注意

サンプルコード内に 列の内部名 が記載されている部分があります。

f:id:tecchan365:20211121195836p:plain

もしサンプルを利用する場合は、その部分を自身のリスト定義にあわせて変更ください。

f:id:tecchan365:20211121195905p:plain

ニュースをページに降格(昇格した状態列の書式設定)

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "flex-wrap": "wrap",
    "display": "flex",
    "flex-direction": "row"
  },
  "children": [
    {
      "elmType": "div",
      "txtContent": "=if(@currentField == 0 ,'0 : Page' , if(@currentField == 1, '1 : News (Unpublished)' , if(@currentField == 2 , '2 : News','') ) )",
      "style": {
        "box-sizing": "border-box",
        "padding": "4px 8px 5px 8px",
        "display": "flex",
        "border-radius": "16px",
        "height": "27px",
        "align-items": "center",
        "white-space": "nowrap",
        "overflow": "hidden",
        "margin": "4px 4px 4px 4px",
        "border": "1px solid"
      },
      "attributes": {
        "class": "=if(@currentField == 0 ,'ms-fontColor-themePrimary ms-borderColor-themePrimary ms-bgColor-white' , if(@currentField == 1, 'ms-fontColor-themePrimary ms-borderColor-themePrimary ms-bgColor-themeLighter' , if(@currentField == 2 , 'ms-fontColor-white ms-borderColor-themePrimary ms-bgColor-themePrimary','') ) )"
      }
    },
    {
      "elmType": "div",
      "style": {
        "margin-left": "10px",
        "font-size": "18px",
        "cursor": "pointer",
        "display": "=if(@currentField == 0 , 'none' ,'')"
      },
      "attributes": {
        "iconName": "PageEdit",
        "class": "ms-fontColor-themePrimary"
      },
      "customCardProps": {
        "openOnEvent": "hover",
        "directionalHint": "rightCenter",
        "isBeakVisible": true,
        "formatter": {
          "elmType": "div",
          "txtContent": "Change to 0:Page",
          "style": {
            "padding": "10px 20px 10px 20px",
            "cursor": "pointer"
          },
          "attributes": {
            "class": "ms-bgColor-themeLighter--hover"
          },
          "customRowAction": {
            "action": "setValue",
            "actionInput": {
              "PromotedState": "0"
            }
          }
        }
      }
    }
  ]
}

クリスマスプレゼント(ギャラリービューの書式設定)

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/tile-formatting.schema.json",
  "width": 220,
  "height": 220,
  "hideSelection": true,
  "formatter": {
    "elmType": "div",
    "style": {
      "padding": "10px"
    },
    "children": [
      {
        "elmType": "div",
        "style": {
          "display": "=if([$Opened] != 1 , 'none' , '')",
          "width": "100%",
          "height": "100%"
        },
        "children": [
          {
            "elmType": "div",
            "style": {
              "display": "flex",
              "align-items": "center",
              "justify-content": "center",
              "height": "90%",
              "width": "100%"
            },
            "children": [
              {
                "elmType": "img",
                "style": {
                  "max-height": "100%",
                  "max-width": "100%"
                },
                "attributes": {
                  "src": "[$Present.serverRelativeUrl]"
                }
              }
            ]
          },
          {
            "elmType": "div",
            "txtContent": "[$Title]",
            "style": {
              "display": "flex",
              "align-items": "center",
              "justify-content": "center",
              "height": "10%",
              "font-size": "18px",
              "font-weight": "bold"
            }
          }
        ]
      },
      {
        "elmType": "div",
        "style": {
          "display": "=if([$Opened] != 1 , 'flex' , 'none')",
          "flex-direction": "column",
          "width": "100%",
          "height": "100%",
          "cursor": "pointer"
        },
        "customRowAction": {
          "action": "setValue",
          "actionInput": {
            "Opened": "=if([$Opened] != 1 , 1 , 0)"
          }
        },
        "children": [
          {
            "elmType": "div",
            "style": {
              "display": "flex",
              "justify-content": "center",
              "height": "20%",
              "position": "relative",
              "top": "5%"
            },
            "children": [
              {
                "elmType": "svg",
                "style": {
                  "fill": "[$RibbonColor]"
                },
                "attributes": {
                  "viewBox": "0 0 40 20"
                },
                "children": [
                  {
                    "elmType": "path",
                    "style": {
                      "stroke": "rgba(0,0,0,0.2)"
                    },
                    "attributes": {
                      "d": "M 20,20 L0,10 L0,0 L20,10 L40,0 L40,10 L20,20 Z"
                    }
                  }
                ]
              }
            ]
          },
          {
            "elmType": "div",
            "style": {
              "display": "flex",
              "flex-direction": "column",
              "align-items": "center",
              "justify-content": "center",
              "position": "relative",
              "width": "100%",
              "height": "80%"
            },
            "children": [
              {
                "elmType": "div",
                "style": {
                  "width": "100%",
                  "height": "20%",
                  "background-color": "[$BoxColor]",
                  "box-shadow": "0 0 5px rgba(0,0,0,0.6)",
                  "z-index": "2"
                }
              },
              {
                "elmType": "div",
                "style": {
                  "width": "90%",
                  "height": "80%",
                  "background-color": "[$BoxColor]",
                  "box-shadow": "0 0 3px rgba(0,0,0,0.6)",
                  "z-index": "1"
                }
              },
              {
                "elmType": "div",
                "style": {
                  "position": "absolute",
                  "width": "15%",
                  "height": "100%",
                  "background-color": "[$RibbonColor]",
                  "box-shadow": "0 0 3px rgba(0,0,0,0.6)",
                  "z-index": "3"
                }
              },
              {
                "elmType": "div",
                "style": {
                  "position": "absolute",
                  "top": "45%",
                  "width": "90%",
                  "height": "15%",
                  "background-color": "[$RibbonColor]",
                  "box-shadow": "0 0 3px rgba(0,0,0,0.6)",
                  "z-index": "4"
                }
              }
            ]
          }
        ]
      }
    ]
  }
}

f:id:tecchan365:20211202214005p:plain

アイコンボタン(はい/いいえ列の書式設定)

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "margin": "5px"
  },
  "children": [
    {
      "elmType": "div",
      "style": {
        "width": "40px",
        "height": "40px",
        "border-radius": "50%",
        "display": "flex",
        "justify-content": "center",
        "align-items": "center",
        "font-size": "20px",
        "cursor": "pointer"
      },
      "attributes": {
        "class": "=if(@currentField , 'ms-bgColor-themePrimary ms-bgColor-themeDark--hover ms-fontColor-white' , 'ms-bgColor-neutralQuaternaryAlt ms-bgColor-neutralTertiaryAlt--hover ms-fontColor-neutralSecondary')",
        "iconName": "BeerMug"
      },
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "Beer": "=if(@currentField , '0' , '1' )"
        }
      }
    }
  ]
}

iconName には、Fluent Icons の アイコン名を設定してください。

f:id:tecchan365:20211201054647p:plain

developer.microsoft.com

上記のサイトでアイコンが見つけにくい場合は、次の Chris Kent さんが作成されたサイトで検索するのがおすすめです。

www.flicon.io

リンククリック数とクリックしたユーザーの記録(ハイパーリンク列の書式設定)

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "display": "=if(@currentField == '' , 'none' , '')"
  },
  "children": [
    {
      "elmType": "div",
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "LinkClickedCount": "= [$LinkClickedCount] + 1",
          "LinkClickedUser": "=appendTo([$LinkClickedUser], @me)"
        }
      },
      "children": [
        {
          "elmType": "a",
          "style": {
            "text-decoration": "none",
            "padding": "5px 10px 5px 10px",
            "font-weight": "bold",
            "border-radius": "5px",
            "font-size": "13px",
            "white-space": "nowrap"
          },
          "txtContent": "@currentField.desc",
          "attributes": {
            "href": "@currentField",
            "target": "_blank",
            "class": "ms-fontColor-themePrimary ms-bgColor-themeLighter ms-fontColor-white--hover ms-bgColor-themePrimary--hover"
          }
        }
      ]
    }
  ]
}

リアクション(複数ユーザー列の書式設定)

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "div",
      "style": {
        "font-size": "18px",
        "padding": "2px 7px 2px 7px",
        "cursor": "pointer",
        "border-radius": "10px"
      },
      "attributes": {
        "class": "ms-bgColor-themeTertiary--hover"
      },
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "User": "=if(indexOf(@currentField.email , @me) > -1 , removeFrom(@currentField.email , @me) , appendTo(@currentField.email , @me) )"
        }
      },
      "customCardProps": {
        "openOnEvent": "hover",
        "directionalHint": "rightCenter",
        "isBeakVisible": true,
        "formatter": {
          "elmType": "div",
          "style": {
            "max-height": "300px",
            "padding": "5px 20px 5px 20px",
            "display": "flex",
            "flex-direction": "column"
          },
          "children": [
            {
              "elmType": "div",
              "txtContent": "= '💖 ' + length(@currentField)",
              "style": {
                "padding": "10px",
                "font-weight": "bold",
                "font-size": "20px"
              }
            },
            {
              "elmType": "div",
              "children": [
                {
                  "forEach": "personIterator in @currentField",
                  "elmType": "div",
                  "style": {
                    "margin-bottom": "5px",
                    "display": "flex",
                    "align-items": "center"
                  },
                  "children": [
                    {
                      "elmType": "img",
                      "style": {
                        "width": "32px",
                        "height": "32px",
                        "border-radius": "50%",
                        "margin-right": "5px"
                      },
                      "attributes": {
                        "src": "=getUserImage([$personIterator.email], 'S')",
                        "title": "[$personIterator.title]"
                      }
                    },
                    {
                      "elmType": "span",
                      "txtContent": "[$personIterator.title]"
                    }
                  ]
                }
              ]
            }
          ]
        }
      },
      "children": [
        {
          "elmType": "span",
          "style": {
            "margin-right": "5px"
          },
          "txtContent": "💖"
        },
        {
          "elmType": "span",
          "style": {
            "font-weight": "=if(indexOf(@currentField.email , @me) > -1 , 'bold' ,'' )"
          },
          "txtContent": "=length(@currentField)"
        }
      ]
    }
  ]
}

タスク完了チェック (はい/いいえ列の書式設定)

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "children": [
    {
      "elmType": "div",
      "style": {
        "cursor": "pointer",
        "font-size": "23px"
      },
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "Completed": "=if(@currentField , '0' , '1' )"
        }
      },
      "attributes": {
        "iconName": "=if(@currentField , 'BoxCheckmarkSolid' , 'Checkbox' )",
        "class": "ms-fontColor-themePrimary"
      }
    }
  ]
}

Count up , Count down(数値列の書式設定)

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "display": "flex",
    "flex-direction": "row",
    "align-items": "center",
    "width": "180px"
  },
  "children": [
    {
      "elmType": "div",
      "style": {
        "width": "20px",
        "height": "20px",
        "display": "flex",
        "align-items": "center",
        "justify-content": "center",
        "cursor": "pointer"
      },
      "attributes": {
        "class": "ms-bgColor-themePrimary ms-bgColor-themeDark--hover ms-fontColor-white",
        "iconName": "CalculatorSubtract"
      },
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "Number": "= @currentField - 1"
        }
      }
    },
    {
      "elmType": "div",
      "txtContent": "@currentField",
      "style": {
        "font-size": "30px",
        "width": "140px",
        "text-align": "center"
      }
    },
    {
      "elmType": "div",
      "style": {
        "width": "20px",
        "height": "20px",
        "display": "flex",
        "align-items": "center",
        "justify-content": "center",
        "cursor": "pointer"
      },
      "attributes": {
        "class": "ms-bgColor-themePrimary ms-bgColor-themeDark--hover ms-fontColor-white",
        "iconName": "CalculatorAddition"
      },
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "Number": "= @currentField + 1"
        }
      }
    }
  ]
}

トグルボタン (はい/いいえ列の書式設定)

通常のトグル

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "display": "flex",
    "width": "100%",
    "height": "100%",
    "align-items": "center"
  },
  "children": [
    {
      "elmType": "div",
      "style": {
        "display": "flex",
        "width": "60px",
        "height": "30px",
        "border-radius": "15px",
        "align-items": "center",
        "flex-direction": "row",
        "justify-content": "=if(@currentField , 'flex-end' , 'flex-start')",
        "cursor": "pointer"
      },
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "YesNo": "=if(@currentField , '0' , '1' )"
        }
      },
      "attributes": {
        "class": "=if(@currentField , 'ms-bgColor-themePrimary' , 'ms-bgColor-neutralTertiaryAlt')"
      },
      "children": [
        {
          "elmType": "div",
          "style": {
            "width": "18px",
            "height": "18px",
            "margin-left": "6px",
            "margin-right": "6px",
            "border-radius": "50%"
          },
          "attributes": {
            "class": "ms-bgColor-white"
          }
        }
      ]
    }
  ]
}

顔アイコン有り

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "display": "flex",
    "width": "100%",
    "height": "100%",
    "align-items": "center"
  },
  "children": [
    {
      "elmType": "div",
      "style": {
        "display": "flex",
        "width": "60px",
        "height": "30px",
        "border-radius": "15px",
        "align-items": "center",
        "flex-direction": "row",
        "justify-content": "=if(@currentField , 'flex-end' , 'flex-start')",
        "cursor": "pointer"
      },
      "customRowAction": {
        "action": "setValue",
        "actionInput": {
          "YesNo": "=if(@currentField , '0' , '1' )"
        }
      },
      "attributes": {
        "class": "=if(@currentField , 'ms-bgColor-themePrimary' , 'ms-bgColor-neutralTertiaryAlt')"
      },
      "children": [
        {
          "elmType": "div",
          "style": {
            "display": "flex",
            "align-items": "center",
            "justify-content": "center",
            "font-size": "22px",
            "width": "18px",
            "height": "18px",
            "margin-left": "6px",
            "margin-right": "6px",
            "border-radius": "50%"
          },
          "attributes": {
            "class": "=if(@currentField , 'ms-fontColor-themePrimary ms-bgColor-white ' , 'ms-fontColor-neutralTertiaryAlt ms-bgColor-white')",
            "iconName": "=if(@currentField , 'Emoji2' , 'EmojiDisappointed')"
          }
        }
      ]
    }
  ]
}

※このサンプルは、次の PnP のサンプルを利用しています。

github.com