I have stumbled upon two problems after asking for help on this community and getting a quick and on point answer by a member of this community ( Tedinoz ).
So getting to the problems, I'm trying to make a spreadsheet where rows are based on the number in a column called quantity (basically quantity-1) if a checkbox is marked.
First problem: How do I write my code in order for my function to go dynamically to all rows inserted in the spreadsheet? I get an error when I try to set var row = sheet.getDataRange();
Second problem: When using the function onEdit(e)
is there a workaround so that the function doesn't add infinite rows when the checkbox is marked? I'm searching for something that adds rows based on quantity when the checkbox is True or subtracts the rows when the checkbox is False (if they where initially added for that row).
Also is there an way to copy the text of column C or a range of text (through columns when inserting rows)?
Below is what I have so far:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "Test";
var sheet = ss.getSheetByName(sheetname);
var row = sheet.getDataRange;
// get value of Column H
var colHValue = sheet.getRange(row,8).getValue()
if (colHValue = true){
//Logger.log("DEBUG: Col H = True. do something")
// get value of Column D
var Value = sheet.getRange(row,4).getValue();
// get the quantity and convert from a string to a number
var qty = Value;
var qtynum = +qty;
// var newtype = typeof qtynum; // DEBUG
//Logger.log("DEBUG: the quantity is "+qtynum+", new type = "+newtype)
// This inserts rows after
sheet.insertRowsAfter(row, qtynum-1);
}
else{
//Logger.log("DEBUG: col H <> True. do nothing");
}
}