简体   繁体   English

满足条件后锁定 Google 表格单元格/范围

[英]Lock Google Sheets cells/range after condition is met

We're using Google Sheets for financial reconciliation internally and facing some mistakes in it.我们在内部使用 Google 表格进行财务核对,但其中存在一些错误。 There is a spreadsheet with all the data to which almost everyone in company has access for editing.有一个包含所有数据的电子表格,几乎公司中的每个人都可以访问并进行编辑。 What I want to do is to lock certain cells for all users except few people when simple condition (for example, cell fill color is changed to red) is met.我想做的是在满足简单条件(例如,单元格填充颜色更改为红色)时,为除少数人之外的所有用户锁定某些单元格。 So the function description looks like:所以功能描述看起来像:

  1. everyone has access to spreadsheet每个人都可以访问电子表格
  2. cells in range (which one should be locked) are not locked范围内的单元格(应锁定的单元格)未锁定
  3. cells are not locked until condition is met在满足条件之前单元格不会被锁定
  4. user enters value to cell/range用户向单元格/范围输入值
  5. user applies condition (fill color, for example)用户应用条件(例如填充颜色)
  6. cell locks.细胞锁。 access from all users except few ones is removed除少数用户外,所有用户的访问权限都被删除
  7. users with access could edit/unlock具有访问权限的用户可以编辑/解锁

It would be much appreciated if someone could help with the exact function to apply.如果有人可以帮助应用确切的功能,我们将不胜感激。 Many thanks in advance!提前谢谢了!

The only thing I did found is the documentation which is close to my problem: https://developers.google.com/apps-script/reference/spreadsheet/range https://developers.google.com/apps-script/reference/spreadsheet/protection But I'm zero in Apps Script which is used by Google Sheets(我唯一找到的是接近我的问题的文档: https ://developers.google.com/apps-script/reference/spreadsheet/range https://developers.google.com/apps-script/reference /spreadsheet/protection但我在 Google 表格使用的 Apps 脚本中为零(

This gets you close but unfortunately I ran into the problem of onEdit events do not consider background color changes apparently... So ultimately this will only fire after a cell value changes.这让你接近但不幸的是我遇到了 onEdit 事件的问题显然不考虑背景颜色变化......所以最终这只会在单元格值改变后触发。

If the cell is Red and the range is not yet protected, it will be protected as you being the only editor.如果单元格为红色且范围尚未受到保护,那么它将受到保护,因为您是唯一的编辑者。 If the background is not red it will either strip away the protection or not change.如果背景不是红色,它将取消保护或不改变。

/**
* Protects and unprotects ranges;
* @param {Object} e event object;
*/
function onEdit(e) {
  //access cell formats;
  var bgColor = e.range.getBackground();
  var bold = e.range.getFontWeight();

  //access edited range, value and sheet;
  var rng = e.range;
  var val = e.value;
  var sh  = rng.getSheet();

  //access edited range row and column;
  var row = rng.getRow();
  var col = rng.getColumn();

  //access protections;
  var ps = sh.getProtections(SpreadsheetApp.ProtectionType.RANGE);

  //filter out other cells protections;
  ps = ps.filter(function(p){
   var ptd = p.getRange();
   if(row===ptd.getRow()&&col===ptd.getColumn()) {
     return p;
   }
  })[0];

  //SpreadsheetApp.getActive().toast(bgColor); //Uncomment to get a toast displaying background color of edited cell.

  //if protection not set -> protect;
  if(!ps) {
    if (bgColor === '#ff0000' && bold === 'bold') {
      SpreadsheetApp.getActive().toast("Cell Locked");
      var protection = rng.protect(); //protect Range;
      var users = protection.getEditors(); //get current editors;
      var emails = [Session.getEffectiveUser(),'email1@email.com','email2@email.com']; //declare list of users (emails)
      
      protection.addEditor(Session.getEffectiveUser());
      protection.addEditors(emails);
      protection.removeEditors(users); //remove other editors' access;
    }}else {
      if(!val || bgColor != '#ff0000' || bold != 'bold') { ps.remove(); } //if cell is empty -> remove protection;
    }
}

Perhaps someone can improve on this and adjust for background events only.也许有人可以对此进行改进并仅针对背景事件进行调整。 Also if working fast this doesn't keep up well.另外,如果工作速度很快,这就跟不上了。

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

相关问题 谷歌表。 锁定范围 - Google sheets. Lock range 谷歌工作表 =indirect() 在单元格范围内 - google sheets =indirect() on range of cells 如果条件不满足,则在 30 分钟后再次运行应用程序脚本(谷歌表格),但每天最多运行一次 - Run apps script (google sheets) again after 30 minutes if condition not met but maximum once per day 如果条件满足,则 Google Apps 脚本复制范围 - Google Apps Script Copy Range If Condition Met 如何从Google表格中的一系列单元格中获取值,并在满足条件的情况下将其与预定义值进行比较以发送邮件 - How to get the values from a range of cells in Google Sheets and compare the same with a predefined value to send mail if the condition mets Google Sheets 根据条件获取特定单元格范围的每日/每周计数总和 - Google Sheets Get Sum of Daily/Weekly Counts for a specific range of cells as per condition 遍历一系列Google表格单元格 - Loop through a range of cells Google Sheets 将特定范围的单元格导出到 PDF - Google 表格 - Exporting Specific Range of Cells to PDF - Google Sheets 如果在 Google 表格中满足条件,则向行添加边框格式 - Add border format to row if condition met in Google Sheets 在 Google 表格上满足条件时如何将 email 发送给自己 - How to send email to myself when a condition is met on Google Sheets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM