获取typeof错误未定义和0值谷歌电子表格

时间:2017-05-24 04:37:43

标签: javascript google-sheets

在尝试获取数据时,我很难尝试捕获或阻止未定义的错误。此错误会阻止脚本运行。该脚本旨在从电子表格中获取A2:A中的所有AppID。从那里,它填写了一些关于游戏的基本信息;姓名,免费与否,并有卡。

其中一个错误:TypeError: Cannot read property "0" from undefined. (line 40, file "Code")

第40行:var gameId = values[i][0];

Spreadsheet

以下是我能够整理并在一定程度上工作的代码

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  options = [
    {name:"Update Headers", functionName:"createHeaders"},
    {name:"Update Games", functionName:"steamData"}
    ];
  ss.addMenu("Steam Info", options);
}


function createHeaders() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  // Freezes the first row
  sheet.setFrozenRows(1);

  //Set header values
  var headerNames = [
    ["APPID","GAME NAME","HAS CARDS","IS FREE?","KEY","DLC KEYS","SG LINK","WINNER","SG STATUS"]
   ];

  //Set range
  var range = sheet.getRange("A1:I1");

  //Set range with values
  range.setValues(headerNames);
}

function steamData() 
{
  var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues();
  //values[i][y] y is index of the column starting from 0
  //values[y][i] y is index of the row starting from 0
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  for( var i = 0; sheet.getLastRow() > 0; i--) 
  {
    if ( i === 0 ){ continue; }
    var gameId = values[i][0];
    var url = 'http://store.steampowered.com/api/appdetails?appids='+gameId;
    var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
    var json = response.getContentText();
    var data = JSON.parse(json);
    var gameData = data[gameId].data;
    var hasCards = "No";
    for(var j = 0; j < gameData.categories.length; j++) {
      if(gameData.categories[j].id === 29) {
        hasCards = "Yes";
       }
     }
    var cellData = [
      [gameId.toString(),gameData.name,hasCards,gameData.is_free]
    ];
    if ( i === 1 ){ continue; }
    Logger.log("A"+i);
    var selection = sheet.getRange("A"+ i +":D"+ i);
    selection.setValues(cellData);
  }
}

更新了现在可以使用的代码

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  options = [
    {name:"Update Headers", functionName:"createHeaders"},
    {name:"Update Games", functionName:"steamData"}
    ];
  ss.addMenu("Steam Info", options);

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

  // Freezes the first row
  sheet.setFrozenRows(1);

  //Set header values
  var headerNames = [
    ["APPID","GAME NAME","HAS CARDS","IS FREE?","KEY","DLC KEYS","SG LINK","WINNER","SG STATUS"]
   ];

  //Set range
  var range = sheet.getRange("A1:I1");

  //Set range with values
  range.setValues(headerNames);
}

function steamData() 
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var range = sheet.getRange("A2:A");
  var cells = range.getValues();
  var last = sheet.getLastRow().toString();
  for(var i = 0; i < last-1; i++) 
  {
    if(cells[i] == 'APPID') {
      continue;
    }
//    Logger.log("B"+(i+2)+":D"+(i+2));
    var gameId = cells[i].toString();
    var url = 'http://store.steampowered.com/api/appdetails?appids='+gameId;
    var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
    var json = response.getContentText();
    var data = JSON.parse(json);
    var gameData = data[gameId].data;
    var hasCards = "No";
    for(var j = 0; j < gameData.categories.length; j++) {
      if(gameData.categories[j].id == 29) {
        hasCards = "Yes";
      }
    }
    var cellData = [
      [gameData.name,hasCards,gameData.is_free]
    ];
    var selection = sheet.getRange("B"+(i+2)+":D"+(i+2));
    selection.setValues(cellData);
  }
}

2 个答案:

答案 0 :(得分:0)

无论出于何种原因,值[i]未定义。尝试打印i。这是消极的吗?这个for( var i = 0; sheet.getLastRow() > 0; i--)应该是i ++吗?此外,for循环中的第二个表达式应该用i来定义。如果sheet.getLastRow()返回任何正整数,则循环将永远运行。

答案 1 :(得分:0)

在搞乱更多代码并查看更多文档之后。能够让它运作。还要感谢@Stephen L

$('#calendar').fullCalendar({
  droppable: true,
  drop: function(date) {
    alert("Dropped on " + date.format());
  }
});