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 checkSlicersOnSheet1() {
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".
2
Answers
This is what I wanted and this shows "All" or has all checkboxes ticked in all Slicers:
Select the range of cells where the check boxes are in one contiguous range and then run this function.
This version will toggle them: