How to include Null values in 2D array

664 views
Skip to first unread message

AdamD

unread,
Mar 9, 2022, 9:15:52 PM3/9/22
to Google Apps Script Community
I'm needing to create a 2D array that also includes null values.  Examples of the type of values I need are:

['Mon, Wed', null, 'Tue']
[null,'Mon, Tue, Wed','Fri']
['Wed',null,null]

I have tried pushing the values into the array but null/undefined values cause an error of ".push is not a function".  I then tried assigning a variable as a null value of '' and pushing that into the array.  This also caused the same error.

I have also tried filtering the results, which does not cause an error, but the array remained undefined.

The codes I have tried are:

const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow();
var lastCol = sheet.getLastColumn();
var responses = sheet.getRange(2,2,lastRow-1,lastCol).getValues();
var questions = sheet.getRange(2,1,lastRow-1,1).getValues();
var cellValue = [];

function createArray(){

for(r = 2; r <= lastRow; r++){
var question = questions[0];
var response = responses[r-2];

if(question == 'Select your preferred nights and locations.' && response != ','){
console.log('Response = '+response+' & Row = '+r);
cellValue = cellValue.push([response]);
} else {
if(question == 'Select your preferred nights and locations.' && response == ','){
var n = '';
cellValue = cellValue.push([n]);
}
}
}
}

function filter(){
var filteredValues = responses.filter(r=>questions.includes(r[0]) );
for(i = 0; i<=filteredValues.length; i++){
console.log('i = '+i+' '+filteredValues[i]);
}
}

How do you insert null into an array when it causes errors to occur?

Adam

Jonathan Butler

unread,
Mar 9, 2022, 9:55:11 PM3/9/22
to google-apps-sc...@googlegroups.com
The only objects that have a push function are arrays. A null object is not an array. If your goal is to wrap the null in an array ou should try changing "cellValue = cellValue.push([n]);" to " cellValue =[null]; "

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/0a4fa188-37d4-49ab-aad6-0a4838322044n%40googlegroups.com.

AdamD

unread,
Mar 10, 2022, 6:37:57 AM3/10/22
to Google Apps Script Community
Thank you for the reply.

The var cellValue is the final array that will be returned and it needs to include the nulls.  Setting it back to null will eliminate the values that had previously been pushed into the array.

I wonder if a stringified version of the combined arrays would allow the null values to be included in the array when converted back?  Something like the string
"'Mon, Wed', null, 'Tue'" that is then passed through .split(',') to recreate the array.

Edward Ulle

unread,
Mar 10, 2022, 8:28:26 AM3/10/22
to Google Apps Script Community
Your problem are these instructions  cellValue = cellValue.push([response]); Array.push() returns the new length of the array so you are converting cellValue from an array to an integer. Simply do this cellValue.push(). This changes the existing array cellValue to add a new value at the end

Clark Lind

unread,
Mar 10, 2022, 10:26:35 AM3/10/22
to Google Apps Script Community
I may be mistaken, but from what I can see,  response is a 2D array (more than one column), but you are treating it as a 1D array.
So you will have to go further into response to see the individual column values. Something like:

function createArray(){

for(r = 2; r <= lastRow; r++){
var question = questions[0];
var response = responses[r-2]; //this should be the whole row

     response.forEach( ( col, index ) => {   //iterate through the response looking at each column (col) and making note of the column's place (index) in the array
         if(question == 'Select your preferred nights and locations.' && (col == ',' || col == ""){ //only need to add null if the Col is blank
                response[index] = null;      //just change whatever value is already there in the array to null. Using array.push() just adds a new element to the end of array, it won't replace what is already there
            })
       })

cellValue.push([response]); 
}
console.log (cellValue) //or: cellValue.forEach( (item) => {
console.log(item)
})
}

You may have to play around with the above, but that is the general idea I think.

AdamD

unread,
Mar 11, 2022, 6:52:14 AM3/11/22
to Google Apps Script Community
Thanks again guys!  I'll hopefully have some time this weekend to mess with this more but this gave me some ideas to run with.  I'll try to remember and update later.

Adam

Reply all
Reply to author
Forward
0 new messages