In your situation, how about the following modification?
function UpdateGroup() {
// This is from https://stackoverflow.com/a/44563639/7108653
Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) {
const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
return search ? search.getRow() : offsetRow;
};
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Loot');
const lastRowH = sheet.get1stNonEmptyRowFromBottom(8);
const lastRowJ = sheet.get1stNonEmptyRowFromBottom(10);
sheet.getRange("H23:I" + lastRowH).copyTo(sheet.getRange("J" + (lastRowJ+ 1)), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
In this modification, the last rows of "H" and "J" are retrieved and use them.
For example, the last rows of columns "H" and "I" are different and/or the last rows of columns "J" and "K" are different, how about the following modification?
function UpdateGroup() {
// This is from https://stackoverflow.com/a/44563639/7108653
Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) {
const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
return search ? search.getRow() : offsetRow;
};
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Loot');
const lastRowH = sheet.get1stNonEmptyRowFromBottom(8);
const lastRowI = sheet.get1stNonEmptyRowFromBottom(9);
const lastRowJ = sheet.get1stNonEmptyRowFromBottom(10);
const lastRowK = sheet.get1stNonEmptyRowFromBottom(11);
sheet.getRange("H23:I" + (lastRowH > lastRowI ? lastRowH : lastRowI))
.copyTo(sheet.getRange("J" + ((lastRowJ > lastRowK ? lastRowJ : lastRowK) + 1)), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
Added:
From your following reply,
This solved it, thank you!! I did not manage to make the script start checking for last empty cell from J23 and below, but this is not an issue, since I filled J1:J22 with spaces to make these cells 'not blanks'.
In this case, how about the following sample script? In this sample script, when the last rows of the column "J" and "K" are 0
, the value is copied to the row 23.
Sample script:
function UpdateGroup() {
// This is from https://stackoverflow.com/a/44563639/7108653
Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) {
const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
return search ? search.getRow() : offsetRow;
};
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Loot');
const lastRowH = sheet.get1stNonEmptyRowFromBottom(8);
const lastRowI = sheet.get1stNonEmptyRowFromBottom(9);
const lastRowJ = sheet.get1stNonEmptyRowFromBottom(10);
const lastRowK = sheet.get1stNonEmptyRowFromBottom(11);
const dest = lastRowJ > lastRowK ? lastRowJ : lastRowK;
sheet.getRange("H23:I" + (lastRowH > lastRowI ? lastRowH : lastRowI))
.copyTo(sheet.getRange("J" + ((dest >= 22 ? dest : 22) + 1)), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}