I’m trying to programmatically check all boxes in a Google Sheet’s sheet using Apps Script.
I think there is code somewhere whether it’s from the source or in some other code else that edits the slicers to show (e.g.) 1 of 4 checkboxes, but I was unable to find it or it may not be visible to me.
Specifically, I’m trying to get the Slicers and select all checkboxes in said slicers, then iterate to the next slicer until there are no more.
Along with extensively looking through Google Documentation and forums, I have yet to find a solution.
Here are just a few of the .gs code I have tried:
function selectAllSlicers() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var slicers = spreadsheet.getSlicers();
slicers.forEach(function(slicer) {
var items = slicer.getRange().getValues();
slicer.setRangeValues(items);
});
}
//This works in retrieving the names of the Slicers.
// -
function checkAllSlicers() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var slicers = spreadsheet.getSlicers();
slicers.forEach(function(slicer) {
var range = slicer.getRange();
var checkboxes = range.getDataValidations();
checkboxes.forEach(function(row, rowIndex) {
row.forEach(function(cell, colIndex) {
if (cell == SpreadsheetApp.DataValidationCriteria.CHECKBOX) {
range.getCell(rowIndex + 1, colIndex + 1).setValue(true);
}
});
});
});
}
// No visible change on the sheet.
//--
function checkSlicersOnDailyPlanner() {
const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/<SPREADSHEET_ID>/edit');
const sheet = ss.getSheetByName('Sheet 1');
const slicers = sheet.getSlicers();
for (const slicer of slicers) {
const slicerValues = slicer.getRange().getSlicerValues();
if (slicerValues) {
for (const value of slicerValues) {
slicer.setIsItemChecked(value, true);
}
}
}
}
//Changes all cells to "TRUE".
Joey Tatú is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.