如何在 Apps 脚本 (javascript) 的 json 格式 API 调用中包含变量?

How can I include a variable in a json-format API call in Apps Script (javascript)?

我正在为 Google 表格开发一个自定义函数,它接收一个单元格值作为输入,并从 API 调用中输出响应。知道在当前阶段 Google Analytics Reporting API 无法直接与自定义函数一起使用,我正在遵循 and 解决方法。

运行 API 调用 Google 分析报告 API 的函数包括一个 JSON 格式的请求,其中包含指标、维度、过滤器等。问题是维度过滤器是可变的,我希望它是来自 Google Sheets 的输入。但是,我不断收到此错误消息:

GoogleJsonResponseException:API 调用 analyticsreporting.reports.batchGet 失败,出现错误:需要字段 request.dimensionFilterClauses.filters.expressions。 (第 2 行,文件“代码”)

    function plateToCampaign(plate) {
  globalThis.r = AnalyticsReporting.Reports.batchGet({
  "reportRequests":
  [
    {
      "viewId": {"my_viewId"},
      "pageSize": 1,
      "dateRanges": [
        {"endDate": "today", "startDate": "30daysAgo"}
      ],
      "metrics": [
        {"expression": "ga:pageValue"}
      ],
      "dimensions": [{"name": "ga:sourceMedium"}, {"name": "ga:campaign"}, {"name": "ga:dateHourMinute"}],
      "orderBys": {"fieldName": "ga:dateHourMinute","sortOrder": "DESCENDING"},
      "dimensionFilterClauses": [
        {
          "filters": [
            {
              "dimensionName": "ga:pagePath",
              "operator": "PARTIAL",
              "expressions": [plate]
            }
          ]
        }
      ]
    }
  ]
});
globalThis.c = r.reports[0].data.rowCount > 0? r.reports[0].data.rows[0].dimensions[0] : null
if (c === "google / cpc") {
  globalThis.x = r.reports[0].data.rows[0].dimensions[1]
  console.log(x)
  return(x)
} else {
  console.log(c);
  return(c)
}
}


function doGet(e) {
  const res = plateToCampaign(e.parameter.plate)
  return ContentService.createTextOutput(JSON.stringify(res));
}

function test(plate) {
  const url = {"my_web_app_url"}
  return UrlFetchApp.fetch(url).getContentText();  
}

如果我将变量 plate 更改为单个值,它就会起作用,即函数 plateToCampaign(plate = {"fixed_value"})。但是这个值每次都会不同。我该如何解决这个问题?

按如下方式更正您的 json 后

  var reportRequests = 
  [
    {
      "viewId": "my_viewId",
      "pageSize": 1,
      "dateRanges": [
        {"endDate": "today", "startDate": "30daysAgo"}
      ],
      "metrics": [
        {"expression": "ga:pageValue"}
      ],
      "dimensions": [{"name": "ga:sourceMedium"}, {"name": "ga:campaign"}, {"name": "ga:dateHourMinute"}],
      "orderBys": {"fieldName": "ga:dateHourMinute","sortOrder": "DESCENDING"},
      "dimensionFilterClauses": [
        {
          "filters": [
            {
              "dimensionName": "ga:pagePath",
              "operator": "PARTIAL",
              "expressions": ["plate"]
            }
          ]
        }
      ]
    }
  ]

你可以这样换盘子

function myfunction(item) {
  var reportRequests =
    [
      {
        "viewId": "my_viewId",
        "pageSize": 1,
        "dateRanges": [
          { "endDate": "today", "startDate": "30daysAgo" }
        ],
        "metrics": [
          { "expression": "ga:pageValue" }
        ],
        "dimensions": [{ "name": "ga:sourceMedium" }, { "name": "ga:campaign" }, { "name": "ga:dateHourMinute" }],
        "orderBys": { "fieldName": "ga:dateHourMinute", "sortOrder": "DESCENDING" },
        "dimensionFilterClauses": [
          {
            "filters": [
              {
                "dimensionName": "ga:pagePath",
                "operator": "PARTIAL",
                "expressions": [""]
              }
            ]
          }
        ]
      }
    ]
  reportRequests[0].dimensionFilterClauses[0].filters[0].expressions[0] = item
  // include reportRequests in your own script ...
}

申请

试试这个

function plateToCampaign(item = 'plate') {
  var myRequest =
    [
      {
        "viewId": "my_viewId",
        "pageSize": 1,
        "dateRanges": [
          { "endDate": "today", "startDate": "30daysAgo" }
        ],
        "metrics": [
          { "expression": "ga:pageValue" }
        ],
        "dimensions": [{ "name": "ga:sourceMedium" }, { "name": "ga:campaign" }, { "name": "ga:dateHourMinute" }],
        "orderBys": { "fieldName": "ga:dateHourMinute", "sortOrder": "DESCENDING" },
        "dimensionFilterClauses": [
          {
            "filters": [
              {
                "dimensionName": "ga:pagePath",
                "operator": "PARTIAL",
                "expressions": [""]
              }
            ]
          }
        ]
      }
    ]
  myRequest[0].dimensionFilterClauses[0].filters[0].expressions[0] = item
  globalThis.r = AnalyticsReporting.Reports.batchGet({
    "reportRequests": myRequest
  });
  globalThis.c = r.reports[0].data.rowCount > 0 ? r.reports[0].data.rows[0].dimensions[0] : null
  if (c === "google / cpc") {
    globalThis.x = r.reports[0].data.rows[0].dimensions[1]
    console.log(x)
    return (x)
  } else {
    console.log(c);
    return (c)
  }
}