Incorrect Range Height - Google Script

2019-02-15 18:18发布

I'm trying to create a Google sheet script that will take a list of names and resend them to the Google Sheet. I have two columns of data, the first column contains a persons name. The second column contains multiple cells that the person in the first cell is inviting to a party. This creates a problem, the name in column 1 might be on row 2, but if they invite 20 people then column one has blank spaces rows 3-21. It may sound pointless right now to most of you, but I want to be able to sort the sheet alphabetically by the name of the person who did the inviting, AND be able to sort it into a separate sheet alphabetically by the name of the guest in Column 2, while still keeping the person who invited them tracked as well. This is the only way I could think of accomplishing the task.

I'm currently stuck on writing the array back to the sheet, I keep getting "Incorrect range height, was 1 but should be 339." I've figured out how to successfully get an array of data, filled exactly how I wanted it, but can't seem to get this part. I've searched through here and tried to implement the solutions I find, but have had no luck.

This is what I have come up with so far, and it works up until the setValues(

 function inviteSorter() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var current = sheet.getSheets()[0];
  var lastRow = current.getLastRow();
  var rangeData = current.getRange(2,1,lastRow-1,3);
  var numColumns = rangeData.getNumColumns();
//  Logger.log(rangeData);
  var info = rangeData.getValues();
  var Name = {};
//  Examines the cell in the first column, if it is empty replaces it with the name from the previous cell.
  for ( var i = 0; i< info.length; i++){
    if (typeof(info[i][0]) == "string" && info[i][0] == ""){
      Name[i] = Name[i-1];
    } else{
    Name[i] = info[i][0];
    }

  }
  var data = []
  for (var i = 0; i<lastRow-1; i++){
    data.push(Name[i]);
  }

  var writeRange = current.getRange(2,1,data.length,1);

  writeRange.setValues([data]);

1条回答
做个烂人
2楼-- · 2019-02-15 18:37

The value you are expecting should be a 2D array, 1 column of multiple rows. What you get when using data.push(Name[i]); is a simple array of strings.

Try this way : data.push([Name[i]]); this will return an array of arrays and should satisfy the conditions for setValues(data)

( don't forget to remove the brackets in your last setValues statement )

查看更多
登录 后发表回答