一维数组以填充电子表格列 [英] 1D Array to fill a spreadsheet column

查看:76
本文介绍了一维数组以填充电子表格列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用内置的网上论坛服务来提取用户名列表...

I'm using the built-in group service to pull a list of usernames...

function getUsersInGroup(group) {
  var usersInGroup = GroupsApp.getGroupByEmail(group).getUsers();
  return usersInGroup;
}

...它返回一个数组,像这样...

...and it returns an array, like this...

[ person1@email.com, person2@email.com, person3@email.com ]

我希望这些电子邮件显示在这样的电子表格列中...

What I would like is for those emails to show up in a spreadsheet column like this...

即使数据是扁平"的,Apps Script的.setValues()似乎也需要2D数组,因此我从测试中得出的印象是我的数据必须采用这种格式...

It seems that Apps Script's .setValues() wants a 2D array, even when the data is "flat", so I get the impression from my tests that my data has to be in a format like this...

var data = [[person1@email.com],[person2@email.com],[person3@email.com]]

如果我希望它显示为数据行而不是列中的一项.

If I want it to show up as rows of data instead of one item in a column.

如果我是对的,我想我正在寻找问题的两个(可能是三个)中最好的一个.

If I'm correct, I think I'm looking for the best of two (maybe three) possible answers to my question.

我认为可能的解决方案

  1. 变换数组并使数组中的每个项目成为一个项目数组 可能是for循环

  1. Transform the array and make each item in the array a one item array with probably a for loop

发现有一个更好的内置函数可以将 数组并将其转换为一行数据

Find out there is a better built-in function that will take the array and turn it into a row of data

还有其他我可以做的事,甚至我在这里都没有考虑

There's something else I could do that I didn't even consider here

推荐答案

如果您不想循环浏览,也可以使用工作表API.

You can also use the sheets API, If you don't want to loop through.

使用高级Google服务,并将majorDimension切换为列:

Use Advanced Google services and switching majorDimension as columns:

  var data = [ 'person1@email.com', 'person2@email.com', 'person3@email.com' ];
  var request = {
    range: 'Sheet1!A1',
    majorDimension: 'COLUMNS',
    values: [data], //still 2D
  };
  //API must be enabled before use. 
  //https://developers.google.com/apps-script/advanced/sheets
  Sheets.Spreadsheets.Values.update(request, ss.getId(), request.range, {
    valueInputOption: 'USER_ENTERED',
  });

或循环遍历:

data = data.map(function(e){return [e];});

这篇关于一维数组以填充电子表格列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