Reputation: 361
I have the following code. It currently adds the checkbox to E3. I would like to modify so as a new row is added it will add a checkbox to Column E.
function AddCheckBox(e) {
var spreadsheet = SpreadsheetApp.getActive();
var cell = SpreadsheetApp.getActive().getRange('E3');
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation().requireCheckbox().build();
cell.setDataValidation(rule);
}
I tried this, but it didn't work:
spreadsheet.getActiveRange().autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
Appreciate any help.
Upvotes: 1
Views: 999
Reputation: 201368
spreadsheet.getActiveRange().autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
. From this, I thought like above.a new row is added it
means that the values are put to the cells.If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
In this pattern, the active range is used.
When this script is run, a checkbox is put to the column "E" of the row of the active range.
function AddCheckBox(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Donations Received") // Modified
var activeRange = sheet.getActiveRange();
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation().requireCheckbox().build();
var range = sheet.getRange(activeRange.getRow(), 5);
range.clearContent();
range.setDataValidation(rule);
}
In this pattern, the last row is used.
When this script is run, a checkbox is put to the column "E" of the last row of the active sheet.
function AddCheckBox(e) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Donations Received") // Modified
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation().requireCheckbox().build();
var range = sheet.getRange(sheet.getLastRow(), 5);
range.clearContent();
range.setDataValidation(rule);
}
In this pattern, it supposes that AddCheckBox(e)
is installed as the installable trigger of OnEdit event trigger.
When this script is run, a checkbox is put to the column "E" of the edited row.
function AddCheckBox(e) {
var range = e.range;
var sheet = range.getSheet();
if (sheet.getSheetName() == "Donations Received") { // Added
var criteria = SpreadsheetApp.DataValidationCriteria.CHECKBOX;
var rule = SpreadsheetApp.newDataValidation().requireCheckbox().build();
var range = sheet.getRange(range.getRow(), 5);
range.clearContent();
range.setDataValidation(rule);
}
}
range.clearContent();
, if you are not required, please remove it.If I misunderstood your question and this was not the result you want, I apologize.
Upvotes: 2