Google Apps脚本-TypeError:无法读取未定义的属性“长度”

时间:2020-02-18 19:46:00

标签: google-apps-script google-sheets typeerror

我有一个Google Sheets脚本,该脚本应该从API抓取数据,但出现错误

TypeError:无法读取未定义的属性“ length”(第5行,文件“ draftOrder”)

如何解决此错误?我已经好多年没有接触过该脚本了,所以我不确定是什么原因造成的。

  function draftOrder(ownerRange, numRounds) {
  var output = [];
  var owners = [];

  for (var i = 0; i < ownerRange.length; i++) {
    if (ownerRange[i][0] !== '') {
      owners.push(ownerRange[i][0]); 
    }
  }

  for (var i = 0; i < numRounds; i++) {
    if (i % 2 == 0) {
      output = output.concat(owners);
    } else {
      output = output.concat([].concat(owners).reverse());
    }

  }
  return output;
}

在此先感谢您提供的任何帮助。干杯!

1 个答案:

答案 0 :(得分:1)

function draftOrder(ownerRange, numRounds) {
  var output = [];
  var owners = [];
  if(ownerRange && numRounds) {//checking for valid inputs

    for (var i = 0; i < ownerRange.length; i++) {
      if (ownerRange[i][0] !== '') {
        owners.push(ownerRange[i][0]); 
      }
    }

    for (var i = 0; i < numRounds; i++) {
      if (i % 2 == 0) {
        output = output.concat(owners);
      } else {
        output = output.concat([].concat(owners).reverse());
      }

    }
    return output;
  }else{
    SpreadsheetApp.getUi().alert('Invalid Inputs')
  }
}