现在我尝试在Google电子表格(应用程序脚本)中使用Google URL Shortener。
但是,当我运行脚本时,它会显示错误:"超出用户速率限制。请注册(第16行,文件"代码")"
我访问了Google Developers Console并将配额限制更改为1000个请求/秒/用户,但似乎该脚本甚至没有发送请求,因为此API的流量报告为0。
以下是我的脚本。我想在电子表格中获得缩短网址的分析点击次数。
function onOpen() {
total_clicks()
}
function total_clicks() {
var sheet = SpreadsheetApp.getActiveSheet();
var end = SpreadsheetApp.getActiveSheet().getLastRow();
for (var i = 2; i <= end; ++i)
{
var short_url = sheet.getRange(i, 2).getValue();
if (short_url !== "") {
var click_number = UrlShortener.Url.get(short_url, {
projection: 'ANALYTICS_CLICKS'
});
var clicks_month = sheet.getRange(i, 5);
clicks_month.setValue(click_number.analytics.day.shortUrlClicks);
var clicks_week = sheet.getRange(i, 6);
clicks_week.setValue(click_number.analytics.week.shortUrlClicks);
var clicks_month = sheet.getRange(i, 7);
clicks_month.setValue(click_number.analytics.month.shortUrlClicks);
var clicks_all = sheet.getRange(i, 8);
clicks_all.setValue(click_number.analytics.allTime.shortUrlClicks);
}
else {
}
}
}
答案 0 :(得分:1)
var key = 'AIzaSy___________X2e86zY5Ko';// get your own API key from the console
function total_clicks() {
var sheet = SpreadsheetApp.getActiveSheet();
var end = SpreadsheetApp.getActiveSheet().getLastRow();
for (var i = 2; i <= end; ++i){
var shortUrl = sheet.getRange(i, 1).getValue();
if (shortUrl == "") {continue};
var row = [];var header = [];
var response = getClicks(shortUrl);
//Logger.log(response);
for(var prop in response){
var element = response[prop];
for(var detail in element){
var el = element[detail];
for(var d in el){
if(typeof(el[d])=='object'){el[d]=toSt(el[d])};
row.push(el[d]);
header.push(d)
}
}
}
// Logger.log(row);
sheet.getRange(i,2,1,row.length).setValues([row])
}
sheet.getRange(1,2,1,header.length).setValues([header])
}
function toSt(array){
var ret = '';
for(var idx in array){
var val = array[idx]
var str = '';
for(var p in val){
str+= val[p]+' : ';
}
ret+=str+'\n';
}
return ret;
}
function getClicks(shortUrl) {
var response = UrlFetchApp.fetch('https://www.googleapis.com/urlshortener/v1/url?shortUrl='+shortUrl+'&projection=FULL&key='+key).getContentText()
return JSON.parse(response);
}
电子表格中的示例结果(实际屏幕要宽得多,因为要显示许多结果):
url = http://goo.gl/fbsS