The reason I don’t call the second function directly is because I need this function to be called by some users who do not have cell protection permissions. Therefore, K2 is modifiable by everyone, however, the cells that the second function modify aren’t. So far both halves of the functions work. By clicking a button that runs the first function, I can clearly see that cell K2 is changed. Additionally, when I manually modify K2 to be “Wait…”, it also runs the second function. However, for some reason Google Appscript doesn’t count modification through functions to count as an OnEdit() trigger.
Sorry for the poor commenting, I am just getting some ideas now.
Additionally, I need to mention that add10KanbanRows() has a onEdit() trigger setup.
// Last Modified by Rajat Sengupta
// Requests an additional 10 Kanban rows by setting K2 to Wait...
// Adds a row to the kanban copying all necessary formulas and data validations
function request10KanbanRows() {
let ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Kanban");
// to lockout usage
let lockout = ss.getRange(2, 11, 1, 1) // "K2"
// prevent double running function
// Additionally, allows people to add rows bypassing the Cell protections
lockout.setBackground("red")
lockout.setValue("Wait...")
}
// Last Modified by Rajat Sengupta
// Adds a row to the kanban copying all necessary formulas and data validations
// if K2 is set to "Wait..."
function add10KanbanRows(e) {
const ss = e.source.getActiveSheet();
const lockout = e.range;
const editedCell = lockout.getA1Notation();
if (!(ss.getName() === "Kanban") || !(editedCell === "K2") || !(lockout.getValue() === "Wait...")) {
return;
}
// Get total number of tasks before so we know
let lastTaskNumber = 0
if (ss.getLastRow() > 6) {
lastTaskNumber = Number(ss.getRange(7, 2, 1, 1).getValue().slice(1))
}
console.log(lastTaskNumber)
// Insert a 10 rows at the top
ss.insertRowsAfter(6, 10);
// Copy values from the previous row (6th row)
let range = ss.getRange(6, 5, 1, 2); // "To Do" and "Doing" cells
let range2 = ss.getRange(6, 18, 1, 1); // Stale formula
let formatRange = ss.getRange(6, 1, 1, ss.getLastColumn()); // rows copies formatting - Rajat
for (let row = 16; row > 6; row--) {
range.copyTo(ss.getRange(row, 5, 1, 2), { contentsOnly: false });
range2.copyTo(ss.getRange(row, 18, 1, 1), { contentsOnly: false });
// Set the date in the new row
let date = Utilities.formatDate(new Date(), "PST", "MMMM dd");
ss.getRange(row, 13, 1, 1).setValue(date);
formatRange.copyFormatToRange(ss, 1, ss.getLastColumn(), row, row);
// To buffer numbers till 99 with leading 0s 17 because 6 header rows (including hidden rows), plus 10 because we start from bottom of 10 stack, and finally another +1 because we start counting at 1 more that last task
var taskNumber = lastTaskNumber + 17 - row;
if (taskNumber < 1000) {
taskNumber = ("00" + taskNumber).slice(-3);
}
ss.getRange(row, 2, 1, 1).setValue("T" + taskNumber);
}
lockout.setBackground("green")
lockout.setValue("Ready")
}
I have tried running each function separately, and they both work. I beleive there isn’t something functionally wrong, it is just that I may be missing a setting or something. Perhaps there is a better way to do this?
MLG_RAJ Gaming is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.