[英]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:
所以功能描述看起来像:
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.