简体   繁体   English

如何使用用户可以在 getFields 的计算字段中更新的参数(Data Studio 社区连接器)

[英]How to use a parameter which a user can update in a calculated field in getFields (Data Studio Community Connector)

I have a single select parameter that I define in a data studio community connector in getConfig() as below.我有一个 select 参数,我在 getConfig() 的数据工作室社区连接器中定义如下。 The parameter is then used as a dropdown data control in the report.然后将该参数用作报表中的下拉数据控件。

config
    .newSelectSingle()
    .setId("characteristic_selected")
    .setName("Characteristic selected")
    .addOption(
        config
          .newOptionBuilder()
          .setLabel("a")
          .setValue("a")
    )
    .addOption(
      config
        .newOptionBuilder()
        .setLabel("b")
        .setValue("b")
    )
    .addOption(
      config
        .newOptionBuilder()
        .setLabel("c")
        .setValue("c")
    )
    .addOption(
      config
        .newOptionBuilder()
        .setLabel("d")
        .setValue("d")
    )
    .addOption(
      config
        .newOptionBuilder()
        .setLabel("e")
        .setValue("e")
    )
    .setAllowOverride(true);

In getFields() I define Characteristic which then returns data from the database a, b, c, d or e:在 getFields() 中,我定义了 Characteristic,然后从数据库 a、b、c、d 或 e 返回数据:

  fields.newDimension()
    .setId('Characteristic')
    .setType(types.TEXT);

I am trying to define a calculated field which I will then use as a filter in my charts and tables to only display data for the option selected by the user in the dropdown data control.我正在尝试定义一个计算字段,然后将其用作图表和表格中的过滤器,以仅显示用户在下拉数据控件中选择的选项的数据。 Eg if the user selects "b", then only data labelled "b" for Characteristic will display.例如,如果用户选择“b”,那么只有标记为“b”的数据才会显示。

My attempt (inspired by the answer here: How to use a Parameter in calculated field defined in getFields() (Google Data Studio Community Connector)? ) is:我的尝试(受到此处答案的启发: 如何在 getFields()(Google Data Studio 社区连接器)中定义的计算字段中使用参数? )是:

  fields.newDimension()
    .setId('Characteristic calc')
    .setDescription('Sets true if characteristic selected in dropdown is the same as the characteristic dimension field')
    .setFormula('$Characteristic = "' + request.configParams.characteristic_selected + '"')
    .setType(types.BOOLEAN);

I then apply a filter to the tables and charts only include Characteristic calc = True然后我对表格和图表应用过滤器,仅包含 Characteristic calc = True

The default is "a".默认值为“a”。 When I first load the page, the data filters correctly and only displays "a".当我第一次加载页面时,数据过滤正确,只显示“a”。 However when I select "b" from the dropdown data control, it still only displays data for "a".但是,当我从下拉数据控件中选择 select "b" 时,它仍然只显示 "a" 的数据。 It appears that the code does not capture the updated configParam when changed in the report.在报告中更改时,代码似乎没有捕获更新的 configParam。

Note: if I set the calculated field up in the report as opposed to in the data studio connector, then it works correctly.注意:如果我在报告中而不是在数据工作室连接器中设置计算字段,那么它可以正常工作。 I use the connector in lots of reports however, so it is annoying to have to create the calculated field each time I create a new report.但是,我在很多报告中都使用了连接器,因此每次创建新报告时都必须创建计算字段,这很烦人。

Sorry, using the .setFormula will only work, if the parameter is only set during the source configuration.抱歉,只有在源配置期间才设置参数时,使用.setFormula才有效。 If the user can change it in the report the field calculation has to be done in the function getSchema(request) .如果用户可以在报告中更改它,则必须在function getSchema(request)中完成字段计算。

function getSchema(request) {
  var fields = cc.getFields();
  var types = cc.FieldType;
 fields.newDimension()
    .setId('Characteristic_info_info')
    .setDescription('tempory field')
    .setType(types.TEXT);
 fields.newDimension()
    .setId('Characteristic calc')
    .setDescription('Sets true if characteristic selected in dropdown is the same as the characteristic dimension field')
    .setFormula('$Characteristic = $Characteristic_info_info')
    .setType(types.BOOLEAN);

Then set the column to the right value in this function:然后在这个 function 中将列设置为正确的值:

function getData(request) {
....
  var schemaE = getFields(request).forIds(
    request.fields.map(function(field) {
      return field.name;
    })
  ).build();

  var out1=[];
  schemaE.forEach(a=>out1.push(a.name in params ? params[a.name] : 'nope:'+a.name ));

  var out= {
    schema: schemaE,
    rows: [{values:out1},{values:out1},{values:out1}] 
  };
console.log(schemaE)

console.log(request.configParams.characteristic_selected)

for(let col in schemaE){
if(schemaE[col].name=="Characteristic_info_info"){
for(let i in out.rows)
 {
   out.rows[i].values[col]=request.configParams.characteristic_selected+"!";
 }
}
}
  return out;
}

If a paramter could be used in .setFormula and not only $ for fields, life would be much easier.如果可以在.setFormula中使用参数而不仅仅是$用于字段,那么生活会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Data Studio Community Connector:如何更改数据新鲜度间隔 - Data Studio Community Connector: How to change Data freshness interval 如何在 Google Data Studio 中创建计算字段过滤器? - How to create Calculated Field Filters in Google Data Studio? 将 Google Data Studio 社区连接器与 BigQuery 结合使用时的时间戳查询问题 - Timestamp query issue when using Google Data Studio community connector with BigQuery 如何在 firebase v9 的 firesrore 中为每个用户只更新一次特定字段的数据? - How to update a specific field of data only one time for each user in firesrore in firebase v9? 使用 UPDATE、SUM 和 EXTRACT 函数从计算字段更新列 - Updating a column from a calculated field using UPDATE, SUM & EXTRACT functions Data Studio 自定义连接器字段数量错误 - Data Studio Custom Connector wrong amount of fields 使用计算字段订购 QuerySnapShot - Ordering QuerySnapShot with calculated field 如何使用计算字段将字符串转换为 AWS Quicksight 中的日期时间? - How to convert string to Datetime in AWS Quicksight using calculated field? FireBase - 使用用户身份验证时更新字段 - FireBase - update field when using user authentication Google Data Studio - 用户定义函数 (UDF) 的使用 - Google Data Studio - Use of User Defined Functions (UDFs)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM