了解 PowerApps 中的變數Understand variables in PowerApps

如果您使用其他的程式設計工具,例如 Visual Basic 或 JavaScript,您可能會問︰變數在哪裡?If you've used another programming tool, such as Visual Basic or JavaScript, you may be asking: Where are the variables? PowerApps 有點不同,而且需要不同的方法。PowerApps is a little different and requires a different approach. 並非觸達變數,而是問自己︰我會在 Excel 中做什麼?Instead of reaching for a variable, ask yourself: What would I do in Excel?

在其他工具中,您可能會明確執行計算,並在變數中儲存結果。In other tools, you may have explicitly performed a calculation and stored the result in a variable. 不過,PowerApps 和 Excel 都會隨著輸入資料變更而自動重新計算公式,因此您通常不需要建立和更新變數。However, PowerApps and Excel both automatically recalculate formulas as the input data changes, so you usually don't need to create and update variables. 透過這種方式,可能的話,您可以更輕鬆地建立、了解和維護您的應用程式。By taking this approach whenever possible, you can more easily create, understand, and maintain your app.

在某些情況下,您必須在 PowerApps 中使用變數,這會新增行為公式來擴充 Excel 的模型。In some cases, you'll need to use variables in PowerApps, which extends Excel's model by adding behavior formulas. 例如,當使用者選取一個按鈕時,就會執行這些公式。These formulas run when, for example, a user selects a button. 在行為公式內,設定要在其他公式中使用的變數通常很有用。Within a behavior formula, it's often helpful to set a variable to be used in other formulas.

一般而言,避免使用變數。In general, avoid using variables. 但有時候,只有一個變數可以啟用您想要的體驗。But sometimes only a variable can enable the experience you want.

將 Excel 轉譯至 PowerAppsTranslate Excel into PowerApps

ExcelExcel

讓我們來檢視 Excel 的運作方式。Let's review how Excel works. 資料格可以包含值,例如數字或字串,或根據其他資料格的值之公式。A cell can contain a value, such as a number or a string, or a formula that's based on the values of other cells. 使用者在儲存格中輸入不同的值之後,Excel 會依新值的任何公式自動重新計算。After the user enters a different value into a cell, Excel automatically recalculates any formulas that depend on the new value. 您不必進行任何程式設計以啟用這項行為。You don't have to do any programming to enable this behavior.

Excel 沒有變數。Excel doesn't have variables. 包含公式的資料格值會依據其輸入而變更,但沒有任何方法可記住公式的結果,並將它儲存在儲存格中或任何地方。The value of a cell that contains a formula changes based on its input, but there's no way to remember the result of a formula and store it in a cell or anywhere else. 如果您變更儲存格的值,可能會變更整個試算表,且會遺失任何先前計算的值。If you change a cell's value, the entire spreadsheet may change, and any previously calculated values are lost. Excel 使用者可以複製並貼上資料格,但會在使用者的手動控制之下進行,而且不可能有公式。An Excel user can copy and paste cells, but that's under the user's manual control and isn't possible with formulas.

PowerAppsPowerApps

您在 PowerApps 中建立的應用程式行為非常類似 Excel。Apps that you create in PowerApps behave very much like Excel. 並非更新資料格,您可以在螢幕上想要的任何位置新增控制項,並將它們命名以在公式中使用。Instead of updating cells, you can add controls wherever you want on a screen and name them for use in formulas.

例如,您可以藉由新增名為 TextBox1標籤控制項,以及兩個名為 TextInput1TextInput2文字輸入控制項來複寫應用程式中的 Excel 行為。For example, you can replicate the Excel behavior in an app by adding a Label control, named TextBox1, and two Text input controls, named TextInput1 and TextInput2. 如果您接著將 TextBox1Text 屬性設定為 TextInput1 + TextInput2,一律會自動顯示 TextInput1TextInput2 中的數字總和 (無論數字為何)。If you then set the Text property of TextBox1 to TextInput1 + TextInput2, it will always shows the sum of whatever numbers are in TextInput1 and TextInput2 automatically.

