1D Array to fill a spreadsheet column

前端 未结 2 1176
甜味超标
甜味超标 2021-01-07 15:31

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

function getUsersInGroup(group) {
  var usersInGroup = GroupsApp.getGroupByEmail(group).         


        
相关标签:
2条回答
  • 2021-01-07 15:51

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

    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',
      });
    

    Or loop through:

    data = data.map(function(e){return [e];});
    
    0 讨论(0)
  • 2021-01-07 16:04

    As @TheMaster pointed out, a simple loop with the map function worked well. Here's what I did:

    function writeArrayToColumn(array) {
      var mainSheet = SpreadsheetApp.getActiveSheet(); 
    
      var newArray = array.map(function (el) {
        return [el];
      });
    
      var range = mainSheet.getRange(2, 1, newArray.length, 1);
    
      range.setValues(newArray);
    }
    
    0 讨论(0)
提交回复
热议问题