【问题标题】:Get all ranges with data获取所有数据范围
【发布时间】:2020-03-09 13:27:34
【问题描述】:

我有一个电子表格,其中的数据分布在不同工作表的不同单元格中。我想要一个名为protect的函数,它将所有未受保护的单元格与数据设置为受保护的,最好不必遍历每张表中的每个单元格,因为这是我在此保护函数运行之前所做的事情。

有没有办法为所有不为空的单元格/范围或至少组合相邻范围的单元格/范围获取范围或范围列表。

例如:range("A2") + range("A3") + range("A4") -> range("A2:A4")

为图片中的工作表运行 protect() 应检索选定的范围并将它们中的每一个设置为受保护。

【问题讨论】:

  • 哦,我认为您不依赖于用户选择,而只是寻找包含数据的单元格。我不认为有一个内置的方法。你可以使用.getDataRange() 之类的东西,但它也会拾取空单元格。为了得到你想要的,你可能必须编写一个函数。
  • 您可以使用 getDataRange 并且您需要迭代抛出包括空单元格在内的整个数组,同时使用条件来根据该条件进行操作。您也可以使用getNextDataCell 跳过空单元格,但它的性能可能最差。
  • “有没有办法为所有不为空的单元格/范围或至少组合相邻的范围获取范围或范围列表。”你想要这两个选项中的哪一个?
  • @Tedinoz,要么。由于我已经遍历所有单元格以获取数据,因此我可以将每个单元格 A1 表示法存储在一个列表中并制作一个范围列表。但是,我会评估尽可能多的范围,因为它可能是例如除 G1 外,范围 A1:P1 中的所有单元格都有数据。在这种情况下,这些可以组合成 2 个范围 A1:F1 和 H1:P1,而不是 15 个单独的单元格。尽管正如句子所暗示的那样,前者更可取。

标签: google-apps-script google-sheets


【解决方案1】:

您有一个工作表,其中没有可区分的具有值的单元格的模式。您想运行一个脚本,该脚本将找到每个具有值的单元格并保护它(如果它尚未受到保护)。

向@TheMaster 提供How to format UnprotectedRanges? 中的答案,我已在此处改编。

以下脚本使用电子表格“保护类”ref

逻辑是:

  • sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);:获得“保护”。
  • var rngList = protections.map(function(pro) {return pro.getRange().getA1Notation();});:获取受保护单元格的 A1 符号值数组
  • if (outputname[o] !==""){:逐列遍历每个单元格并测试非空白单元格
  • var indexOfFirst = rngList.indexOf(a1note);:检查非空白单元格是否列在受保护单元格数组中
  • var warning = outrange.protect().setWarningOnly(true);:如果没有,那就保护细胞吧。

function so5884236203(){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "Sheet1";
  var sheet = ss.getSheetByName(sheetname);

  // get sheet protectiuons info
  var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  // Logger.log(protections); //DEBUG
  var rngList = protections.map(function(pro) {
    return pro.getRange().getA1Notation();
  });
  // Logger.log(rngList); //DEBUG


  // get sheet dimensions, range and values
  var LR = sheet.getLastRow();
  var LC = sheet.getLastColumn();
  var range = sheet.getRange(1,1,LR,LC);
  //Logger.log(range.getA1Notation()); //DEBUG
  var values = range.getValues();


  // loop though the values by column
  for (var i=0;i<LC;i++){

    // get values for just a single column
    var outputname = values.map(function(e){return e[i];});//[[e],[e],[e]]=>[e,e,e]
    var outlen =  outputname.length; 
    // Logger.log("DEBUG: i="+i+" "+outputname)

    // loop through the cells in the column
    for (var o=0;o<outputname.length;o++){
      // Logger.log("DEBUG: o="+o+", value = "+outputname[o]);

      // test for blank cells
      if (outputname[o] !==""){
        // cell is not blank

        // get the range and A1Notation
        var outrange = sheet.getRange(o+1,i+1);
        var a1note = outrange.getA1Notation();
        //Logger.log("this cell is "+a1note);

        //test if this cell is in the list of protected cells
        var indexOfFirst = rngList.indexOf(a1note);
        if (indexOfFirst != -1){
          // cell is protected
          // Logger.log("DEBUG: Cell "+a1note + " is  protected");
        }
        else {
          // cell is not protected
          //Logger.log("DEBUG: cell "+a1note+" is  NOT protected");

          // protect the cell
          var warning = outrange.protect().setWarningOnly(true);

          // optionally colour the cell to indicate protection
          //outrange.setBackground("wheat");
        }      
      }
    }
  }
}

之前


之后

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多