請注意,已選取 TextBox1 控制項,在螢幕頂端的公式列中顯示其文字公式。Notice that the TextBox1 control is selected, showing its Text formula in the formula bar at the top of the screen. 我們在這裡找到公式 TextInput1 + TextInput2Here we find the formula TextInput1 + TextInput2. 就像在 Excel 活頁簿中的儲存格之間建立相依性,這個公式會建立這些控制項之間的相依性。This formula creates a dependency between these controls, just as dependencies are created between cells in an Excel workbook. 讓我們變更 TextInput1 的值:Let's change the value of the TextInput1:

TextBox1 的公式已自動重新計算,顯示新的值。The formula for TextBox1 has been automatically recalculated, showing the new value.

在 PowerApps 中,您使用公式不只可以判斷控制項的主要值,還有格式化等屬性。In PowerApps, you can use formulas to determine not only the primary value of a control but also properties such as formatting. 在下一個範例中,標籤之 Color 屬性的公式會自動以紅色顯示負數的值。In the next example, a formula for the Color property of the label will automatically show negative values in red. If 函式從 Excel 看起來應該很熟悉:The If function should look very familiar from Excel:
If( Value(TextBox1.Text) < 0, Red, Black )If( Value(TextBox1.Text) < 0, Red, Black )

現在,如果在 TextBox1.Text 中我們的計算結果是負數,數字會以紅色顯示︰Now, if the result of our calculation in TextBox1.Text is negative, the number will be shown in red:

您可以在多種情況下使用公式:You can use formulas for a wide variety of scenarios:

  • 藉由使用裝置的 GPS,地圖控制項可以透過使用 Location.LatitudeLocation.Longitude 的公式來顯示您目前的位置。By using your device's GPS, a map control can display your current location with a formula that uses Location.Latitude and Location.Longitude. 當您移動時,地圖會自動追蹤您的位置。As you move, the map will automatically track your location.
  • 其他使用者可以更新資料來源Other users can update data sources. 例如,小組的其他人可能會更新 SharePoint 清單中的項目。For example, others on your team might update items in a SharePoint list. 當您重新整理資料來源時,會自動重新計算任何相依的公式以反映更新的資料。When you refresh a data source, any dependent formulas are automatically recalculated to reflect the updated data. 進一步的範例,您可以將資源庫的 Items 屬性設定為公式 Filter( SharePointList ),它會自動顯示新篩選的記錄集Furthering the example, you might set a gallery's Items property to the formula Filter( SharePointList ), which will automatically display the newly filtered set of records.

權益Benefits

使用公式來建置應用程式有許多優點︰Using formulas to build apps has many advantages:

  • 如果您會使用 Excel,便會使用 PowerApps。If you know Excel, you know PowerApps. 模型和公式語言都相同。The model and formula language are the same.
  • 如果您使用過其他的程式設計工具,想一想需要多少程式碼才能完成這些範例。If you've used other programming tools, think about how much code would be required to accomplish these examples. 在 Visual Basic 中,您必須在每個文字輸入控制項上撰寫變更事件的事件處理常式。In Visual Basic, you'd need to write an event handler for the change event on each text-input control. 要在這之中每一個執行計算的程式碼是多餘的,且可能會不同步,或是您必須撰寫一個常見的副程式。The code to perform the calculation in each of these is redundant and could get out of sync, or you'd need to write a common subroutine. 在 PowerApps 中,您可以使用單一行的公式來您完成所有的工作。In PowerApps, you accomplished all of that with a single, one-line formula.
  • 若要了解 TextBox1 的文字來自何處,您完全清楚要從哪些地方查詢︰Text 屬性的公式。To understand where TextBox1's text is coming from, you know exactly where to look: the formula in the Text property. 沒有其他方法可影響此控制項的文字。There's no other way to affect the text of this control. 在傳統程式設計工具中,任何事件處理常式或副程式皆可能從程式中任何地方變更標籤的值。In a traditional programming tool, any event handler or subroutine could change the value of the label, from anywhere in the program. 這會使變數的變更時機和位置難以追蹤。This can make it hard to track down when and where a variable was changed.
  • 如果使用者變更滑桿控制項,然後改變心意,他們可以將滑桿變更移回其原始值。If the user changes a slider control and then changes their mind, they can change the slider back to its original value. 且如同沒有任何變更︰應用程式會和以前一樣顯示相同的控制項值。And it's as if nothing had ever changed: the app shows the same control values as it did before. 實驗沒有後續發展,並詢問「假設」,如同在 Excel 中沒有一樣。There are no ramifications for experimenting and asking "what if," just as there are none in Excel.

