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);
}
}
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.
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);
}
}