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

454
Views
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 answers
Answer question

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 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!