一般而言,如果您可以使用公式來達成效果,您會比較理想。In general, if you can achieve an effect by using a formula, you'll be better off. 讓 PowerApps 中的公式引擎為您工作。Let the formula engine in PowerApps work for you.

知道何時要使用變數Know when to use variables

讓我們變更我們簡單的新增項目以便如同舊式加法器般運作,具有計算加總。Let's change our simple adder to act like an old-fashioned adding machine, with a running total. 如果您選取 [新增] 按鈕,您會將數字加入計算加總。If you select an Add button, you'll add a number to the running total. 如果您選取 [清除] 按鈕,您會將計算加總重設為零。If you select a Clear button, you'll reset the running total to zero.

我們的加法器會使用 Excel 中不存在的項目︰按鈕。Our adding machine uses something that doesn't exist in Excel: a button. 在此應用程式中,您無法僅使用公式來計算加總,因為它的值取決於一系列使用者採取的動作。In this app, you can't use only formulas to calculate the running total because its value depends on a series of actions that the user takes. 相反地,我們必須以手動方式記錄並更新計算加總。Instead, our running total must be recorded and updated manually. 大部分的程式設計工具會將此資訊儲存於變數。Most programming tools store this information in a variable.

有時候,您的應用程式將需要變數進行您想要的行為。You'll sometimes need a variable for your app to behave the way you want. 但此方法需要注意的事項︰But the approach comes with caveats:

  • 您必須手動更新的計算加總。You must manually update the running total. 自動重新計算不會為您這麼做。Automatic recalculation won't do it for you.
  • 計算加總無法再根據其他控制項的值計算。The running total can no longer be calculated based on the values of other controls. 這取決於使用者選取 [新增] 按鈕的次數,以及每次在文字輸入控制項中的值。It depends on how many times the user selected the Add button and what value was in the text-input control each time. 使用者輸入 77 並選取 [新增] 兩次,還是針對每個新增項目指定 24 以及 130?Did the user enter 77 and select Add twice, or did they specify 24 and 130 for each of the additions? 總計達到 154 後,您便無法區分其間的差異。You can't tell the difference after the total has reached 154.
  • 總計的變更可能來自不同的路徑。Changes to the total can come from different paths. 在此範例中,[新增] 和 [清除] 按鈕皆可以更新總數。In this example, both the Add and Clear buttons can update the total. 如果應用程式的行為並未如您預期,哪一個按鈕會造成問題?If the app doesn't behave the way you expect, which button is causing the problem?

建立全域變數Create a global variable

若要建立我們的加法器,我們需要一個變數來保存計算加總。To create our adding machine, we require a variable to hold the running total. PowerApps 中最簡單好用的變數是全域變數。The simplest variables to work with in PowerApps are global variables.

全域變數如何運作:How global variables work:

  • Set 函式設定全域變數值。You set the value of the global variable with the Set function. Set( MyVar, 1 ) 將全域變數 MyVar 的值設為 1Set( MyVar, 1 ) sets the global variable MyVar to a value of 1.
  • 您可以參照與 Set 函式搭配使用的名稱來使用全域變數。You use the global variable by referencing the name used with the Set function. 在此情況下,MyVar 會回到 1In this case, MyVar will return 1.
  • 全域變數可以保留任何值,包括字串、數字、記錄和資料表Global variables can hold any value, including strings, numbers, records, and tables.

