在[Google表格]列中标识并删除第一个选中的复选框 [英] Identify and delete the first checked checkbox in a column [Google Sheets]

查看:68
本文介绍了在[Google表格]列中标识并删除第一个选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 从第6行开始的C列完全填充了复选框.
  • 我要删除选中该行C列中所有框的所有行.
  • 此工作表的名称为今天",并且在下面的同名变量中.

到目前为止,这是我无法成功运行的原因,我无法确定原因:

This is what I have so far, which does not successfully run and I cannot identify why:

    var values = Today.getRange('C6:C').getValues();
    var a = 0;
      while (a<=values.length){
        if ( values[a][0] == "True" ) {   
          Today.deleteRow(a+6);           //Here I add 6 to 'a' since the range 'values' started from row 6.
        }
      a++    //Increase 'a' by 1 and continue.
      }

推荐答案

  • 您要删除在工作表名称"Today"上选中"C"列的复选框的行.
  • 此复选框位于"C6:C".
  • 您想使用Google Apps脚本实现这一目标.
  • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    • 可以通过检索数据范围来降低循环成本.
    • 当删除该行时,使用反向循环时,该过程可以更简单.因为当删除行时,行号会更改.请注意这一点.

    在此模式下,使用诸如 SpreadsheetApp 之类的电子表格服务.

    In this pattern, Spreadsheet service like SpreadsheetApp is used.

    function myFunction() {
      var sheetName = "Today"; // Please set the sheet name.
    
      var Today = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
      var values = Today.getRange('C6:C' + Today.getLastRow()).getValues();
      for (var i = values.length - 1; i >= 0; i--) {
        if (values[i][0]) Today.deleteRow(i + 6);
      }
    }
    

    模式2:

    在此模式下,使用Sheets API.因此,请在高级Google服务中启用Sheets API.删除行数很大,此方法的处理成本比模式1的处理成本低.

    Pattern 2:

    In this pattern, Sheets API is used. So please enable Sheets API at Advanced Google services. When the number of delete rows is large, the process cost of this method is lower than that of the pattern 1.

    function myFunction() {
      var sheetName = "Today"; // Please set the sheet name.
    
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName(sheetName);
      var sheetId = sheet.getSheetId();
      var values = sheet.getRange('C6:C' + sheet.getLastRow()).getValues();
      var requests = values.reduce(function(ar, [e], i) {
        if (e) ar.push({deleteDimension:{range:{sheetId:sheetId,dimension:"ROWS",startIndex:(i + 5),endIndex:(i + 6)}}});
        return ar;
      }, []).reverse();
      Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
    }
    

    参考文献:

    • 方法:sheetssheets.batchUpdate
    • 高级Google服务
    • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

      If I misunderstood your question and this was not the direction you want, I apologize.

      这篇关于在[Google表格]列中标识并删除第一个选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