0
votes

Now I'm trying to use Google URL Shortener in Google Spreadsheet(app script).

However, when I run the script, it shows an error: "User Rate Limit Exceeded. Please sign up (line 16, file "Code")"

I went to Google Developers Console and changed Quota limit as 1000 requests/second/user, but it seems like the script didn't even send the request because the traffic reports for this API is 0.

The following is my script. I wanna get the analytics clicks for shortened URL in the spreadsheet.

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 {

}
}
}
1
I've had the same error message when trying to use Drive advanced app in an old script file today. Copying/pasting the code in a new file solved the issue, it seems the authorization was wrong. Did you try this code in a new spreadsheet? (didn't test at all...)Serge insas
Thanks for your reply. I tried to create a new spreadsheet and copy/paste the code, but the issue still exists. I think you are right. There's something wrong with the authorization. Do you have any idea to solve it?jimrockey
I've been spending the last 2 hours on this (;-) and since I couldn't get it working I switched on the UrlFetch method... I know it's not the subject of your post but you know... I'm curious ;-) btw, your code is correct (imho) and I #@!?## wonder how to identify in this context... :-DSerge insas
finally, I decided to show the result I get from my experiments... interesting I think, although not exactly what you were asking. sorry about that.Serge insas

1 Answers

1
votes

Note : this does not provide an exact answer but instead suggests a different way to get the desired result.

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);
}

Example result in spreadsheet (real screen is much wider as there are many results to show):

url = http://goo.gl/fbsS

enter image description here