讓我們使用全域變數來重建加法器︰Let's rebuild our adding machine by using a global variable:

  1. 新增名為 TextInput1 的文字輸入控制項,和名為 Button1Button2 的兩個按鈕。Add a text-input control, named TextInput1, and two buttons, named Button1 and Button2.

  2. Button1Text 屬性設為 "Add",並將 Button2Text 屬性設為 "Clear"Set the Text property of Button1 to "Add", and set the Text property of Button2 to "Clear".

  3. 每當使用者選取 [新增] 按鈕時若要更新計算加總,將其 OnSelect 屬性設為以下公式︰To update the running total whenever a user selects the Add button, set its OnSelect property to this formula:

    Set( RunningTotal, RunningTotal + Text1 )Set( RunningTotal, RunningTotal + Text1 )

    使用者第一次選取 [新增] 按鈕且呼叫 Set 時,會建立 RunningTotal 與空白的預設值。The first time a user selects the Add button and Set is called, RunningTotal is created with a default value of blank. 此外,它將會視為零。In the addition, it will be treated as a zero.

  4. 每當使用者選取 [清除] 按鈕時若要將計算加總設為 0,將其 OnSelect 屬性設為以下公式︰To set the running total to 0 whenever the user selects the Clear button, set its OnSelect property to this formula:

    Set( RunningTotal, 0 )Set( RunningTotal, 0 )

  5. 新增標籤控制項,並將其 Text 屬性設定為 RunningTotalAdd a Label control, and set its Text property to RunningTotal.

    此公式會自動重新計算,並會在其根據使用者選取的按鈕變更時,為使用者顯示 RunningTotal 的值。This formula will automatically be recalculated and show the user the value of RunningTotal as it changes based on the buttons that the user selects.

  6. 預覽應用程式,我們會有如上所述的加法器。Preview the app, and we have our adding machine as described above. 在文字方塊中輸入數字,然後按 [新增] 按鈕幾次。Enter a number in the text box and press the Add button a few times. 準備就緒時,使用 Esc 鍵返回撰寫體驗。When ready, return to the authoring experience using the Esc key.

  7. 若要查看全域變數的值,選取 [檔案] 功能表,然後在左側窗格中選取 [變數]。To see our global variable's value, select the File menu and select Variables in the left hand pane.

  8. 若要查看變數定義及使用的所有位置,請選取變數。To see all the places where our variable is defined and used, select it.

變數類型Types of variables

PowerApps 中有三種變數類型:There are three types of variables in PowerApps:

變數類型Variables type 範圍Scope 說明Description FunctionsFunctions
全域變數Global variables 應用程式App 最簡單好用。Simplest to use. 保留數字、文字字串、布林值、記錄、資料表等,可以在應用程式中的任何位置參考。Holds a number, text string, Boolean, record, table, etc. that can be references from anywhere in the app. SetSet
內容變數Context variables 畫面Screen 很適合用於將值傳遞至畫面,如同其他語言中的參數至程序。Great for passing values to a screen, much like parameters to a procedure in other languages. 只能從一個畫面參考。Can only be referenced from one screen. UpdateContextUpdateContext
NavigateNavigate
集合Collections 應用程式App 保留資料表,可以在應用程式中的任何位置參考。Holds a table that can be references from anywhere in the app. 允許修改資料表的內容,而不是設定為一個整體。Allows the contents of the table to be modified rather than being set as a whole. 可以儲存至本機裝置,以供稍後使用。Can be saved to the local device for later use. CollectCollect
ClearCollectClearCollect
PatchPatch
UpdateUpdate
RemoveRemove
SaveDataSaveData
LoadDataLoadData
等等。etc.

