0

I've seen the following SO quesiton and I thought my array was 2d. Here's my code:

var testingArray = [ 
    { poolIndex: 0 },
    { stakingStrategy: 'masterchef' },
    { farmName: 'sushiChef' }
    ];


function placeSushiData () {
   let ss = SpreadsheetApp.getActiveSpreadsheet();            
   let targetSheet = ss.getSheetByName('test spreadsheet');   
   let targetRange = targetSheet.getRange(11,2, 2, testingArray.length)

   targetRange.setValues(testingArray)
}

But I'm still getting this error:

Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues.

Why isn't my array 2d? If it isn't, how do I make it 2D? Or, more broadly, how do I get it into two rows of my google sheet?

2 Answers 2

4
function placeSushiData () {
  var A = [["poolIndex", 0 ],["stakingStrategy",'masterchef'],[ "farmName", 'sushiChef' ]];   
   let ss = SpreadsheetApp.getActive();            
   let sh = ss.getSheetByName('Sheet2');   
   let rg = sh.getRange(11,2, A.length, A[0].length)
   rg.setValues(A)
}

Output:

poolIndex 0
stakingStrategy masterchef
farmName sushiChef
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! So, I need to have that data in brackets; An array of arrays. Got it.
No brackets for 2d arrays. And the arrays must be rectangular. Although you can include nulls
Sorry, but aren't [] brackets? Are they called something else?
Well I guess they're both brackets there are curly brackets and square brackets ref So I should have said no curly brackets use square brackets instead.
Deep, deep trivia: { can also be referred to as an "opening brace" or even a "left brace", as well as a "left curly bracket". But [ is not used with the word "brace" - only with the word "bracket" as in "left square bracket". I am basing this on the Unicode nomenclature - see here and here. @DBWeinstein
|
2

A 2d array is a matrix. basically, it's an array of arrays of values.

So, your data should look something like this:

const testingArray = [ 
  [ "2.000", "1,000,000", "$2.99" ],
  [ "stakingStrategy", "masterchef", '' ],
  [ "farmName", "sushiChef", '' ]
];

Here's an example from the official documentation.

1 Comment

It really should be rectangular and your link does not link directly to an example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.