如何修复Google Apps脚本中的“无法将数组转换为对象[] []”错误

时间:2019-08-08 14:44:27

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

我正在尝试获取所有组和组中所有成员的列表,这些列表将发布到名为“ allGroups”的电子表格中。但是,每当我尝试将Array打印到工作表时,都会收到错误消息,提示我无法将Array转换为Object。

我尝试将数组设置为不同的大小,更改范围以使其更具体,然后更改代码以使组名首先发布(清除数组),然后从那里移动,但是它没有没有用。

function listAllGroupsAndMembers() {
  var ss = SpreadsheetApp.getActive();
  var groupPageToken, groupPage;
  var listArray = [];
  var outputSheet = ss.getSheetByName('allGroups') || ss.insertSheet('allGroups', 1);
  outputSheet.clear();
  do {
    groupPage = AdminDirectory.Groups.list({
      domain: 'google.com',
      maxResults: 100,
      pageToken: groupPageToken
    });
    var groups = groupPage.groups; //Gets the list of groups and begins to iterate for each one
    if (groups) {
      for (var i = 0; i < groups.length; i++) {
        var group = groups[i];
        listArray.push([group.name]);
        var membersPageToken, membersPage;
        do {
          membersPage = AdminDirectory.Members.list(group.email, {
            maxReults: 100,
            pageToken: membersPageToken});
          var members = membersPage.members;
          if (members) {
            for (var u = 0; u < members.length; u++) {
              var member = members[u];
              listArray.push(member.email);
              outputSheet.getRange(u+1, i+1, listArray.length, listArray[0].length).setValues(listArray);
            } 
        listArray = [];
      } membersPageToken = membersPage.nextPageToken;
    } while (membersPageToken);
   } 
  }
 } while (groupPageToken);
  try {
    outputSheet = ss.getSheetByName('allGroups');
    outputSheet.getDataRange();
  } catch(err) {
    outputSheet = ss.insertSheet('allGroups', 2);
  }
}

预期结果将是在第1行中填充一个组列表,并且成员电子邮件列表将出现在每个组下方。目前,一旦我到达 outputSheet.getRange(u+1, i+1, listArray.length, listArray[0].length).setValues(listArray);  它告诉我不能将数组转换为对象而失败。

编辑

感谢AMolina,Ross和Cooper,我设法使它正常运行。这是我现在得到的代码:

function listAllGroupsAndMembers() {
  var ss = SpreadsheetApp.getActive();
  var groupPageToken, groupPage;
  var listArray = [];
  var outputSheet = ss.getSheetByName('allGroups') || ss.insertSheet('allGroups', 1);
  var p = 0;
  outputSheet.clear();
  do {
    groupPage = AdminDirectory.Groups.list({
      domain: 'google.com',
      pageToken: groupPageToken
    });
    var groups = groupPage.groups; //Gets the list of groups and begins to iterate for each one
    if (groups) {
      for (var i = 0; i < groups.length; i++) {
        var group = groups[i];
        listArray.push([group.name]);
        var membersPageToken, membersPage;
        do {
          membersPage = AdminDirectory.Members.list(group.email, {
            maxReults: 100,
            pageToken: membersPageToken});
          var members = membersPage.members;
          if (members) {
            for (var u = 0; u < members.length; u++) {
              var member = members[u];
              listArray.push([member.email]);
            } 
          if(membersPageToken != undefined) {
            p = p + 200;
          } else { p = 0; }
            Logger.log(p);
          outputSheet.getRange(p+1, i+1, listArray.length, listArray[0].length).setValues(listArray);
          listArray = [];
          } membersPageToken = membersPage.nextPageToken;

        } while (membersPageToken);
      } 
    }
  } while (groupPageToken);
  try {
    outputSheet = ss.getSheetByName('allGroups');
    outputSheet.getDataRange();
  } catch(err) {
    outputSheet = ss.insertSheet('allGroups', 2);
  }
}

当一个组中的成员超过200个时,它也可以处理。

1 个答案:

答案 0 :(得分:0)

尝试一下:

function listAllGroupsAndMembers() {
  var ss = SpreadsheetApp.getActive();
  var groupPageToken, groupPage;
  var listArray = [];
  var outputSheet = ss.getSheetByName('allGroups') || ss.insertSheet('allGroups', 1);
  outputSheet.clear();
  do {
    groupPage = AdminDirectory.Groups.list({
      domain: 'sbtagent197.eu',
      maxResults: 100,
      pageToken: groupPageToken
    });
    var groups = groupPage.groups; //Gets the list of groups and begins to iterate for each one
    if (groups) {
      for (var i = 0; i < groups.length; i++) {
        var group = groups[i];
        listArray.push([group.name]);
        var membersPageToken, membersPage;
        do {
          membersPage = AdminDirectory.Members.list(group.email, {
            maxReults: 100,
            pageToken: membersPageToken});
          var members = membersPage.members;
          if (members) {
            for (var u = 0; u < members.length; u++) {
              var member = members[u];
              listArray.push([member.email]);
            } 

            // This is where I made the change, moving this line outside the inner loop.
            outputSheet.getRange(1, i+1, listArray.length, listArray[0].length).setValues(listArray);

            listArray = [];
          } membersPageToken = membersPage.nextPageToken;
        } while (membersPageToken);
      } 
    }
  } while (groupPageToken);
  try {
    outputSheet = ss.getSheetByName('allGroups');
    outputSheet.getDataRange();
  } catch(err) {
    outputSheet = ss.insertSheet('allGroups', 2);
  }
}

我通过更改outputSheet.getRange(1, i+1, listArray.length, listArray[0].length).setValues(listArray);行的位置来修改了您拥有的代码,您在循环中多次调用了该代码,这导致标题多次出现。基本上,您是一遍又一遍地编写group_name -> members块,每次都低一行,所以看起来好像您在写标题时就写了很多遍。

在数组完成后,编辑后的代码将设置一次值,它将在第一行中写入组名,并在其下一行中写入成员。展望未来,我建议您考虑使用@Cooper的建议,使用getValues()setValues()可以更轻松地处理工作表。