【问题标题】:google sheet -- protect sheet and unprotect all non-formulas within with google apps scriptgoogle sheet - 使用 google 应用程序脚本保护工作表并取消保护所有非公式
【发布时间】:2021-04-02 17:36:22
【问题描述】:

我是 Google Apps 脚本的新手,正在尝试编写一个脚本来执行以下操作:

  1. 保护活动工作表,然后在该工作表的指定范围内:
  2. 确定所有“公式单元格/范围”(以及“非公式单元格/范围”),然后
  3. 取消保护非公式范围[]

这是我目前正在使用的(请参阅我遇到困难的部分)

function protectFormulas() {

// DEFINE WORKSHEET
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var ws = ss.getActiveSheet();
  
// PROTECT ACTIVE SHEET
    var protection = ws.protect().setDescription('Protect Fomulas');
  
// Establish Range and Formulas within [currently my range is the entire sheet]
    var range = ws.getDataRange();
    var formulas = range.getFormulas();  
    
// *****THIS IS WHERE I'M STUCK - how to determine the Range[] Array within "Formulas" ??  [aka ignore the notes]
    //var result = new Array(formulas.length)
    //for (var i = 0; i < formulas.length; i++) {
        //IF formulas [i] is blank
            //add formulas R1C1 to Result Array [how to?]
        //ELSE
            // do nothing
    //}
//  *****END OF WHERE I'M STUCK


// UNPROTECT select Ranges  
  var unprotected = ws.range;
  protection.setUnprotectedRanges(result);

  }

请帮忙!

【问题讨论】:

    标签: google-apps-script google-sheets range


    【解决方案1】:
    function protectFormulas() {
      const ss = SpreadsheetApp.getActive();
      const sh = ss.getActiveSheet();
      let up = [];
      let protection=sh.protect();
      const range = sh.getDataRange();
      const formulas = range.getFormulas();
      formulas.forEach((r, i) => {
        r.forEach((c, j) => {
          if (c == '') {//if not a formula then add range to up
             up.push(sh.getRange(i+1,j+1));
          }
        });
      });
      protection.setUnprotectedRanges(up);//unprotect ranges in up
    }
    

    【讨论】:

    • 库珀你是个天才!!现在,不是“向上”中的范围仅成为单个单元格,有没有办法修改循环以将任何/所有相邻的非公式单元格连接在一起? [限制 UP 内的#ranges]
    • 另外 - 有没有办法让用户选择他们希望搜索/保护的“范围”? [又名而不是整个工作表 sh.getDataRange();]
    • 第一个问题:可能但我不想这样做。第二个问题:ui().prompt()
    • 好的,谢谢。其他人可以压缩范围吗? (问题1)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多