简体   繁体   English

取消选中数组中的复选框(Google Apps 脚本 - 表格)

[英]Uncheck checkboxes in an array (Google Apps Script - Sheets)

I'm trying to write a script that finds all the checkboxes in a Google Sheet, and resets them all to unchecked.我正在尝试编写一个脚本来查找 Google 表格中的所有复选框,并将它们全部重置为未选中状态。 (A daily sheets cleanup) I am able to get an array of all the cells that contain checkboxes from one sheet and uncheck them, but I'm spinning my wheels here on how to efficiently get all the checkboxes on all sheets to uncheck. (每日工作表清理)我能够从一张工作表中获取包含复选框的所有单元格的数组并取消选中它们,但我正在这里讨论如何有效地取消选中所有工作表上的所有复选框。 I see there is a method called uncheck() so that's what I'm trying to use.我看到有一个名为 uncheck() 的方法,这就是我要使用的方法。 My script and array is below.我的脚本和数组如下。 My questions are...我的问题是...

  1. Currently I'm looping through the array and unchecking each box one at a time.目前我正在遍历数组并一次取消选中每个框。 The script is kinda slow and my end goal is to have this go through all sheets within the spreadsheet.脚本有点慢,我的最终目标是让这个通过电子表格中的所有工作表。 I'm concerned the script may time out.我担心脚本可能会超时。 Is there a way to uncheck all the checkboxes on 1 sheet at once, without doing a loop that goes through them 1 at a time?有没有一种方法可以一次取消选中 1 张纸上的所有复选框,而不需要一次遍历它们 1 个的循环? Or even better having it do all checkboxes on all sheets at once?或者甚至更好地让它一次完成所有工作表上的所有复选框? Obviously unchecking them in 1 go would be more efficient, but idk if that's an option and everything I've tried has not worked.显然,一次取消选中它们会更有效率,但如果这是一个选项,我尝试过的一切都没有奏效。 (I've also considered replacing the file each day with a template file that has all the checkboxes unchecked already, however I need the document ID to never change) (我也考虑过每天用一个模板文件替换文件,该文件已经取消选中所有复选框,但是我需要文档 ID 永远不会改变)

  2. How do I get my script to go through all tabs of the spreadsheet?如何让我的脚本浏览电子表格的所有选项卡? Would that be getting all the sheets and doing another for loop?那会得到所有的工作表并做另一个 for 循环吗? Or is there a more efficient way to do this too?或者有没有更有效的方法来做到这一点?

Script:脚本:

function uncheckCheckboxes() {
  var spreadsheet=SpreadsheetApp.getActive(); // get spreadsheet
  var sheet=spreadsheet.getActiveSheet(); // get current sheet
  var allRange=sheet.getDataRange(); // get all data in sheet
  var validations=allRange.getDataValidations();
  var Checkboxes=[]; // create empty 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())); // get array of only cells w/ checkboxes
        }
      }
    }
  }
  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, H29] 1 张表中的数组(存储在“复选框”变量中):[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, D11 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、F2 G22、H22、C23、D23、E23、F23、G23、H23、C24、D24、E24、F24、G24、H24、C25、D25、E25、F25、G25、H25、C26、D26、E26、F26、G2 H26、C27、D27、E27、F27、G27、H27、C28、D28、E28、F28、G28、H28、C29、D29、E29、F29、G29、H29]

I've been working on this for hours and would love some expertise from someone who actually knows what they are doing.我已经为此工作了几个小时,并且希望能够从真正知道自己在做什么的人那里获得一些专业知识。 (I'm definitely no JavaScript expert!) (我绝对不是 JavaScript 专家!)

Thanks in advance.提前致谢。

Amber琥珀色

Untested, can't test at the moment.未测试,暂时无法测试。

function uncheckCheckboxesFromAllSheets() {
    // Should uncheck all checkboxes from all worksheets 
    // in a given document.
    
    const spreadsheet = SpreadsheetApp.getActive();
    
    for (const sheet of spreadsheet.getSheets()) {
        Logger.log(`About to uncheck checkboxes in sheet ${sheet.getName()}`);
        sheet.getDataRange().uncheck();
    }
}

Documentation for Range.uncheck() ( https://developers.google.com/apps-script/reference/spreadsheet/range#uncheck ): Range.uncheck()文档( https://developers.google.com/apps-script/reference/spreadsheet/range#uncheck ):

Changes the state of the checkboxes in the range to “unchecked”.将范围内复选框的状态更改为“未选中”。 Ignores the cells in the range which currently do not contain either the checked or unchecked value configured.忽略范围内当前不包含配置的选中或未选中值的单元格。

Think that means you can pass it a multi-cell range and the uncheck function will internally take care of details for you (like skipping irrelevant cells).认为这意味着您可以将多单元格范围传递给它,而uncheck函数将在内部为您处理细节(例如跳过不相关的单元格)。


Unrelated: If you have an array and you need to loop over its values directly, maybe use for-of loop instead of for-in .无关:如果你有一个数组并且你需要直接循环它的值,可以使用for-of循环而不是for-in

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM