Reputation: 491
I've tried a few ways to code for insertion of rows, but they give the wrong outputs repeatedly.
In the spreadsheet, when I type IF in cell C2, a blank row should be inserted AFTER the C2 IF. This happens and is correct. But when I subsequently type IF in cell C3, the GAS interpreter inserted a blank line at the C3 row and pushed the C3 IF to C4. This should not happen. It should insert a blank row AFTER the row where the C3 IF was at, but it did not. The same thing happens when I use AND, WHEN and OR. Each of these have different insertion codes. There should not be any blank rows between the rows of IF, AND, WHEN and OR. Is there a solution for this?
I've shared my simplified Google Sheet and the reduced code (a minimal reproducible example) at:
https://docs.google.com/spreadsheets/d/11skN3VLxSqQOqT7NX6tXTUY6kFYivbr9w-Yn2pI4z1s/edit?usp=sharing
And here's the reduced code for ease of analysis:
function onEdit(e) {
let c = e.range
let cvalue = e.value;
if (cvalue == "IF") {
c.getSheet().insertRowAfter(c.rowStart);
}
if (cvalue == "AND") {
c.getSheet().insertRows(c.rowStart + 1,1);
}
if (cvalue == "WHEN") {
c.getSheet().insertRowsAfter(c.rowStart, 1);
}
if (cvalue == "OR") {
c.getSheet().insertRowAfter(c.getRowIndex());
}
}
I've also attached 2 pictures: the first of which is what's supposed to happen, and the other is the wrong output because the GAS interpreter malfunctioned:
Upvotes: 4
Views: 336
Reputation: 50799
IF
in C2 and presses enter.activateAsCurrentCell()
) to make UI repent soon and seamlessly.function onEdit(e) {
const c = e.range
const cvalue = e.value;
console.log({rg:c.getA1Notation(),e})
if (cvalue == "IF") {
c.getSheet().insertRowAfter(c.rowStart);
}
if (cvalue == "AND") {
c.getSheet().insertRows(c.rowStart + 1,1);
}
if (cvalue == "WHEN") {
c.getSheet().insertRowsAfter(c.rowStart, 1);
}
if (cvalue == "OR") {
c.getSheet().insertRowAfter(c.getRowIndex());
}
if(/OR|WHEN|AND|IF/g.test(cvalue)){
c.activateAsCurrentCell();
SpreadsheetApp.flush();
c.offset(1,0).activateAsCurrentCell()
};
}
Upvotes: 2