Deselect the checkboxes within an array

I've been grappling with a script to reset all checkboxes in a Google Sheet to unchecked as part of my daily cleanup routine. I've managed to identify and uncheck checkboxes in one sheet but am struggling to efficiently extend this to all sheets. The method uncheck() seems promising, and I've shared my script and array below. Here are my queries...

  1. Currently, I'm looping through the array to uncheck each box individually, but the process is slow. My goal is to apply this to all sheets in the spreadsheet. I'm worried about potential timeouts. Is there a way to uncheck all checkboxes on a single sheet at once, avoiding the need for a loop? Or better yet, can I tackle all checkboxes on all sheets simultaneously? Unchecking them in one go would definitely be more efficient, but so far, my attempts have been futile. (I've toyed with the idea of replacing the file daily with a template where all checkboxes are unchecked, but I can't afford to change the document ID)

  2. How can I modify my script to cover all tabs in the spreadsheet? Do I need to fetch all the sheets and employ another loop, or is there a smarter approach?

Script:

function uncheckCheckboxes() {
  var spreadsheet=SpreadsheetApp.getActive(); // grab spreadsheet
  var sheet=spreadsheet.getActiveSheet(); // grab current sheet
  var allRange=sheet.getDataRange(); // fetch all data
  var validations=allRange.getDataValidations();
  var Checkboxes=[]; // initialize array
  for(var i=0;i<validations.length;i++) { 
    for(var j=0;j<validations[i].length;j++) {
      var rule=validations[i][j];
      if(rule!=null) {
        var criteria = rule.getCriteriaType();
        if(criteria == SpreadsheetApp.DataValidationCriteria.CHECKBOX) {
          Checkboxes.push(Utilities.formatString('%s', sheet.getRange(i+1,j+1).getA1Notation())); // store cells with checkboxes only
        }
      }
    }
  }
  Logger.log(Checkboxes) // Logging
  for (var CB in Checkboxes) { // Loop through array
  var checkbox = Checkboxes[CB]; 
  sheet.getRange(checkbox).uncheck(); // uncheck checkbox
  }

}

Array from 1 sheet (stored in 'Checkboxes' variable): [C3, D3, E3, F3, G3, H3, C4, D4, E4, F4, G4, H4, C5, D5, E5, F5, G5, H5, C6, D6, E6, F6, G6, H6, C7, D7, E7, F7, G7, H7, C8, D8, E8, F8, G8, H8, C9, D9, E9, F9, G9, H9, C10, D10, E10, F10, G10, H10, C11, D11, E11, F11, G11, H11, C12, D12, E12, F12, G12, H12, C13, D13, E13, F13, G13, H13, C14, D14, E14, F14, G14, H14, C15, D15, E15, F15, G15, H15, C16, D16, E16, F16, G16, H16, C17, D17, E17, F17, G17, H17, C18, D18, E18, F18, G18, H18, C19, D19, E19, F19, G19, H19, C20, D20, E20, F20, G20, H20, C21, D21, E21, F21, G21, H21, C22, D22, E22, F22, G22, H22, C23, D23, E23, F23, G23, H23, C24, D24, E24, F24, G24, H24, C25, D25, E25, F25, G25, H25, C26, D26, E26, F26, G26, H26, C27, D27, E27, F27, G27, H27, C28, D28, E28, F28, G28, H28, C29, D29, E29, F29, G29]

I've been laboring on this for hours and would greatly appreciate insights from someone knowledgeable in JavaScript. (I'm definitely not a JavaScript expert!)

Answer №1

Currently untested and unable to be tested at this time.

function uncheckCheckboxesInAllSheets() {
    // The purpose of this function is to uncheck all checkboxes in all sheets within a specified document.
    
    const spreadsheet = SpreadsheetApp.getActive();
    
    for (const sheet of spreadsheet.getSheets()) {
        Logger.log(`Preparing to uncheck checkboxes in sheet ${sheet.getName()}`);
        sheet.getDataRange().uncheck();
    }
}

For more information on the Range.uncheck() method, visit https://developers.google.com/apps-script/reference/spreadsheet/range#uncheck:

This function changes the state of checkboxes in the range to "unchecked". It will ignore cells that do not currently contain the checked or unchecked value.

It appears that you can provide a multi-cell range as input, and the uncheck function will handle the specifics internally (such as bypassing irrelevant cells).


On a side note: When iterating over values directly in an array, consider using a for-of loop rather than a for-in loop.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with Chrome Extension in Selenium JavaScript WebDriver Test

I'm currently facing an issue with my Selenium JavaScript Webdriver test. Everything seems to be working fine, no errors are being thrown. The only problem is related to a Chrome Extension that is supposed to change the title of the page and then retr ...

Encountering issues with Visual Studio Code following the integration of the MongoDB API Mongoose into my code

