Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

106
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda