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

102
Views
How do I extract data from certain columns in AppScript?

I am trying to fecth some data from a google spreadsheet in an AppScript standalone file.

I tried with this piece of code, but it is definitely incomplete. It raises this error:

TypeError: Cannot read property 'getDataRange' of undefined

My function:

var sheet=SpreadsheetApp.openById('1n2a5iecKo7seMXcef81V_pxWSRjE4agQ9DSoROtf0').getSheets()[0]
function getColumnId(sheet, namecol) {
          var data = sheet.getDataRange().getValues();
          var col = data[0].findIndex((name) => name === namecol) + 1;
          return col;
        }

After extracting the column data, I want to store that data in a normal list:

var IDCol=getcolumnId(sheet, 'ID')
var AFCol=getcolumnId(sheet, 'AF')

Is that possible in this way? I'd do debugging but I do not know how to proceed first with my function.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I believe your goal is as follows.

  • You want to retrieve the values from a column using the function getColumnId(sheet, namecol).

Modification points:

  • Your function name is getColumnId. But you are trying to call the function with getcolumnId. I think that an error occurs.
  • var sheet=DriveApp.getFileById('1n2a5ieOMcKo7seMXcef81V_pxWSRjE4agQ9DSoROtf0').getBlob() is Blob. In your script, an error occurs at var data = sheet.getDataRange().getValues().
  • In order to retrieve the values from the column, I think that it is required to retrieve the values using the value of col. For this

Modified script:

function getColumnId(sheet, namecol) {
  var data = sheet.getDataRange().getValues();
  var col = data[0].findIndex((name) => name === namecol);
  var transpose = data[0].map((_, c) => data.map(r => r[c])); // Here, "data" is transposed.
  return transpose[col];
}

// Please run this function.
function sample() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set the sheet name.
  var IDCol = getColumnId(sheet, 'ID')
  var AFCol = getColumnId(sheet, 'AF')
  console.log(IDCol)
  console.log(AFCol)
}

When this modified script is run, the values of columns with the header values of ID and AF.

Reference:

  • map()

Added:

From your following replying and your updated question,

Hi Tanaike, I will definitely use your approach mapping the values as you said. But first of all I need to read the data from gsheet properly. Please see my Edit and the error is raised. My code is not place in the google spreadsheet itself but in a StandAlone file in AppsScript

I tried with this piece of code, but it is definitely incomplete. It raises this error:

TypeError: Cannot read property 'getDataRange' of undefined

If you are trying to directly execute the function getColumnId(sheet, namecol) by the script editor, such an error occurs, because sheet is undefined. I thought that this might be the reason for your current issue. From this situation, I thought that you might want to use sheet, IDCol and AFCol as the global variable. If my understanding is correct, how about the following sample script?

Sample script:

var sheet = SpreadsheetApp.openById('###').getSheets()[0];

function getColumnId(sheet, namecol) {
  var data = sheet.getDataRange().getValues();
  var col = data[0].findIndex((name) => name === namecol);
  var transpose = data[0].map((_, c) => data.map(r => r[c]));
  return transpose[col];
}

var IDCol = getColumnId(sheet, 'ID')
var AFCol = getColumnId(sheet, 'AF')

// Please run this function.
function myFunction() {
  console.log(IDCol)
  console.log(AFCol)
}
  • In this function, when myFunction is run, you can see the values at the log.
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!