Google Scripts: Populating Multi-Dimensional Array With Cell Values - arrays

I am learning the basics of Google Scripting after using VBA in Excel for many years, so please forgive the basic question.
I want to take a range of cells in my sheet using getValues() and then create a multi-element array.
Here is a simple example from my code:
var gameInfo = [];
gameInfo = gameMasterSheet.getRange(3, 3, 1, 9).getValues();
As you can see, the defined range is 9 cells in a single row.
My goal is to create an array from these 9 cells, and have each cell be accessible via a separate array element. However, it seems all 9 cell values are being inserted into gameInfo[0], and when I reference gameInfo[2] hoping to obtain the value of the third cell in the range, "undefined" is returned.
Is there a way to use getValues() to populate an array with separate elements? If so, how is this done? If not, what is a better alternative?
Thanks for any help you all can provide!

getValues returns a 2d array, even when it's a single row of cells. Think of it like this: gameInfo[row - 1][column - 1], so the top left is gameInfo[0][0].
All of your data is in gameInfo is in one row (gameInfo[0]), so the third element will be accessed as gameInfo[0][2] (row 1, column 3).
gameInfo[2] would be the third row, which is indeed outside of the range and undefined.
Also: to get just the values into an array from a 2d array, you could do this:
const values = [];
gameInfo.forEach(row => {
row.forEach(column => {
values.push(column);
})
})

See the script at the bottom of the Simple Mail Merge Tutorial for some useful code to retrieve data. Copy all the code from this comment down:
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects'
// tutorial.
//
/////////
/////////////////////////////////////////////////////////////////////////////////
// getRowsData iterates row by row in the input range and returns an array of objects.
Once you have, use getRowsData to retrieve the data as an array of objects. If I understand you correctly, this will get you want you are after.
Karl S

Related

Function similar to vlookup: Why doesn't my indexOf work?

If I would like to match the word that I need to find it the same as <lookup> in Excel. I intend to create a form for Example. I have a file certain big data and I will create a box for fill in data that you need then Enter it show the Description of data. Now I got stuck I don't know how to write to the script I have learned in youtube but don't have a solution that nearby with my need it nearby just <Indexof> function.
var data = Activesheet.getRange(1,1,Activesheet.getLastRow()-1,1).getValues();
Logger.log(data.indexOf("TPBSA"));
data is a 2D(two dimensional) array. indexOf only works with a 1D array. flatten the array before indexOf:
const data = [["A1"],["A2"],["TPBSA"],["A4"]];
console.info(data.flat().indexOf("TPBSA"));
//or
console.info(data.findIndex(e=>e[0]==='TPBSA'))

Printing array to variable cell range VBA

I have a variable array which I want to print into another workbook range. As I loop through separate arrays and secondary workbooks, the cell I want to print to will change.
Some sheets will need to array to be printed to A6:N, some will need the array printed to A300:N etc. I want to get to the bottom of the list of existing data and 'paste' the array below it.
Here is what I have so far:
bottomrow2 = Range("A9999").End(xlUp).Row
Set PasteCell = ClientBook.Sheets("PasteSheet").Range(Cells(bottomrow2 + 1,1),Cells(bottomrow2 + 1, 14))
Range(PasteCell & UBound(array)) = array
PasteCell is effectively trying to be my A1:N. It's worth noting that if I hard-code where to paste the array to, it works fine.
Range("A1:N" & UBound(array)) = array
'^This works fine.
Thanks for any help you can give.
You need to resize Paste Cell, so
Set PasteCell=PasteCell.Resize(ubound(array),14)
something like that. Or just use the anchor cell maybe, set paste cell to just be bottomrow2,1 ?

How to return an array between two array based on a cell value in Google spreadsheet?

I have a Google spreadsheet with cells A2:D20 as the first array.
The other array is F2:I20.
What I want to do is to return an array out of the above two at K2:N20 based on the value of D20. If D20 > 100, return the array A2:D20. If not, F2:I20. How to go about this problem?
Pretty much like you have worded it:
=index(if(D20>100, A2:D20, F2:I20))

How to derive values from an array using the index value

I am trying to use an array in NetLogo to store my images and call the values using their index. Looks like I am getting stuck with common manner of accessing the array's value via arrayName[0].
How do I do that in NetLogo? googling doesn't seem to have the answer.
My array:
let imgArray ["easy1.png" "easy2.png" "easy3.png" "easy4.png" "easy5.png"]
I am trying to fix the image in the following manner:
clear-drawing import-drawing imgArray[1]
You're looking for item:
item 1 imgArray
Note that it's zero-indexed, so the first item is item 0 imgArray, though first imgArray is more idiomatic.
Also, arrays in NetLogo are called lists.

How to copy cell to array in Matlab

Alright let me explain detailed my question
this below image is displaying my matrix where i want to copy my data
Alright now what i want to do that is as you can see 1x4 cell
i want to copy it as an array to another variable such as
input_values=ones(1,4);%init
input_values=input_matrix_training(1);
So at the above i am trying to copy the elements in that cell array which is row 1 to the input_values array. But if i do as i above i am getting this instead of the values that array contains. ty
instead of above it should be like
The other values are a cell, and are thus best referenced with {} instead of (). Also, sometimes they need to be wrapped into [], depending on the format. Plus the fact that you don't need to initialize input_values, and what you should do becomes this:
input_values=[input_matrix_training{1}];
Or you can just use cell2mat
input_values=cell2mat(input_values(1));

Resources