Analytics API(Apps 脚本)文档丢失? | "getItems" vs "items" 和其他问题

Analytics API (Apps Script) Documentation Missing ? | "getItems" vs "items" and other issues

大家好(提前感谢您的帮助)!我很想知道分析 API(管理 API):https://developers.google.com/analytics/devguides/config/mgmt/v3/

Google 表格/Apps 脚本/分析

我使用 Google 表格工作,所以我使用 Apps 脚本提取数据。 Apps 脚本本身的文档就足够自我解释了,但对于与 Apps 脚本结合使用的 Analytics Management API 似乎没有任何内容。到目前为止,我一直在查看 Javascript 样本以获得接近的结果。

隐藏功能?

然而,我在各种示例中发现了一些可用的函数,但文档中没有这些函数。例如:

var properties = Analytics.Management.Webproperties.list(AcountID);
var webPropertyIdOfFirstElement = properties.getItems()[0].getId();

请问return第一个元素的ID。

文档

但是根据此文档 (Javascript)

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/webproperties/list

实际代码(return相同)should/would为:

var results = Analytics.Management.Webproperties.list(accountID);
var properties = results.items;
var property = properties[0];
var theID = property.id;

在哪里?

所以我的问题是函数在哪里

getItems()[X] and .getId()

来自于那些不是 javascript 内置函数?是否有某种秘密文件是我一直遗漏的?老实说,我很想知道。

还有(在上面示例中的同一页上)。有人可以解释一下 "execute" 函数吗?老实说,我以前从未见过这个。它是如何工作的?

function listProperties() {
  var request = gapi.client.analytics.management.webproperties.list({
    'accountId': '123456'
  });
  request.execute(printProperties);
}

function printProperties(results) { //do stuff }

? "execute" 并将函数作为参数传递?嗯?

这个答案怎么样?

问题 1 的答案:

  • 您想了解 var res = Analytics.Management.Webproperties.list(AcountID)res.items[0].idres.getItems()[0].getId() 的区别。
下面脚本的

value1value2是一样的。但是 itemsgetItems 分别是键和函数。

var res = Analytics.Management.Webproperties.list(AcountID);
var value1 = res.items[0].id;
var value2 = res.getItems()[0].getId();

似乎每个值都可以通过在顶部添加 get 并将其作为函数添加 运行 来检索。函数名必须是驼峰式

但是我在官方文档和脚本编辑器的补全功能上都没有找到这样的功能。所以我一直以为这些功能可能是隐藏功能。

在Advanced Google Services中,我们可以通过脚本编辑器的完成功能来确认为API创建请求体的功能。 但是虽然找不到取值函数,但是可以作为隐藏方法使用。这样的函数也可以确认其他API.

也有这样的隐藏方法

其他情况:

例如,和其他情况一样,在Sheets API,您可以在以下脚本中看到隐藏的功能。

var r = Sheets.Spreadsheets.Values.batchGet(spreadsheetId, {ranges: ["Sheet1", "Sheet2"]});

var value1a = r.spreadsheetId;
var value1b = r.getSpreadsheetId();

var value2a = r.valueRanges;
var value2b = r.getValueRanges();

在这种情况下,value1avalue1b是一样的。 value2avalue2b 也是一样的。为了确认getSpreadsheetId()getValueRanges()是否可以使用,我使用下面的脚本

var r = Sheets.Spreadsheets.Values.batchGet(spreadsheetId, {ranges: ["Sheet1", "Sheet2"]});
Logger.log(typeof r.getSpreadsheetId)
Logger.log(typeof r.getValueRanges)

如果可以作为函数使用,日志显示function。如果不能作为函数使用,则返回undefined

问题 2 的答案:

  • 您想了解 google-api-javascript-客户端的 execute()

我想答案可以在Using Promises看到。

Migrating from callbacks to promises

The result parameter of the fulfilled promise value is equivalent to the first parameter in execute's callback. To update your code to use promises, change your code as shown in the before and after examples below.

The following example shows using a callback:

gapi.client.request({
  'path': 'plus/v1/people',
  'params': {'query': name}
 }).execute(function(resp, rawResp) {
   processResponse(resp);
 });

You can rewrite the example shown above to use a promise like the following:

gapi.client.request({
  'path': 'plus/v1/people',
  'params': {'query': name}
 }).then(function(resp) {
   processResponse(resp.result);
 });

参考文献:

如果我误解了您的问题并且这不是您想要的答案,我深表歉意。