キャンバスアプリにおける変数の理解 – Power Apps

Power Appsは、Excelと同様のアプローチでアプリを構築するプラットフォームです。変数の使用は避けがちですが、時には必要です。Power Appsでは、ユーザーのアクションによって変数を手動で更新する必要があります。グローバル変数は任意の値を保持でき、アプリ全体で使用可能です。コンテキスト変数は1つの画面内でのみ有効です。コレクションは、テーブルを持ち、データの保存や変更が可能です。これにより、アプリの動的な構成を実現します。

Power Appsの変数とExcelとの統合

Power Appsを使うと、アプリケーションを視覚的に構築でき、Excelのような感覚で使えるのが魅力です。しかし、他のプログラミングツール(Visual BasicやJavaScriptなど)での経験がある方は、Power Appsにおける変数の使い方について戸惑うかもしれません。本記事では、Excelとの関連性を説明しながら、Power Appsでの変数の使用方法について解説します。

Excelの機能とPower Appsの類似性

Excelでは、セルに数値や文字列を入力することができ、セルに書かれた式は、参照している他のセルの値が変更されるたびに自動的に再計算されます。この自動計算の仕組みは、Power Appsにおいても類似しています。Power Appsでは、セルの更新の代わりに、画面上にコントロールを追加し、それらを使用して数式に組み込んでいくことで、動的なアプリを作成できます。

たとえば、Power Apps内で、ラベルコントロール(Label1)と2つのテキスト入力コントロール(TextInput1、TextInput2)を作成し、Label1のTextプロパティに「TextInput1 + TextInput2」という数式を設定すると、常にその2つのテキスト入力の合計が自動的に表示されるようになります。

変数の使い方

Power Appsでは、時には変数を使う必要がありますが、基本的には変数を使わずに数式で済む場合がほとんどです。変数は、アプリの動作中に使用するために明示的に作成され、値の設定は数式内の関数を通じて行われます。

変数の種類

Power Appsでは、主に以下の3つの種類の変数が存在します。

  1. グローバル変数: アプリ全体から参照可能な単純な変数。Set関数を使用して設定します。
  2. コンテキスト変数: スクリーンごとに有効な変数。UpdateContext関数を使用して設定します。
  3. コレクション: 複数の値(テーブル形式)を保持できるデータ型。ClearCollect関数を使用して設定します。

変数の作成と削除

変数は、数式の中で初めて使用されたときに自動的に作成されます。具体的には、Set( X, 1 )という数式を使うことで、Xという名前のグローバル変数が数値1を持つ変数として定義されます。変数を削除するには、関連する数式をすべて削除するだけです。

変数のライフサイクルと初期値

アプリが実行されている間、すべての変数はメモリに保持されますが、アプリを終了するとその値は失われます。新しいアプリが開かれると、すべての変数の初期値はblankになります。

変数を使うタイミング

アプリの動きによっては、アクションが成り立つ条件や、ユーザーが選択したボタンで状態が変わる場合、変数が不可欠になります。たとえば、加算機能を持ったアプリで、ユーザーが数字を入力し「加算ボタン」をクリックするたびに、合計を更新する必要がある場合、この合計値は変数で追跡されます。

変数の実践的使用例: 加算機

グローバル変数による加算機の例

  1. テキスト入力コントロール(TextInput1)と2つのボタン(Button1: 加算、Button2: リセット)を追加します。
  2. Button1のOnSelectプロパティをSet(RunningTotal, RunningTotal + TextInput1.Text)に設定し、加算ボタンが押されたときに合計が更新されるようにします。
  3. Button2のOnSelectプロパティをSet(RunningTotal, 0)に設定し、リセットボタンで合計を0に戻せるようにします。

これにより、ユーザーが加算ボタンを押すたびに、合計が更新され、リセットボタンで簡単に初期化することができます。

コレクションを使用した加算機の例

数値の履歴を保持したい場合、コレクションを使うこともできます。

  1. ClearCollect(PaperTape, TextInput1.Text)を使用して、PaperTapeというコレクションに値を追加します。
  2. 合計を表示するためには、ラベルのTextプロパティにSum(PaperTape, Value)を設定します。

このようにすることで、入力したすべての値をコレクションとして保持し、必要に応じて合計を計算できるアプリを構築できます。

結論

Power Appsでは、Excelに似た感覚でアプリをビジュアルに構築できる一方で、変数の使い方についても理解が必要です。望ましいユーザー体験を実現するためには、適切なタイミングで変数やコレクションを活用することが重要です。

————-

Understand variables in canvas apps – Power Apps

Source link

The article discusses how to effectively use variables in Power Apps, comparing its model to that of Excel. Unlike traditional programming tools where variables are used to store results of calculations, Power Apps, similar to Excel, avoids the frequent use of variables for most operations. Instead, it leverages formulas that automatically recalculate based on user input. This approach simplifies app development and maintenance.

However, there are scenarios where variables become necessary, particularly when user actions dictate changes. Power Apps features three types of variables: global variables (accessible throughout the app), context variables (bound to a specific screen), and collections (tables that can hold multiple records). The article illustrates how to create an adding machine app using each of these variable types.

  1. Global Variables: These can be set with the Set function and are used for values that need to be referenced at any point in the app. They retain their value during the app session and reset upon closing.

  2. Context Variables: Created using the UpdateContext or Navigate function, context variables are local to a single screen and useful for passing internal values.

  3. Collections: Defined using ClearCollect, these store tables of values and are ideal for situations that require maintaining a list or a running total.

The article emphasizes a formula-driven approach for creating applications in Power Apps because it mirrors the automatic recalculation feature of Excel, making it easier for users familiar with Excel to transition into app development. It recommends using variables judiciously, primarily for specific functional needs while leaning towards formulas for most tasks. The guide offers detailed steps for building an adding machine with all three variable types and demonstrates how to manage data persistence with Save and Load functionalities.

In conclusion, developers can simplify their app-building process by understanding the intuitive relationship between Excel’s operations and Power Apps’ formula-based design, utilizing variables only in scenarios where state tracking is necessary.

関連記事

コメント

この記事へのコメントはありません。