公式在 Google 应用程序脚本控制台中有效,但在电子表格中出现 Returns 身份验证错误
Formula Works in Google App Script Console, But Returns Authentication Error in Spreadsheets
我为我的一个电子表格创建了一个自定义公式。它获取两个日期参数和 returns 特定 Google 分析事件的唯一事件总数。当我测试我的代码时,它在 Google Apps 脚本中完美运行。但是当我尝试在关联的电子表格中使用相同的公式时,会导致以下错误:
GoogleJsonResponseException: API call to analytics.data.ga.get failed with error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (line 13).
这是我的代码:
var tableId = 'ga:78467590';
function test(){
USE_CLICK_COUNT("2020-08-31", "2020-09-06");
}
function USE_CLICK_COUNT(startDate, endDate){
var optArgs = {
'dimensions':'ga:eventLabel',
'filters':'ga:eventLabel=~^UseClick'
};
var results = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:uniqueEvents', optArgs);
Logger.log(results.getTotalsForAllResults());
return results.getTotalsForAllResults();
}
当我 运行 test()
函数时,它 returns 并记录 {ga:uniqueEvents=13}
- 这就是我要查找的数字。
我一遍又一遍地检查 API 和配置细节,但无法弄清楚我遗漏了什么。请看附件
问题和解决方法:
我以为你的情况和一样。遗憾的是,在现阶段,Google高级Google服务的API不能直接与自定义功能一起使用。因为无法在自定义函数中检索访问令牌。这似乎是当前的规范。因此,作为实现目标的解决方法,它使用由 Google Apps Script 创建的 Web Apps。
但我认为您问题的脚本与 有点不同。因此,当提出示例脚本时,它可能对您有用。因此,我想介绍实现您的目标的示例脚本。
1。准备脚本。
请将以下脚本复制并粘贴到脚本编辑器中并保存。
示例脚本:
// This is your script.
var tableId = 'ga:78467590';
function test(){
USE_CLICK_COUNT("2020-08-31", "2020-09-06");
}
function USE_CLICK_COUNT(startDate, endDate){
var optArgs = {
'dimensions':'ga:eventLabel',
'filters':'ga:eventLabel=~^UseClick'
};
var results = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:uniqueEvents', optArgs);
Logger.log(results.getTotalsForAllResults());
return results.getTotalsForAllResults();
}
// I added below script.
const key = "samplekey"; // This is used as the key for running the Web Apps.
// Web Apps using as the wrapper.
function doGet(e) {
if (e.parameter.key === key) {
const res = USE_CLICK_COUNT(e.parameter.startDate, e.parameter.endDate);
return ContentService.createTextOutput(JSON.stringify(res));
}
return ContentService.createTextOutput("Error.");
}
// This is used as the custom function.
function RUN_USE_CLICK_COUNT(startDate, endDate) {
const url = `https://script.google.com/macros/s/###/exec?startDate=${startDate}&endDate=${endDate}&key=${key}`; // Please set the URL of Web Apps after you set the Web Apps.
return UrlFetchApp.fetch(url).getContentText();
}
2。部署 Web 应用程序。
在脚本编辑器上,通过“发布”->“部署为网络应用程序”打开一个对话框。
Select "Me" for "将应用程序执行为:".
- 至此,脚本运行作为所有者。
Select “任何人,甚至是匿名的” for “有权访问应用程序的人:”.
- 在这种情况下,请求不需要访问令牌。我认为我建议使用此设置来测试此解决方法。
- 当然你也可以使用access token。但是,在这种情况下,当使用访问令牌时,此示例脚本不能直接用作自定义函数。所以在这个答案中,我建议使用 运行 网络应用程序脚本的密钥。但是如果你想使用access token,我觉得可以使用PropertiesService来实现。
单击“部署”按钮作为新的“项目版本”。
自动打开“需要授权”的对话框。
- 单击“查看权限”。
- Select自己的账号。
- 点击“此应用未验证”处的“高级”。
- 点击“转到###项目名称###(不安全)”
- 单击“允许”按钮。
点击“确定”。
复制 Web Apps 的 URL。就像 https://script.google.com/macros/s/###/exec
.
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
请将https://script.google.com/macros/s/###/exec
的URL设置为上述脚本的url
。并请重新部署 Web 应用程序。由此,最新的脚本被反映到Web Apps。所以请注意这一点。
4。测试此解决方法。
请将 =RUN_USE_CLICK_COUNT("2020-08-31", "2020-09-06")
放入单元格。通过这种方式,"2020-08-31", "2020-09-06"
的值被发送到 Web 应用程序并返回 USE_CLICK_COUNT
的值。
注:
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
参考文献:
我为我的一个电子表格创建了一个自定义公式。它获取两个日期参数和 returns 特定 Google 分析事件的唯一事件总数。当我测试我的代码时,它在 Google Apps 脚本中完美运行。但是当我尝试在关联的电子表格中使用相同的公式时,会导致以下错误:
GoogleJsonResponseException: API call to analytics.data.ga.get failed with error: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. (line 13).
这是我的代码:
var tableId = 'ga:78467590';
function test(){
USE_CLICK_COUNT("2020-08-31", "2020-09-06");
}
function USE_CLICK_COUNT(startDate, endDate){
var optArgs = {
'dimensions':'ga:eventLabel',
'filters':'ga:eventLabel=~^UseClick'
};
var results = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:uniqueEvents', optArgs);
Logger.log(results.getTotalsForAllResults());
return results.getTotalsForAllResults();
}
当我 运行 test()
函数时,它 returns 并记录 {ga:uniqueEvents=13}
- 这就是我要查找的数字。
我一遍又一遍地检查 API 和配置细节,但无法弄清楚我遗漏了什么。请看附件
问题和解决方法:
我以为你的情况和
但我认为您问题的脚本与
1。准备脚本。
请将以下脚本复制并粘贴到脚本编辑器中并保存。
示例脚本:// This is your script.
var tableId = 'ga:78467590';
function test(){
USE_CLICK_COUNT("2020-08-31", "2020-09-06");
}
function USE_CLICK_COUNT(startDate, endDate){
var optArgs = {
'dimensions':'ga:eventLabel',
'filters':'ga:eventLabel=~^UseClick'
};
var results = Analytics.Data.Ga.get(tableId, startDate, endDate, 'ga:uniqueEvents', optArgs);
Logger.log(results.getTotalsForAllResults());
return results.getTotalsForAllResults();
}
// I added below script.
const key = "samplekey"; // This is used as the key for running the Web Apps.
// Web Apps using as the wrapper.
function doGet(e) {
if (e.parameter.key === key) {
const res = USE_CLICK_COUNT(e.parameter.startDate, e.parameter.endDate);
return ContentService.createTextOutput(JSON.stringify(res));
}
return ContentService.createTextOutput("Error.");
}
// This is used as the custom function.
function RUN_USE_CLICK_COUNT(startDate, endDate) {
const url = `https://script.google.com/macros/s/###/exec?startDate=${startDate}&endDate=${endDate}&key=${key}`; // Please set the URL of Web Apps after you set the Web Apps.
return UrlFetchApp.fetch(url).getContentText();
}
2。部署 Web 应用程序。
在脚本编辑器上,通过“发布”->“部署为网络应用程序”打开一个对话框。
Select "Me" for "将应用程序执行为:".
- 至此,脚本运行作为所有者。
Select “任何人,甚至是匿名的” for “有权访问应用程序的人:”.
- 在这种情况下,请求不需要访问令牌。我认为我建议使用此设置来测试此解决方法。
- 当然你也可以使用access token。但是,在这种情况下,当使用访问令牌时,此示例脚本不能直接用作自定义函数。所以在这个答案中,我建议使用 运行 网络应用程序脚本的密钥。但是如果你想使用access token,我觉得可以使用PropertiesService来实现。
单击“部署”按钮作为新的“项目版本”。
自动打开“需要授权”的对话框。
- 单击“查看权限”。
- Select自己的账号。
- 点击“此应用未验证”处的“高级”。
- 点击“转到###项目名称###(不安全)”
- 单击“允许”按钮。
点击“确定”。
复制 Web Apps 的 URL。就像
https://script.google.com/macros/s/###/exec
.- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
请将
https://script.google.com/macros/s/###/exec
的URL设置为上述脚本的url
。并请重新部署 Web 应用程序。由此,最新的脚本被反映到Web Apps。所以请注意这一点。
4。测试此解决方法。
请将 =RUN_USE_CLICK_COUNT("2020-08-31", "2020-09-06")
放入单元格。通过这种方式,"2020-08-31", "2020-09-06"
的值被发送到 Web 应用程序并返回 USE_CLICK_COUNT
的值。
注:
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。