所有變數會在用於 SetUpdateContextNavigateCollect 函式中時隱含建立。All variables are created implicitly when used in Set, UpdateContext, Navigate, or Collect functions. 變數沒有明確宣告,與其他程式設計工具不同。There is no explicit declaration of variables as is done in other programming tools. 變數的類型也會隱含衍生自置於其中的值。The types of the variables are also derived implicitly from the values that are placed in them.

應用程式執行時,所有變數會保留在記憶體中。All variables are held in memory while the app is running. 應用程式關閉後,保留在變數中的值將會遺失。After the app closes, the values held in the variables are lost. 您可以使用 PatchCollect 函式將變數內容儲存在資料來源中,或者如果是集合,您可以使用 SaveData 函式儲存至本機裝置。You can store the contents of a variable in a Data Source using Patch or Collect functions, or in the case of collections you can store to the local device with the SaveData function. 第一次載入應用程式時,所有變數都具有空白值。When the app is first loaded, all variables will have the blank value.

使用變數名稱讀取其值。You use the variables name to read its value. 例如,一旦以 Set( MyColor, Red ) 定義,您可以直接在任何可使用色彩值的位置使用 MyVar,它將會被取代為紅色For example, once defined with Set( MyColor, Red ) you can simply use MyVar anywhere a color value can be used and it will be replaced with the Red. 有可能全域變數或集合會與內容變數具有相同的名稱。It is possible to have a global variable or collection with the same name as a context variable. 在此情況下,內容變數優先。In this case, the context variable will take precedence. 您仍然可以使用去除混淆運算子 @[MyColor] 來參考全域變數或集合。You can still reference the global variable or collection using the disambiguation operator @[MyColor].

建立內容變數Create a context variable

讓我們看看如何使用內容變數 (而非全域變數) 來建立加法器。Let's look at how our adding machine would be created using a context variable instead of a global variable.

內容變數的運作方式︰How context variables work:

  • 您使用 UpdateContext函式建立並設定內容變數。You create and set context variables by using the UpdateContext function. 如果第一次更新時內容變數尚未存在,就會以空白預設值來建立。If a context variable doesn't already exist when first updated, it will be created with a default value of blank.
  • 您可以使用記錄來建立和更新內容變數。You create and update context variables with records. 在其他的程式設計工具,您通常使用 "=" 來指派,如 "x = 1"。In other programming tools, you commonly use "=" for assignment, as in "x = 1". 針對內容變數,改為使用 {x: 1}For context variables, use { x: 1 } instead. 當您使用內容變數時,請直接使用它的名稱。When you use a context variable, use its name directly.
  • 當螢幕顯示時,您也可以使用 Navigate 函式來設定內容變數。You can also set a context variable when a screen is displayed, by using the Navigate function. 如果您將螢幕視為一種程序或副程式,這類似於其他程式設計工具中傳遞的參數。If you think of a screen as a kind of procedure or subroutine, this is similar to parameter passing in other programming tools.
  • 除了 Navigate,內容變數僅限於單一畫面的內容,也就是他們取得其名稱之處。Except for Navigate, context variables are limited to the context of a single screen, which is where they get their name. 您無法在此內容之外使用或設定它們。You can't use or set them outside of this context.
  • 內容變數可以保留任何值,包括字串、數字、記錄和資料表Context variables can hold any value, including strings, numbers, records, and tables.

