I have a spreadsheet, which is edited by external app via Google Sheets API.
Then, I have a Google Apps Script function in that spreadsheet, which is called by installable on-change trigger. In the beginning this function checks if changeType equals to 'EDIT', and then checks the last column of edited range using getActiveRange(). This works fine, but sometimes getActiveRange() returns smaller range, than it was actually edited.
Function code:
function myOnChange(e) {
if (e.changeType !== 'EDIT') {
Logger.log(`Wrong changeType: ${e.changeType}`);
return;
}
var source_sheet = e.source.getActiveSheet();
var range = source_sheet.getActiveRange();
if (range.getLastColumn() !== 7) {
Logger.log(`Wrong last column: ${range.getLastColumn()}`); // 9 line
Logger.log(`NumColumns: ${range.getNumColumns()}`); // 10 line
Logger.log(`SpreadsheetApp last column: ${SpreadsheetApp.getActiveRange().getLastColumn()}`); // 11 line
return;
}
// Do some stuff
}
For example, we have empty range A3:G5. After inserting values in this range, it fills up properly (including G column), but getActiveRange() in the function returns range - A3:F5 (getLastColumn() and getNumColumns() returns 6 (9-11 line of code above), although order of column G is 7).
Why is this happening? I have about 30 sheets in this spreadsheet and this happens only on a few of them. Maybe number of rows on the sheet causes this somehow?
Also I found similar question, but it has no answer: getActiveRange returning a smaller range than expected
UPDATE 1:
To reproduce the problem, you need to do following steps:
- Create empty sheet.
- Add empty 1000 rows to initial 1000, so you would have 2000 empty rows. (I think this is the reason of the problem)
- Add function from the above to this spreadsheet.
- Add onChange trigger, that will call function from step 3.
- Call the API request to insert values. You can do it with "Try this API" section of the page. Here is an example of request parameters.
- After step 5, in the sheet you should get filled row with the values from your request. For my example, it is a row in range A1:G1.
- But
getActiveRange()returns A1:F1 range andgetLastColumn()is 6.
UPDATE 2:
I think I found the margin. With 1539 or less rows (they can be empty) getActiveRange() works fine. If there are 1540 or more rows in the sheet, then this function will return wrong range: if 'F' column is included in the edited range, getActiveRange() will return range [start column][start row]:F[end row]. So, for example:
if edited range is A1:G1, function will return A1:F1;
if edited range is E2:M5, function will return E2:F5;
if edited range is F10:L10, function will return F10;
if edited range is H3:N9, function will return right range H3:N9;
Need to mention that this issue appears only after API call, if the sheet is edited manually it still works fine.
If you were able to reproduce this issue, please, let me know, that I'm not crazy.
lastColumnevery time I make changes. What do you mean 'this option'? Where is it used? - Yuri KhristichlastColumnreturns wrong value only on a few sheets that I have. What do you think about number of rows in the sheet? Can it possibly cause the problem? Can you try to reproduce it on the sheet with about 900 rows? - loykods