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); } }Explicación:
Como los usuarios ya han mencionado en los comentarios, depende de cómo se inserte la columna. Si eso se hace manualmente a través de la interfaz de usuario de la hoja de cálculo, puede obtener el número de la columna activa que está seleccionada:
const colNumber = sheet.getSelection().getActiveRange().getColumn(); y luego cree un objeto de range que representará el rango de columna completo:
const activeRng = sheet.getRange(1,colNumber,sheet.getMaxRows(),1);entonces puede alimentar este rango a cualquier función.
Por supuesto, en lugar de getMaxRows() , también puede usar getLastRow() para obtener hasta la última fila con contenido en lugar del rango completo de columnas.
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); } }