As I delve into the world of web development, I have been exploring databases with MongoDB Atlas and mongoose. Interestingly, my debugging process has hit a bump when using the node.js(legacy) debugger in VS code after importing mongoose with const mongoos ...

Using Ajax to invoke a C# webmethod

I'm trying to call a webmethod defined in this specific class <%@ WebService Language="C#" Class="emt7anReyady.myService" %> using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Linq; usi ...

What is the procedure for importing material UI components into the main class?

Hey there! I'm currently working on integrating a "SimpleAppBar" element into my React app design. Below is the code snippet for this element sourced directly from the Material UI official website: import React from 'react'; import PropType ...

Issue encountered with create-next-app during server launch

Encountering an error when attempting to boot immediately after using create-next-app. Opted for typescript with eslint, but still facing issues. Attempted without typescript, updated create-next-app, and reinstalled dependencies - unfortunately, the prob ...

Managing 404 errors when loading cached scripts with jQuery

I need to load a JS file using jQuery, but I want the cache to be used if available. Unfortunately, I can't set the global ajax cache setting to true for reasons beyond the scope of this post. This means using the getScript method is not an option. Fo ...

Issue with JavaScript Onclick Event Handler

Rephrasing Full JavaScript Code. Javascript <div id="PopUp1" style="display: none; color: #FFFFFF;"></div> <div id="Mask"></div> <script type="text/javascript"> var Content = '<p>Content!!!!!</p><input ty ...

javascript close the current browser tab

Can someone please help me with a JavaScript code to close the current window? I have tried the following code but it does not seem to work: <input type="button" class="btn btn-success" style="font-weight: b ...

How to transfer data from PHP to JavaScript using Ajax in the webpage_BODY

I am extracting data from my Controller and passing it to a Javascript file via the Ajax success function. This data is then utilized to create charts. Here is an example of my controller logic: $result =array( "PourcentageCommande" => $Pourcen ...

Every time I use my NodeJS MySQL Query function, the results I get are never

Working on a new gaming project involving MySQL for multiplayer functionality. Issue arises when requesting specific queries, where the system retrieves incorrect data or results from previous queries. dat("SELECT * FROM server1;"); Misdirected queries r ...

The Chrome debugger will pause execution at a function without needing a DOM break point

Greetings! I am currently working on an angular js application. One issue that I have encountered is when I run the application and open the debugger by hitting F12, I notice that for every page it continuously calls a certain function and seems to stop th ...

Fascinating CSS rendering glitch observed on zooming in: all browsers create a noticeable gap between containers except for Firefox

I'm experiencing a rather intriguing and peculiar issue with css styles when zooming in and out on the browser. Specifically, I've created a material ui card where the background-color changes upon clicking with an animation effect. The animati ...

How to implement an Angular Animation that is customizable with an @Input() parameter?

Have you ever wondered if it's possible to integrate custom parameters into an Angular animation by passing them through a function, and then proceed to use the resulting animation in a component? To exemplify this concept, consider this demo where t ...

Implementing the disabled attribute in input text fields with Vue.js

There are 2 URLs that I am working with: /register /register?sponsor=4 The first route, /register, provides a clean input field where users can type freely. The second route, on the other hand, pre-fills the input with a value of 4 and disables it, ...

Is it possible to interpret the camera using radius or diameter instead of the traditional x, y, z coordinates

I've been exploring this scenario where I am trying to figure out if it is possible to move a camera by adjusting the radius and diameter instead of using x, y, z positions (Vectors). Currently, I am working with a cube, but my goal is to introduce a ...

Ways to display the ChaptersButton in Videojs-Player

I'm trying to incorporate videojs (version 8.10.0) into my project and provide viewers with a way to select chapters within the video. According to the official documentation, it seems that simply including a track element linking to a VTT file within ...

The default action is not triggered when the click event occurs

Hey there, I have been working on this <ol> list of events using jQuery (version 1.4.2). Everything is set up within the $(document).ready() function. Something strange is happening where when I click on the <li>, it triggers a click on the co ...

Can someone advise me on how to ensure my function effectively delivers a desired outcome?

My goal is to learn Javascript on my own, but I'm struggling to make progress. I've created a function called fixedSpending() that takes two inputs, num1 and num2, and adds them together. However, when the user hits the "=" button, I expect the f ...

The Facebook Customer Chat Plugin embedded within an iframe

Can the Customer Chat Plugin be used within an iframe? When adding content-security-policy: frame-ancestors IFRAME_DOMAIN, it displays the message Refused to frame 'https://www.facebook.com/' because an ancestor violates the following Content Se ...

Exploring the capabilities of JavaScript on the iOS platform

Here is the code I've written for my iOS application: webView = [[UIWebView alloc] init]; webView.delegate = self; [webView loadHTMLString:@"<script type=\"text/javascript\" src=\"myFile.js\"></script& ...