You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Jamf API获取设备组数据填充Google Sheet时遇TypeError报错求助

问题

尝试通过Jamf MDM的API获取一组设备数据并展示在Google Sheet中,代码已能获取到数据,但无法写入表格,报错信息为:TypeError: Cannot read properties of undefined (reading 'length')。

附代码:

function getGroup() {
  // Set the API URL and group ID
  const apiUrl = "https://MYURL/JSSResource/computergroups/id/377";


  // Make the request to the Jamf API
  const response = UrlFetchApp.fetch(apiUrl, {
    headers: {
      "Authorization": "Basic MYKEY",
      "Accept": "application/xml"
    }
  });
  
  // Parse the response as XML
  const xml = XmlService.parse(response.getContentText());
  const root = xml.getRootElement();
  var computers = root.getChildren("computer")
  var list=[]

    computers.forEach(function(item){
    var computer=[]
    item.getChildren().forEach(function(details){
      computer.push(details.getValue())
    })
    list.push(computer)
  })

  Logger.log(response.getContentText())
  
  // Create a new Google Sheet and set the first column to the list of computers
  var sheet = SpreadsheetApp.getActiveSheet()  
  sheet.getRange(sheet.getLastRow()+1,1,list.length,list[0].length).setValues(list)
  

}
分析与解决

错误原因

报错出现在list[0].length,说明list[0]是undefined,核心问题是XML解析路径错误:Jamf API返回的电脑组XML结构中,<computer>节点并非直接在根节点<computer_group>下,而是嵌套在<computers>节点内,原代码直接用root.getChildren("computer")无法获取到有效数据,导致computers为空数组,最终list也为空,访问list[0].length触发类型错误。

修复步骤

  1. 修正XML解析路径
    先获取<computers>父节点,再从中提取<computer>列表,避免空数组:

    const root = xml.getRootElement();
    // 先拿到computers节点,再获取子computer节点
    const computersNode = root.getChild('computers');
    const computers = computersNode ? computersNode.getChildren('computer') : [];
    
  2. 增加空数据判断
    在写入Sheet前检查list是否为空,避免访问list[0].length报错:

    var sheet = SpreadsheetApp.getActiveSheet();
    if (list.length === 0) {
      Logger.log('未获取到有效设备数据');
      return;
    }
    sheet.getRange(sheet.getLastRow()+1, 1, list.length, list[0].length).setValues(list);
    
  3. 可选:指定具体字段提取(优化数据结构)
    原代码提取所有子节点值,可能导致列顺序混乱,建议明确指定需要的字段(比如设备ID、名称),示例:

    computers.forEach(function(item){
      var computer = [
        item.getChildText('id'),
        item.getChildText('name'),
        item.getChildText('serial_number')
      ];
      list.push(computer);
    })
    

完整修复后代码

function getGroup() {
  const apiUrl = "https://MYURL/JSSResource/computergroups/id/377";

  const response = UrlFetchApp.fetch(apiUrl, {
    headers: {
      "Authorization": "Basic MYKEY",
      "Accept": "application/xml"
    }
  });
  
  const xml = XmlService.parse(response.getContentText());
  const root = xml.getRootElement();
  const computersNode = root.getChild('computers');
  const computers = computersNode ? computersNode.getChildren('computer') : [];
  var list=[]

  computers.forEach(function(item){
    // 明确提取需要的字段,避免数据混乱
    var computer = [
      item.getChildText('id'),
      item.getChildText('name'),
      item.getChildText('serial_number')
    ];
    list.push(computer);
  })

  Logger.log('获取到设备数量:' + list.length);
  
  var sheet = SpreadsheetApp.getActiveSheet();
  if (list.length === 0) {
    Logger.log('未获取到有效设备数据');
    return;
  }
  sheet.getRange(sheet.getLastRow()+1, 1, list.length, list[0].length).setValues(list);
}

内容的提问来源于stack exchange,提问作者fission

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:45:27