Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Javascript 2D Array to two-2D Arrays

I'm in Google Apps Script. I grab all of the dta from a spreadsheet, "Documents". The top row is "Letters", "", "Templates", "". Column 1 has human readable descriptions of documents (e.g. "Letter to Client"). Column 2 has Google Docs IDs for the template. Column 3 goes back to human readable descriptions of memoranda, and column 4 has Google Docs IDs for the template.

My goal is to break the array generated from this sheet into two separate arrays, so that var larray = ["letter to client", "docID"] (and so on; there are about 10 rows of letters and 7 rows of memoranda). I need to be able to address, e.g., larray[4][1] and return the docID.

Here's my script but for now I'm only getting a 1D array, not a 2D array. What am I missing to force larray into 2d?

function testArray(){
  var data = ssAF.getSheetByName('Documents').getDataRange().getValues();
  var larray = [];
  var marray = [];
  for(var i = 0; i < data.length; i++){
    if(data[0][i] == "Letters"){
      for (var j = 0; j < data.length; j++) {
        if (data[j][i] != "") {
          larray.push(data[j][i], data[j][i+1])
        }
      }
    }
    else if (data[0][i] == "Memoranda"){
      for (var j = 0; j < data.length; j++) {
        if (data[j][i] != "") {
          marray.push(data[j][i]);
        }
      }
    }
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Description

If larray = [], larray.push(a,b) will result in a 1D array [a,b]. And larray.push(c,d) becomes [a,b,c,d]. To make a 2D array use larray.push([a,b]), now larray is [[a,b]]. larray.push([c,d]) becomes [[a,b],[c,d]]

Script

function testArray(){
  var data = ssAF.getSheetByName('Documents').getDataRange().getValues();
  var larray = [];
  var marray = [];
  for(var i = 0; i < data.length; i++){
    if(data[0][i] == "Letters"){
      for (var j = 0; j < data.length; j++) {
        if (data[j][i] != "") {
          larray.push([data[j][i], data[j][i+1]])
        }
      }
    }
    else if (data[0][i] == "Memoranda"){
      for (var j = 0; j < data.length; j++) {
        if (data[j][i] != "") {
          marray.push([data[j][i]]);
        }
      }
    }
  }
}

Reference

  • https://www.w3schools.com/jsref/jsref_push.asp
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!