讓我們使用內容變數來重建我們的加法器︰Let's rebuild our adding machine by using a context variable:

  1. 新增名為 TextInput1 的文字輸入控制項,和名為 Button1Button2 的兩個按鈕。Add a text-input control, named TextInput1, and two buttons, named Button1 and Button2.

  2. Button1Text 屬性設為 "Add",並將 Button2Text 屬性設為 "Clear"Set the Text property of Button1 to "Add", and set the Text property of Button2 to "Clear".

  3. 每當使用者選取 [新增] 按鈕時若要更新計算加總,將其 OnSelect 屬性設為以下公式︰To update the running total whenever a user selects the Add button, set its OnSelect property to this formula:

    UpdateContext( { RunningTotal: RunningTotal + Text1 } )UpdateContext( { RunningTotal: RunningTotal + Text1 } )

    使用者第一次選取 [新增] 按鈕且呼叫 UpdateContext 時,會建立RunningTotal 與空白的預設值。The first time a user selects the Add button and UpdateContext is called, RunningTotal is created with a default value of blank. 此外,它將會視為零。In the addition, it will be treated as a zero.

  4. 每當使用者選取 [清除] 按鈕時若要將計算加總設為 0,將其 OnSelect 屬性設為以下公式︰To set the running total to 0 whenever the user selects the Clear button, set its OnSelect property to this formula:

    UpdateContext( { RunningTotal: 0 } )UpdateContext( { RunningTotal: 0 } )

    同樣地,UpdateContext 會與 UpdateContext( { RunningTotal: 0 } ) 公式搭配使用。Again, UpdateContext is used with the formula UpdateContext( { RunningTotal: 0 } ).

  5. 新增標籤控制項,並將其 Text 屬性設定為 RunningTotalAdd a Label control, and set its Text property to RunningTotal.

    此公式會自動重新計算,並會在其根據使用者選取的按鈕變更時,為使用者顯示 RunningTotal 的值。This formula will automatically be recalculated and show the user the value of RunningTotal as it changes based on the buttons that the user selects.

  6. 預覽應用程式,我們會有如上所述的加法器。Preview the app and we have our adding machine as described above. 在文字方塊中輸入數字,然後按 [新增] 按鈕幾次。Enter a number in the text box and press the Add button a few timnes. 準備就緒時,使用 Esc 鍵返回撰寫體驗。When ready, return to the authoring experience using the Esc key.

  7. 巡覽至畫面時,可以設定內容變數的值。You can set the value of a context variable while navigating to a screen. 這適用於將「內容」或「參數」從一個畫面傳遞至另一畫面。This is useful for passing "context" or "parameters" from one screen to another. 若要查看,請插入新畫面,然後插入按鈕,其 OnSelect 屬性設定為:To see this, insert a new screen, and insert a button with the OnSelect property set to:

    Navigate( Screen1, None, { RunningTotal: -1000 } )Navigate( Screen1, None, { RunningTotal: -1000 } )

    Screen2 上選取此按鈕 (可在撰寫時執行,只要朝末端選取按鈕即可) 會顯示 Screen1,並將內容變數 RunningTotal 設定為 -1000。Selecting this button on Screen2 (which you can do while authoring if you select the button toward the ends) will show Screen1 and also set the context variable RunningTotal to -1000.

  8. 若要查看內容變數的值,選取 [檔案] 功能表,然後在左側窗格中選取 [變數]。To see our context variable's value, select the File menu and select Variables in the left hand pane.

  9. 若要查看內容變數定義及使用的位置,請選取它。To see where your context variable is defined and used, select it.

建立集合Create a collection

最後,讓我們看看以集合建立加法器的方式。Finally, let's look at creating our adding machine with a collection. 由於集合會保存可以輕鬆修改的資料表,我們會讓加法器在輸入值時保留每個值的「紙帶」。Since a collection holds a table that is easy to modify, we will make this adding machine keep a "paper tape" of each value as they are entered.

集合的運作方式︰How collections work:

  • 使用 ClearCollect 函式來建立和設定集合。Create and set collections by using the ClearCollect function. 您可以改為使用 Collect 函式,但它實際上需要另一個變數,而不是取代舊的變數。You can use the Collect function instead, but it will effectively require another variable instead of replacing the old one.
  • 集合是一種資料來源,因此是資料表。A collection is a kind of data source and, therefore, a table. 若要存取集合中的單一值,請使用 First 函式,然後從產生的記錄中擷取一個欄位。To access a single value in a collection, use the First function, and extract one field from the resulting record. 如果您使用了單一值與 ClearCollect,這會是 Value 欄位,如此範例所示︰If you used a single value with ClearCollect, this will be the Value field, as in this example:
    First( VariableName ).ValueFirst( VariableName ).Value

