2
votes

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
Hope this helps. Maybe your last line should read writeRange.setValues(data); .. But from the looks of things you are learning how to program, this is not a good place to get help with that. Good luck. - eddyparkinson
I'll admit I have a basic understanding of it all, but enough of a grasp on the concepts that I can achieve what I want most times. I usually get hung up on things I'm not seeing the right way. I knew that I needed to "transpose" my array into a column format, but I wasn't seeing the obvious solution. My original code had been your suggestion, and I got an object to array error. My code would have worked except I was attempting to write a row of data into a column. Like I said I get the concepts, I just get hung up some times. - arhubart2
Thanks for the suggestion though. Sorry if that came off hostile, rereading it it kind of does. - arhubart2
I have a basic rule with debugging. Gather more information: So... add logs, use the debugger, change my code, modify a working example. My but, of which I have too many, are always caused by lack of information. .... Glad you got to the bottom of it. - eddyparkinson

1 Answers

7
votes

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 )