The offset of the data in the column with the IMPORTAGE function

Well, I wrote my own code that works almost well. but unfortunately, the onEdit function does not take into account that the range is changed by the IMPORTAGE function. I had to add a checkbox, when changing which the function starts working. but I would like to automatically track changes, taking into account that the range is changed by the IMPORTAGE function.

The code simply aligns the range AL4:AL103 to the last filled cell in the range AK4:AK103

function onEdit(e) {
if (e && e.source) {
if (e.source.getSheetName() !== 'Calculation') return;
}
if (e) {
if (e.range && e.range.columnStart == 37 && e.range.rowStart >= 1 && e.range.rowEnd <= 1) {
const sheet = e.range.getSheet();
const values = sheet.getDataRange().getValues();
const valuesA = sheet.getRange('AK4:AK102').getValues();
const valuesB = sheet.getRange('AL4:AL102').getValues();
// Здесь обрабатываем filteredValues
// Подсчёт значений во всём диапазоне
const allValuesA = valuesA.flat(); // Сглаживаем массив значений
const stringValuesA = allValuesA.filter(String); // Удаляем нестроковые значения
const countA = stringValuesA.length; // Считаем количество оставшихся значений
const allValuesB = valuesB.flat(); // Сглаживаем массив значений
const stringValuesB = allValuesB.filter(String); // Удаляем нестроковые значения
const countB = stringValuesB.length; // Считаем количество оставшихся значений
// Находим индекс последней строки с данными в диапазоне AK4:A103
const lastRowWithDataIndexA = valuesA.findLastIndex(row => row.join('').trim() !== '')+4;
// Находим индекс последней строки с данными в диапазоне AL4:AL103
const lastRowWithDataIndexB = valuesB.findLastIndex(row => row.join('').trim() !== '')+4;
const firstRowWithDataIndexB = valuesB.findIndex(row => row.join('').trim() !== '')+4;
const firstDataB = lastRowWithDataIndexB-countB+1
const targetFirstDataPosB = lastRowWithDataIndexA-countB+1
const targetRange = sheet.getRange(targetFirstDataPosB, 38, 10, 1); // Целевой диапазон смещен на rowCount строк вниз
sheet.getRange(firstDataB, 38, valuesB.length, 1).copyTo(targetRange, {contentsOnly: true}); // Копирование содержимого столбца B в целевой диапазон
const delData = targetFirstDataPosB-4
if (targetFirstDataPosB >= 4) {
sheet.getRange(4, 38, delData, 1).clear({contentsOnly: true});
}   
}
}
}

I wanted my code to track changes in AK4:AK103 cells. which in turn change the IMPORTAGE functions. the onEdit function did not help me in this, I had to add a checkbox to cell AK1. when changing which, the function started working.

can you tell me how best to implement this?

can the onChange or onSelectionChange functions help me with this? I couldn’t get them to work in my code.