讓我們使用集合重新建立我們的加法器︰Let's recreate our adding machine by using a collection:

  1. 新增名為 TextInput1文字輸入控制項,和名為 Button1Button2 的兩個按鈕。Add a Text input control, named TextInput1, and two buttons, named Button1 and Button2.

  2. Button1Text 屬性設為 "Add",並將 Button2Text 屬性設為 "Clear"Set the Text property of Button1 to "Add", and set the Text property of Button2 to "Clear".

  3. 每當使用者選取 [新增] 按鈕時若要更新計算加總,將其 OnSelect 屬性設為以下公式︰To update the running total whenever a user selects the Add button, set its OnSelect property to this formula:

    Collect( PaperTape, TextInput1.Text )Collect( PaperTape, TextInput1.Text )

    此公式會將新值新增至集合結尾。This formula will add the new value to the end of the collection. 由於我們要新增單一值,Collect 會自動將它放在單一資料行資料表中,該資料行名稱為 Value,稍後會使用到。Since we are adding a single value, Collect will automatically place it in a single column table with the column name Value which we will use later.

  4. 若要在使用者選取 [清除] 按鈕時清除紙帶,將其 OnSelect 屬性設為以下公式︰To clear our paper tape when the user selects the Clear button, set its OnSelect property to this formula:

    Clear( PaperTape )Clear( PaperTape )

  5. 若要顯示計算加總,請新增標籤,並將其 Text 屬性設定為下列公式:To display the running total, add a label, and set its Text property to this formula:

    Sum( PaperTape, Value )Sum( PaperTape, Value )

  6. 若要執行加法器,請按下 F5 鍵開啟 [預覽],並在文字輸入控制項中輸入數字,然後選取按鈕。To run the adding machine, press F5 to open Preview, enter numbers in the text-input control, and select buttons.

  7. 若要返回預設工作區,請按 Esc 鍵。To return to the default workspace, press the Esc key.
  8. 若要顯示紙帶,請插入資料表控制項,並將其 Items 屬性設為以下公式:To display the paper tape, insert a Data table control, and set its Items property to this formula:

    PaperTapePaperTape

    您也必須選取要顯示在右側窗格的資料行,在案例中請顯示 Value 資料行:You will also need to select the columns to show in the right hand pane, in our case show the Value column:

  9. 若要查看您集合中的值,請選取 [檔案] 功能表上的 [集合]。To see the values in your collection, select Collections on the File menu.

  10. 若要儲存和擷取集合,請新增兩個額外的按鈕控制項,並將其文字設為 LoadSaveTo store and retrieve your collection, add two additional button controls and set their text to Load and Save. 針對 Load,將 OnSelect 屬性設為︰For Load, set the OnSelect property to:

    Clear( PaperTape ); LoadData( PaperTape, "StoredPaperTape", true )Clear( PaperTape ); LoadData( PaperTape, "StoredPaperTape", true )

    我們需先清除集合,因 LoadData 會將儲存值附加至集合結尾。We need to clear the collection first as LoadData will append the stored values to the end of the collection.

  11. 針對 Save,將 OnSelect 屬性設為︰For Save, set the OnSelect property to:

    SaveData( PaperTape, "StoredPaperTape" )SaveData( PaperTape, "StoredPaperTape" )

  12. 按 F5 鍵再次預覽,在文字輸入控制項中輸入數字,並選取按鈕。Preview again by pressing the F5 key, enter numbers in the text-input control, and select buttons. 選取 [儲存] 按鈕。Select the Save button. 關閉並重新載入應用程式,然後選取 [載入] 按鈕以重新載入集合。Close and reload your app, and select the Load button to reload your collection.

    注意

    SaveDataLoadData 在網頁瀏覽器中執行時沒有效用。SaveData and LoadData do not function when running in a web browser. 您必須使用安裝在 Windows 上的 studio 或行動裝置的其中一個播放程式。You must be using the studio installed on Windows or one of the players for mobile devices.