8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

#PowerApps で日付でフィルターする場合 (SharePoint リスト)

Posted at

概要

SharePointをデータソースとしてPower Appsでアプリを作るといろいろと委任の問題が付きまといます。
Complex-type fieldなどは最近少しずつ委任の対応が進んできましたが、日付・日時のフィールドは2019/12時点ではまだ委任対象外です。
でも、日付でリストをフィルターしたいですよね。XX月YY日以降に期限の来るアイテム一覧とか。
委任の問題に関しては @KodamaJn さんの記事が詳しいので参照してください。

委任シリーズ: https://qiita.com/KodamaJn/items/4341865e6784847a005d

ここでは日付フィールドの委任警告の回避方法としてUnixtime(Number型)を使う方法を紹介します。

結論から先に書くと、

  1. Number型なので、委任の問題は発生しない
  2. ただSharePoint側でリストを見ると、数字なので、いつなのかわからない
    • JSON formatを利用して表示上だけ日付になるように調整
      としています。

セットアップ

SharePoint Onlineのカスタムリストに、startDateTimeという数値型の列を追加します。

Power Apps側では、リストからアプリを作成で、標準のリストアプリを作成しておきましょう。
image.png

SharePointリスト側の対応

まずリストのJSON Formatを簡単に紹介します。

Unixtimeから日付+時刻にするColumn formatting jsonは以下のようにかけます。
@now でDateTime形式にしておいて、そこから、nowのミリ秒を引いて、最後にstartDateTimeの数値を足しています。こうすると、自動的に表示が数値から日時に変換されます。

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=(@now-Number(@now)+@currentField)"
}

また、日付だけを表示したい場合にはtoLocaleDateStringを利用して、

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=toLocaleDateString(@now-Number(@now)+@currentField)"
}

これで表示変更は完了です。
image.png

参考: https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting#operators

PowerApps 側

ギャラリー内で対象のフィールドを表示する場合には、Text関数を用いて、数値から日付/日時型に変換します。

Label.Text
Text(startDateTime, "yyyy/mm/dd hh:mm")

image.png

またここがもともとやりたかったことですが、DatePickerを利用して、ギャラリーのアイテムを日付でFilterしたい場合には、GalleryのItemsプロパティに以下のような数式を記入します。

BrowseGallery.Items
Filter(SPList, startDateTime > Value(DatePicker.SelectedDate))

※この場合は選択した日付以降のアイテムだけを表示
image.png
数式バーを見てわかるとおり、いつも悩まされる青い線が出ていません!これで委任の問題は回避できました。

PowerApps --> SharePoint への登録・更新・表示

念のため、登録・表示も紹介します。

表示

表示側、DispFormでは対象のフィールドに対応するDataCardValue(ラベル)にText関数を用いて、

DataCardValueN.Text
Text(Value(Parent.Default), "yyyy/mm/dd hh:mm")

と設定します。
image.png

登録/更新

登録・更新はEditForm内の対象となるDataCardのUpdateプロパティ変更、TextInputの代わりに日付選択用のDatePickerを挿入といった変更が発生します。
詳細は割愛しますが、

startDateTime_DataCard1.Update
Value(DatePicker.SelectedDate)

データカード内のDatePickerはDefaultDateを、フォームモードに応じて変更します。

DatePickerN.DefaultDate
If(EditForm1.Mode=FormMode.New,Today(),DateTimeValue(Text(Parent.Default,"[$-en-US]yyyy/mm/dd")))

Today()部分は要件に応じて初期日付を変更してください。

以上でPower Apps側は完了です。

image.png

まとめ

委任はすごく厄介で、特にDateTime型はよく使うのに現時点でまったく委任対応されていない部分です。
今後のアップデートでどこまで対応されるか注目ですが、このようにPower AppsとSharePointにすこしずつ改良を加えることでとても使いやすくなるので、ぜひ試してみてください。

8
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?