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

456
Vistas
Google Sheets App Script onChange(event) for INSERT_COLUMN: how to get which column was inserted?
function onInsertColumn(activeRng) {  
  /* I coded this function, and it works, so I'll omit 
  the implementation here because it's irrelevant. 
  But I haven't figured out how to get the active range
  from the onChange event (to pass to this function).*/
}

function onChange(event) {
  // https://stackoverflow.com/a/66686524/470749
  // https://stackoverflow.com/a/64454240/470749
  // https://developers.google.com/apps-script/guides/triggers/installable#google_apps_triggers
  // https://developers.google.com/apps-script/guides/triggers/events
   
  if(event.changeType == 'INSERT_COLUMN'){
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = ss.getActiveSheet()
    const activeRng = sheet.getSelection().getActiveRange(); // WHAT SHOULD GO HERE?
    onInsertColumn(activeRng);
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Explanation:

As users have mentioned already in the comments, it depends on how the column is inserted. If that is done manually via the spreadsheet UI then you can get the number of the active column that is selected:

const colNumber = sheet.getSelection().getActiveRange().getColumn();

and then create a range object that will represent the full column range:

const activeRng = sheet.getRange(1,colNumber,sheet.getMaxRows(),1);

then you can feed this range to any function.

Of course instead of getMaxRows() you can also use getLastRow() to get the up until the last row with content instead of the full column range.

Solution when manually inserting a column:

function onInsertColumn(activeRng) {  
  // example
  activeRng.setValue("Selected");
}


function onChange(event) {
  // https://stackoverflow.com/a/66686524/470749
  // https://stackoverflow.com/a/64454240/470749
  // https://developers.google.com/apps-script/guides/triggers/installable#google_apps_triggers
  // https://developers.google.com/apps-script/guides/triggers/events
   
  if(event.changeType == 'INSERT_COLUMN'){
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = ss.getActiveSheet();
    const colNumber = sheet.getSelection().getActiveRange().getColumn();
    const activeRng = sheet.getRange(1,colNumber,sheet.getMaxRows(),1);
    onInsertColumn(activeRng);
  }
}
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