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?

DBWeinstein
  • 8,605
  • 31
  • 73
  • 118

2 Answers2

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
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thank you! So, I need to have that data in brackets; An array of arrays. Got it. – DBWeinstein Jun 02 '21 at 22:19
  • No brackets for 2d arrays. And the arrays must be rectangular. Although you can include nulls – Cooper Jun 02 '21 at 22:20
  • 1
    Sorry, but aren't `[]` brackets? Are they called something else? – DBWeinstein Jun 02 '21 at 22:21
  • I call these brackets {}; – Cooper Jun 02 '21 at 22:22
  • 2
    Well I guess they're both brackets there are curly brackets and square brackets [ref](https://www.thesaurus.com/e/grammar/parentheses/#:~:text=These%20%7B%20%7D%20have%20a%20variety%20of,signify%20hugging%20in%20electronic%20communication.) So I should have said no curly brackets use square brackets instead. – Cooper Jun 02 '21 at 22:25
  • 1
    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](https://www.fileformat.info/info/unicode/char/007b/index.htm) and [here](https://www.fileformat.info/info/unicode/char/005b/index.htm). @DBWeinstein – andrewJames Jun 02 '21 at 23:18
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.

Thatkookooguy
  • 6,669
  • 1
  • 29
  • 54