Google Script Google URL API Invalid Value (line 5, file "Code")

时间:2016-08-31 17:41:16

标签: google-apps-script google-sheets google-url-shortener

I'm running the below script that will read from google sheets but is failing with the error"

Invalid Value (line 5, file "Code")

function GetShortUrlClicks() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  for (var i = 0; i < data.length; i++){
    var analytics = UrlShortener.Url.get(data ,{projection:'FULL'}).getAnalytics();
    var clicks = analytics.getAllTime();
    Logger.log(clicks)
  }
}

However, if I run the same script with a URL in place of data the script runs and I get the proper data back working the Google URL API that equals to the amount of cells I have in the Google Sheet.

function GetShortUrlClicks() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  for (var i = 0; i < data.length; i++){
    var analytics = UrlShortener.Url.get('google.com',{projection:'FULL'}).getAnalytics();
    var clicks = analytics.getAllTime();
    Logger.log(clicks)
  }
}

I'm assuming the data variable is not getting passed properly.

1 个答案:

答案 0 :(得分:0)

使用以下更新。

 var analytics = UrlShortener.Url.get(data[i],{projection:'FULL'}).getAnalytics();