Patch函数基于从下拉菜单中选择的选项来编辑列。

huangapple go评论56阅读模式
英文:

Power APP, patch column based on choice made from dropdown

问题

我正在使用条件语句来实现这个工作,但我想知道是否可以使用一个变量。以下是我的代码:

ForAll(
  selected_people,
  Patch(
    SampleExcelData,
    LookUp(SampleExcelData, ID = selected_people[@ID]),
    { *dropdown.selected(我的列名)或变量*: "Yes" }
  )
)

因为有时我需要记录15门课程,而使用15个条件语句太多了。

英文:

I'm making this work by using ifs, but I wonder if I can use a variable, here is what I have:

ForAll(
  selected_people,
  If(
    Dropdown1.SelectedText.Value = "Trainning 1",
    Patch(SampleExcelData,LookUp(SampleExcelData,ID = selected_people[@ID]),{Trainning1: "Yes"}),
    If(
     Dropdown1.SelectedText.Value = "Trainning 2",
     Patch(SampleExcelData,LookUp (SampleExcelData,ID = selected_people[@ID]),{Trainning2: "Yes"}),
     If(
       Dropdown1.SelectedText.Value = "Trainning 3",
       Patch(SampleExcelData, LookUp( SampleExcelData,ID = selected_people[@ID]),{Trainning3: "Yes"}),
       ....

I get this result:

ID Person Last Name Trainning 1 Trainning 2 Trainning 3
001 joe xxx yes yes
002 jill rrr yes yes
002 lilly bbb yes yes

but I wonder if I could save the dropdown selected in a variable and do something like this:

ForAll(
  selected_people,
  Patch(
    SampleExcelData,
    LookUp(SampleExcelData,ID = selected_people[@ID]),
    {*dropdown.selected(My column name) or variable*: "Yes"}))

Thanks, because I sometimes have 15 courses being recorded and 15 ifs is a lot.

答案1

得分: 1

不能在表达式中使用变量(或对控件属性值的引用)作为列名 - Power Fx 表达式需要具有强类型。您可以通过使用 Switch 语句而不是多次 If 调用来简化您的表达式:

ForAll(
  selected_people,
  Switch(
    Dropdown1.SelectedText.Value,
    "Trainning 1",
      Patch(SampleExcelData, LookUp(SampleExcelData, ID = selected_people[@ID]), { Trainning1: "Yes" }),
    "Trainning 2",
      Patch(SampleExcelData, LookUp(SampleExcelData, ID = selected_people[@ID]), { Trainning2: "Yes" }),
    "Trainning 3",
      Patch(SampleExcelData, LookUp(SampleExcelData, ID = selected_people[@ID]), { Trainning3: "Yes" }),
       ....
  )
)

这种方法也可能有效,但我没有尝试过:

ForAll(
  selected_people,
  With(
    { recordToUpdate: LookUp(SampleExcelData, ID = selected_people[@ID]) },
    Switch(
      Dropdown1.SelectedText.Value,
      "Trainning 1",
        Patch(SampleExcelData, recordToUpdate, { Trainning1: "Yes" }),
      "Trainning 2",
        Patch(SampleExcelData, recordToUpdate, { Trainning2: "Yes" }),
      "Trainning 3",
        Patch(SampleExcelData, recordToUpdate, { Trainning3: "Yes" }),
      ...
    )
  )
)

希望这可以帮助您。

英文:

You cannot use a variable (or a reference to a control property value) as the column name in an expression - Power Fx expressions need to be strongly-typed. You can make your expression a little simpler by using a Switch statement instead of multiple If calls:

ForAll(
  selected_people,
  Switch(
    Dropdown1.SelectedText.Value,
    "Trainning 1",
      Patch(SampleExcelData,LookUp(SampleExcelData,ID = selected_people[@ID]),{Trainning1: "Yes"}),
    "Trainning 2",
      Patch(SampleExcelData,LookUp(SampleExcelData,ID = selected_people[@ID]),{Trainning2: "Yes"}),
    "Trainning 3",
      Patch(SampleExcelData,LookUp(SampleExcelData,ID = selected_people[@ID]),{Trainning3: "Yes"}),
       ....

This may work as well, but I haven't tried it:

ForAll(
  selected_people,
  With(
    { recordToUpdate: LookUp(SampleExcelData,ID = selected_people[@ID]) },
    Switch(
      Dropdown1.SelectedText.Value,
      "Trainning 1",
        Patch(SampleExcelData, recordToUpdate, { Trainning1: "Yes" }),
      "Trainning 2",
        Patch(SampleExcelData, recordToUpdate, { Trainning2: "Yes" }),
      "Trainning 3",
        Patch(SampleExcelData, recordToUpdate, { Trainning3: "Yes" }),
      ...
    )
  )
)

huangapple
  • 本文由 发表于 2023年6月9日 01:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定