Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性";“长度”;来自未定义的变量_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript TypeError:无法读取属性";“长度”;来自未定义的变量

Javascript TypeError:无法读取属性";“长度”;来自未定义的变量,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我曾经使用过这样的代码:从站点中提取表格信息,然后将其放入Google工作表中。虽然这几个月来效果很好,但我注意到它已经随机停止工作了 我收到消息“TypeError:无法从undefined读取属性“length”。“来自代码: for (var c=0; c<current_adds_array.length; c++) { for(var c=0;c实例化新数组时请小心。您当前使用的是var current\u adds\u array=array()。您不仅缺少new关键字,

我曾经使用过这样的代码:从站点中提取表格信息,然后将其放入Google工作表中。虽然这几个月来效果很好,但我注意到它已经随机停止工作了

我收到消息“TypeError:无法从undefined读取属性“length”。“来自代码:

  for (var c=0; c<current_adds_array.length; c++) {

for(var c=0;c实例化新数组时请小心。您当前使用的是
var current\u adds\u array=array()
。您不仅缺少
new
关键字,而且此构造函数旨在实例化具有类似数组对象的数组


尝试将此更改为
var current\u adds\u array=[]

当您遇到错误时运行的代码必须与上面的代码不同。Google Apps脚本有点奇怪(它基本上是2003年左右的JavaScript,但有一些较新的位[而不是其他位]),但是上面的
current\u adds\u array
的用法非常简单,我无法想象GAS在这方面有什么奇怪的地方。谢谢。现在我想起来这是有道理的。现在我遇到了一个问题:TypeError:无法调用undefined的方法“getChild”。(第29行,文件“code”)代码:var trcontents=tablecontents.getChild('tbody').getChildren('tr');@nirholas在访问
tablecontents
trcontents
时,您的代码为
null
未定义的
留出了空间,您应该使用
getElementById
函数中的else语句返回
null
(剩下的代码将执行后续检查)或在其中搜索的根元素(更安全)。
如果(id!=null&&id.getValue()==idToFind)返回elt;
应该变成
返回id!=null&&id.getValue()==idToFind?elt:element;
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Get Data')
      .addItem('Add new dispatch items','addNewThings')
      .addToUi();
}


function addNewThings() {
  // get page
  var html = UrlFetchApp.fetch("#").getContentText();

  // bypass google's new XmlService because html isn't well-formed
  var doc = Xml.parse(html, true);
  var bodyHtml = doc.html.body.toXmlString();
  // but still use XmlService so we can use getDescendants() and getChild(), etc.
  //   see: https://developers.google.com/apps-script/reference/xml-service/
  doc = XmlService.parse(bodyHtml);
  var html = doc.getRootElement();

  // a way to dig around
  // Logger.log(doc.getRootElement().getChild('form').getChildren('table'));
  // find and dig into table using getElementById and getElementsByTagName (by class fails)
  var tablecontents = getElementById(html, 'formId:tableExUpdateId');
  // we could dig deeper by tag name (next two lines)
  // var tbodycontents = getElementsByTagName(tablecontents, 'tbody');
  // var trcontents = getElementsByTagName(tbodycontents, 'tr');
  // or just get it directly, since we know it's immediate children
  var trcontents = tablecontents.getChild('tbody').getChildren('tr');
  // create a nice little array to pass
  var current_adds_array = Array();
  // now let's iterate through them
  for (var i=0; i<trcontents.length; i++) {
    //Logger.log(trcontents[i].getDescendants());
    // and grab all the spans
    var trcontentsspan = getElementsByTagName(trcontents[i], 'span');
    // if there's as many as expected, let's get values
    if (trcontentsspan.length > 5) {
      var call_num = trcontentsspan[0].getValue();
      var call_time = trcontentsspan[1].getValue();
      var rptd_location = trcontentsspan[2].getValue();
      var rptd_district = trcontentsspan[3].getValue();
      var call_nature = trcontentsspan[4].getValue();
      var call_status = trcontentsspan[5].getValue();
      //saveRow(call_num, call_time, rptd_location, rptd_district, call_nature, call_status);
      current_adds_array.push(Array(call_num, call_time, rptd_location, rptd_district, call_nature, call_status));
    }
  }
  saveRow(current_adds_array);
}  
//doGet();

function saveRow(current_adds_array) {
  // load in sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  // find the current last row to make data range
  var current_last_row = sheet.getLastRow();
  var current_last_row_begin = current_last_row - 50;
  if (current_last_row_begin < 1) current_last_row_begin = 1;
  if (current_last_row < 1) current_last_row = 1;
  //Logger.log("A"+current_last_row_begin+":F"+current_last_row);
  var last_x_rows = sheet.getRange("A"+current_last_row_begin+":F"+current_last_row).getValues();

  var call_num, call_time, rptd_location, rptd_district, call_nature, call_status;
  // iterate through the current adds array
  for (var c=0; c<current_adds_array.length; c++) {
    call_num = current_adds_array[c][0];
    call_time = current_adds_array[c][1];
    rptd_location = current_adds_array[c][2];
    rptd_district = current_adds_array[c][3];
    call_nature = current_adds_array[c][4];
    call_status = current_adds_array[c][5];

    // find out if the ID is already there
    var is_in_spreadsheet = false;
    for (var i=0; i<last_x_rows.length; i++) {
      //Logger.log(call_num+" == "+last_15_rows[i][0]);
      if (call_num == last_x_rows[i][0] && call_time != last_x_rows[i][1]) is_in_spreadsheet = true;
    }
    Logger.log(is_in_spreadsheet);
    //Logger.log(last_15_rows.length);
    if (!is_in_spreadsheet) {
      Logger.log("Adding "+call_num);
      sheet.appendRow([call_num,call_time,rptd_location,rptd_district,call_nature,call_status]);
    }
  }

}

function getElementById(element, idToFind) {  
  var descendants = element.getDescendants();  
  for(i in descendants) {
    var elt = descendants[i].asElement();
    if( elt !=null) {
      var id = elt.getAttribute('id');
      if( id !=null && id.getValue()== idToFind) return elt;    
    }
  }
}

function clearRange() {
  //replace 'Sheet1' with your actual sheet name
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange('A2:F').clearContent();}

function getElementsByTagName(element, tagName) {  
  var data = [];
  var descendants = element.getDescendants();  
  for(i in descendants) {
    var elt = descendants[i].asElement();     
    if( elt !=null && elt.getName()== tagName) data.push(elt);      
  }
  return data;
}

var sheet = SpreadsheetApp.getActiveSheet();  
var range = sheet.getRange("C:C");   
range.setValues(range.getValues().map(function(row) {
  return [row[0].replace(/MKE$/, " Milwaukee, Wisconsin")];
}));